
MongoDB 3.4 offered somegreat new features. MongoDB version 3.6 is quickly approaching a final release date and is introducing many new features too. As I discussed previously, one feature ischange streams. Another very exciting and much anticipated new feature is retryable writes.
The Need for Retryable Writes
In previous versions ofMongoDB, it was incumbent upon the developer and the application to handle situations where a write doesn’t happen. Imagine sending a create or update command to your database and something happens. A network partition occurs, aprimary steps down, or abutterfly flaps it’s wings inEcuador
and causes a network malfunction inChicago
. Or any other host of possibilities.
Driver Handling
Retryable writes in version 3.6 removes the handling of these system failures from the application to the database itself. The MongoDB driver is now capable of automatically retrying many write operations. Meanwhile, the server is in charge of the handling the processing of the write request and the exactly-once concept.
Supported Retryable Write Operations
MongoDB 3.6 supports this functionality for single-statement write operations such as:
insertOne()
updateOne()
replaceOne()
deleteOne()
findOneAndDelete()
findOneAndReplace()
findOneAndUpdate()
Supported multi-statement operations includeinsertMany()
andbulkWrite()
. Although there are some limitations to usingbulkWrite()
with the new feature.
Retryable Write Limitations
In MongoDB 3.6 there are a few current limitations where retryable behavior isn’t supported. Although these limitations may be removed in the future.
- Writes with an unacknowledged write concern,
{w: 0}
- Write commands that might affect multiple documents using a single statement
- Commands other than insert, delete, update, and findAndModify. For example, aggregation commands using the
$out
operator.
How it Works in a Nutshell
As I mentioned, retryable writes are handled on the server. But it is implemented through the driver through another new feature in MongoDB 3.6, logical sessions. By creating a session with the server, we are now able to establish a unique transaction identifier for each write operation.
> var session = startSession( { retry_writes : True });> session.db.collection.updateOne({'_id': 1}, {'$inc': {'counter': 5}} );
This transaction identifier for the session is re-sent to the server, by the driver, to determine the success of a previous write attempt. This implementation brings with it some big wins.
- No extra code is needed for applications, such as save points or retry logic.
- Retryable writes aren’t limited only to idempotent operations.
- They are safe for operations that fail acknowledge write success due to timeout exceptions.
Wrap Up
Retryable writes are yet another great feature brought forth by the team at MongoDB. For applications that simply cannot tolerate any loss of write operations, retryable writes are a big benefit. One of the many reasons to look at thelatest release of MongoDB.
Follow me on Twitter@kenwalger to get the latest updates on my postings.
There are a few MongoDB specific terms in this post. I created aMongoDB Dictionary skill for theAmazon Echo line of products. Check it out and you can say “Alexa, ask MongoDB for the definition of a document?” and get a helpful response.
The postRetryable Writes in MongoDB appeared first onBlog of Ken W. Alger.
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse