Retryable Writes in MongoDB
Tags: mongodb, 36, newfeature, retryalbewrites
Published: 2017-11-10 01:39:28

---
title: "Retryable Writes in MongoDB"
published: true
tags: MongoDB,3.6,new feature,retryalbe writes
canonical_url: https://www.kenwalger.com/blog/nosql/mongodb/retryable-writes/
cover_image: https://thepracticaldev.s3.amazonaws.com/i/zuv81fbjz2irp2levywl.png
---

MongoDB 3.4 offered some [great new features](https://www.kenwalger.com/blog/nosql/new-version-new-features/).&nbsp;MongoDB version 3.6 is quickly approaching a final release date and is introducing many new features too. As I discussed previously, one feature is [change streams](https://dev.to/kenwalger/change-streams-coming-soon-in-mongodb-36-967). Another very exciting and much anticipated new feature is retryable&nbsp;writes.

### The Need for Retryable Writes

In previous versions of [MongoDB](https://www.mongodb.com), 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,&nbsp;a [primary steps&nbsp;down](https://docs.mongodb.com/manual/core/replica-set-elections/), or a [butterfly](https://www.amazon.com/gp/product/078948983X/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=078948983X&linkCode=as2&tag=kenwalgersite-20&linkId=8afe7c700070a08f2f0053fef2c8416e) ![](https://www.kenwalger.com/blog//ir-na.amazon-adsystem.com/e/ir?t=kenwalgersite-20&l=am2&o=1&a=078948983X) flaps it’s wings in [Ecuador](https://www.amazon.com/gp/product/0241245745/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0241245745&linkCode=as2&tag=kenwalgersite-20&linkId=61f4992dae867976e401b1909bcc3d30) ![](https://www.kenwalger.com/blog//ir-na.amazon-adsystem.com/e/ir?t=kenwalgersite-20&l=am2&o=1&a=0241245745) and causes a network malfunction in [Chicago](https://www.amazon.com/gp/product/B01D6P25O4/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=B01D6P25O4&linkCode=as2&tag=kenwalgersite-20&linkId=4a94952d1dc3aa13dff59353966228ae) ![](https://www.kenwalger.com/blog//ir-na.amazon-adsystem.com/e/ir?t=kenwalgersite-20&l=am2&o=1&a=B01D6P25O4). Or any other host of possibilities.

### Driver Handling

Retryable writes in version 3.6 removes the handling of these system&nbsp;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 include `insertMany()` and `bulkWrite()`. Although there are some limitations to using `bulkWrite()` with the new feature.

###### Retryable Write Limitations

In MongoDB 3.6 there are a&nbsp;_few_ current limitations where retryable&nbsp;behavior isn’t supported. Although these limitations may be removed in the future.

1. Writes with an unacknowledged write concern, `{w: 0}`
2. Write commands that might affect multiple documents using a single statement
3. 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&nbsp;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&nbsp;_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&nbsp;any loss of write operations,&nbsp;retryable writes are a big benefit. One of the many reasons to look at the [latest release](https://www.mongodb.com/download-center) of MongoDB.

* * *

Follow me on Twitter [@kenwalger](https://www.twitter.com/kenwalger) to get the latest updates on my postings.

There are a few MongoDB specific terms in this post. I created a [MongoDB Dictionary](https://www.echoskillstore.com/MongoDB-Dictionary/45103) skill for the [Amazon Echo](https://www.amazon.com/gp/product/B01DFKC2SO/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=B01DFKC2SO&linkCode=as2&tag=kenwalgersite-20&linkId=f9e513223de2525a72b95cf9561db55b)&nbsp;line of products. Check it out and you can say “Alexa, ask MongoDB for the definition of a&nbsp;document?” and get a helpful response.

[![Facebook](https://i1.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png?resize=48%2C48&ssl=1 "Share on Facebook")](http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fretryable-writes%2F&t=Retryable%20Writes%20in%20MongoDB&s=100&p%5Burl%5D=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fretryable-writes%2F&p%5Bimages%5D%5B0%5D=https%3A%2F%2Fi1.wp.com%2Fwww.kenwalger.com%2Fblog%2Fwp-content%2Fuploads%2F2017%2F11%2Frw_feature.png%3Ffit%3D125%252C125%26ssl%3D1&p%5Btitle%5D=Retryable%20Writes%20in%20MongoDB "Share on Facebook")[![twitter](https://i0.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/twitter.png?resize=48%2C48&ssl=1 "Share on Twitter")](https://twitter.com/intent/tweet?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fretryable-writes%2F&text=Hey%20check%20this%20out "Share on Twitter")[![google_plus](https://i1.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/google_plus.png?resize=48%2C48&ssl=1 "Share on Google+")](https://plus.google.com/share?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fretryable-writes%2F "Share on Google+")[![reddit](https://i1.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png?resize=48%2C48&ssl=1 "Share on Reddit")](http://www.reddit.com/submit?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fretryable-writes%2F&title=Retryable%20Writes%20in%20MongoDB "Share on Reddit")[![linkedin](https://i2.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png?resize=48%2C48&ssl=1 "Share on Linkedin")](http://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fretryable-writes%2F&title=Retryable%20Writes%20in%20MongoDB "Share on Linkedin")[![mail](https://i2.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png?resize=48%2C48&ssl=1 "Share by email")](mailto:?subject=Retryable%20Writes%20in%20MongoDB&body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fretryable-writes%2F "Share by email")

The post [Retryable Writes in MongoDB](https://www.kenwalger.com/blog/nosql/mongodb/retryable-writes/) appeared first on [Blog of Ken W. Alger](https://www.kenwalger.com/blog).