12-"MongoDB Getting Started Tutorial" MongoDB write concern

Source: Internet
Author: User
Tags mongodb server

MongoDB Write attention

MongoDB provides write attention to the level of assurance used to describe the return information of a database write operation. The intensity of the written attention determines the level of assurance. When inserting, updating, deleting is a weak write concern, the operation returns faster. If the write attention is weak, the write operation may not last when some write fails. The stronger the write attention level, the longer the client will need to wait for MongoDB to confirm the write operation.

MongoDB provides different levels of write attention to better meet the needs of different applications. In a real MongoDB deployment, the client needs to adjust the level of write attention to ensure the persistence of important write operations. For other less important writes, the client needs to adjust to weak write attention to ensure better performance.

Write the level of attention

MongoDB defines the following four levels of write attention, from weak to strong in turn:

    • Unacknowledged
    • Acknowledged
    • Journaled
    • Replica acknowledged

The actual application is generally in the client driver set write attention, it and the Db.getlasterror () method is very important. In general, Mongo driver calls the Db.getlasterror () command immediately after performing a write operation (Insert,update,delete). This gives you the opportunity to know if the write operation was successful, and if you catch an error, you can handle it accordingly. MongoDB server does not care about errors and is only responsible for notifying the client that an error has occurred.

Here are 2 points to note:
1. The Db.getlasterror () method is called by driver, so the business code does not need to be called explicitly.
2. Driver calls the Db.getlasterror () function, but does not necessarily catch errors, depending primarily on the setting level of the write concern.

Unacknowledged

At this level of write attention, MONGODB returns the result immediately, and then performs a write operation, similar to ignoring the write error. However, the driver will go to receive and handle network errors. So the return value of GetLastError () must be null, even if the subsequent apply error has occurred, driver does not know. Write calls at this level are returned immediately, so the performance is the best, but the reliability is the worst, so it is not recommended for use. Concern,driver In the latest version of driver for each platform, 0 is no longer the default level. In fact, there is a w:-1 level, is the error ignored, basically and w:0 almost. The difference is that w:-1 does not catch any errors, and w:0 can capture network error.

Acknowledged

At this level of write attention, Mongod confirms the write operation and completes the data change on the memory view. It allows clients to capture networks, duplicate keys and other errors. The difference with unacknowledged is that MongoDB now returns a response to GetLastError () only after apply (the actual write operation) is complete. So if an error occurs at the time of writing, the driver can be captured and processed. This level of write concern has basic reliability. But the acknowledged level of write attention does not ensure that the data is persisted to the system hard drive, which is the default write attention level for MongoDB.

Journaled

The write concern at the acknowledged level is also not absolutely reliable. Because MongoDB's apply operation is to write data to memory and write to the hard disk periodically via Fsync. If after apply, Fsync before Mongod hangs, or even the server hangs, that persistence is actually a failure. However, at the w:1 level, driver cannot capture the error in this case because response has returned to driver after apply. MongoDB solves this problem by using the journal mechanism, the write operation after writing to the memory, will also write to the journal file, so if the mongod is not normal down, restart can be based on the contents of the journal file, to restore the write operation. At the driver level, in addition to setting w:1, set Journal:true or j:true to capture the error in this case. At this level of attention, MongoDB ensures that the write operation submits the data to the journal log. This write attention can ensure that MongoDB crashes after recovering data. Of course, if you have to turn on the journal log function. However, the write operation must wait until the next log commit to return the results, in order to reduce the operation delay, to improve the frequency of log submission configuration.

Replica acknowledged

With this write attention level, the results of GetLastError () will not be returned until secondary has finished copying from primary. You can also set journal:true or j:true at the same time, and wait for Journal writes to be successful before returning. Note, however, that as long as Primary's journal writes are returned, there is no need to wait for secondary's journal to write. Similarly, W:3 can be set to indicate that at least 3 nodes have data, or w:majority, which indicates that the >1/2 node has data. The general small-scale cluster is a 3-node deployment, so the configuration of W:2 is possible.

Summary

Setting the write concern level is actually a trade-off between performance and reliability of the write operation. The longer the write operation waits, the better the reliability. For non-critical data, it is recommended to use the default W:1, and for critical data, it is better to use W:1 & J:true. It is important to note that journal is recommended to open anyway, set j:true, just say driver call GetLastError () to wait for journal write to complete before returning. This is not to say that the server-side journal is closed without setting j:true.

About GetLastError ()

In general, developers write code, do not need to call the Db.getlasterror () function, driver after each write operation, will automatically call the method immediately.

db.collection("test", {}, function(err, collection) {    collection.insert({        name: "world peace"    },    function(err, result) {        assert.equal(null, err);        console.log(result);        db.close();    })});

This code, driver after insert (), implicitly calls Db.getlasterror (), and if any errors are caught, it is assigned to the ERR parameter in the callback function. The difference is in the ability to catch errors. In W:-1, err is always null (no chance to catch error), and in w:0, it is generally not captured except for network error, and if Mongod apply has an error at W:1, it is passed to the Err parameter. The code is the same, the difference is in the set write concern level. Please refer to http://docs.mongodb.org/manual/reference/command/getLastError/for detailed instructions.

12-"MongoDB Getting Started Tutorial" MongoDB write attention

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.