MongoDB: What happens if two clients update the same document at the same time?

Updated: February 3, 2024 By: Guest Contributor Post a comment

Overview

When two or more clients attempt to update the same document at the very same time in MongoDB, understanding the outcome is crucial for database integrity and consistency. MongoDB employs certain mechanisms to handle concurrent writes and maintain database performance. Let’s deep dive into concurrent updates and explore different scenarios you might encounter.

What Is Concurrent Document Update?

Concurrent document updates occur when multiple operations try to modify the same data at the same time. This might lead to conflicts and undesirable behavior if not properly managed—which is where concurrency control comes in.

In MongoDB, concurrency control is handled by a system of locks, which assists in governing access to the database for read and write operations, ensuring that only one write operation impacts a single document at any given moment.

Default Behavior in MongoDB

MongoDB uses a method called ‘locking’ to prevent multiple clients from writing to the same document simultaneously. Until version 2.2, MongoDB used a global write lock; however, from version 2.2 onwards, the locking system improved to database-level, and from version 3.0 to collection level and document level locking with WiredTiger storage engine in some cases.

Code Example: Simple Update Operations

db.products.updateOne(
  { _id: ObjectId('507f191e810c19729de860ea') },
  { $set: { price: 9.99 } }
);

If two clients execute this operation simultaneously, MongoDB will lock the document on a first-come, first-served basis. The second client’s operation will have to wait until the first one is completed. If the operations are truly simultaneous, it will be down to the operational timing as to which client obtains the lock first. You typically won’t see an error, just one of the updates ‘losing’ because the second write overwrites the first one.

Handling Concurrent Updates Safely

To handle concurrent updates securely and avoid lost updates, you might want to use update operations with atomic operators or employ optimistic concurrency with version numbers.

Atomic Operators

MongoDB provides atomic operators like $inc, $push, and $addToSet to help you update values in a document atomically and prevent issues with concurrent updates.

db.products.updateOne(
  { _id: ObjectId('507f191e810c19729de860ea') },
  { $inc: { stock: -1 } }
);

Even if multiple clients execute this atomic update simultaneously,each increase or decrease of the stock field will be processed in the order received, thus preventing any overwrite and ensuring no stock count is lost.

Optimistic Concurrency

Another solution is to use a versioning system within your document schema, where each update increments a version number.

db.products.findAndModify({
    query: {_id: ObjectId('507f191e810c19729de860ea'), version: 1},
    update: {
        $set: {price: 14.99},
        $inc: {version: 1}
    },
    new: true
});

Here, if two updates occur at the same time, one will fail because the version number would have been incremented by the first successful update.

Transactions

Starting with version 4.0, MongoDB added support for multi-document transactions. Concurrency control within transactions is also handled through document-level locking.

session.startTransaction();
db.products.updateOne(
  { _id: ObjectId('507f191e810c19729de860ea') },
  { $set: { price: 19.99 } }
);
db.orders.insertOne({
  productId: ObjectId('507f191e810c19729de860ea'),
  quantity: 2,
  pricePerUnit: 19.99
});
session.commitTransaction();

Using transactions allows multiple connected operations to be treated as a single unit of work, ensuring that no other operations can interfere with any documents involved in the transaction until it’s either committed or aborted.

Performance Considerations

With concurrent updates being managed more efficiently through locking mechanisms, MongoDB provides solid performance while ensuring data integrity. It’s important, though, to architect your update patterns thoughtfully to avoid bottlenecks, especially if high concurrency is a requirement in your system.

Indexed Updates

To improve performance in highly concurrent environments, ensure that update operations utilize indexes wherever possible.

db.products.createIndex({stock: 1}); // Creating an index on stock field

Proper indexing helps MongoDB to locate the document to be updated quickly, thus reducing the time locks are held for each update operation.

Isolation Levels and Durability

The concepts of Isolation levels and durability are also pertinent when discussing concurrent updates in MongoDB. MongoDB’s approach guarantees that reads and writes have an isolation level that is conceptually similar to a ‘Read Committed’ level in SQL databases.

Writes in MongoDB are durable upon acknowledgment (depending on the write concern level specified). However, without the use of transactions or versioning as described earlier, simple reads and writes are not isolated from other concurrent operations.

See also: Understanding Transactions in MongoDB (with examples).

Conclusion

In the event two clients attempt to update the same document simultaneously in MongoDB, the locking mechanism will ensure data integrity by granting access to the document on a first-come, first-served basis. Through the use of atomic operators, versioning, and transactions, applications can safely handle concurrent updates, reducing the risk of data conflicts.