MongoDB Locks

MongoDB Locks – In this tutorial, we shall learn about how locking does happen in MongoDB, available granular locking levels and the different lock modes available.

There are four granular levels of locking. They are

  1. Global (MongoD instance) – All databases in it are affected by the lock.
  2. Database – Only the Database on which lock is applied is affected.
  3. Collection – Only the Collection on which lock is applied is affected.
  4. Document – Only the Document on which lock is applied is affected.

Following figure depicts the level of locks that MongoDB provides, and the ones it allows Storage Engine to implement.

MongoDB Locks

There are four types of locking modes :

  1. S – Shared – This lock mode allows concurrent readers to share the resource. Shared mode is used for read operations.
  2. X – Exclusive – This lock mode does not allow concurrent readers to share the resource. Exclusive mode is used for write operations.
  3. IS – Intent Shared – This lock mode indicates that the lock holder will read the resource at a granular level. For example, if IS is applied to a database, then it means that lock holder is willing to apply a Shared (S) lock on Collection or Document level.
  4. IX – Intent Exclusive – This lock mode indicates that the lock holder will modify the resource at a granular level. For example, if IX is applied to a database, then it means that lock holder is willing to apply a Exclusive (X) lock on Collection or Document level.

Example 1 – Locking a Collection for Writing

Collection is granular level (or the resource) for this example.

Whenever there is a write operation, Exclusive (X) lock mode must be applied on the resource.

As Collection is the resource in this scenario, Exclusive (X) lock mode must be applied on the Collection.

For all outer levels : Database, Global; Intent Exclusive (IX) must be applied.

When a write operation is triggered for a Collection, the control must do the following series of locks :

  1. The MongoD instance must be locked in Intent Exclusive (IX) lock mode, with an intent to apply write operation in a granular level (Database / Collection / Document).
  2. The Database must be locked in Intent Exclusive (IX) lock mode, with an intent to apply write operation in a granular level (Collection / Document).
  3. The Collection must be locked in Exclusive (X) lock mode, to apply write operation on the Collection.

Example 2 – Locking a Document for Reading

Document is granular level (or the resource) for this example.

Whenever there is a read operation, Shared (S) lock mode must be applied on the resource.

As Document is the resource in this scenario, Shared (S) lock mode must be applied on the Document.

For all outer levels : Database, Global, Collection; Intent Shared (IS) must be applied.

When a read operation is triggered for a Document, the control must do the following series of locks :

  1. The MongoD instance must be locked in Intent Shared (IS) lock mode, with an intent to apply the read operation in a granular level (Database / Collection / Document).
  2. The Database must be locked in Intent Shared (IS) lock mode, with an intent to apply read operation in a granular level (Collection / Document).
  3. The Collection must be locked in Intent Shared (IS) lock mode, with an intent to apply read operation in a granular level (Document).
  4. The Document must be locked in Shared (X) lock mode, to apply read operation on the Document.

Simultaneous Locks

A resource could simultaneously be locked in multiple locking modes.

For example, a Collection could be simultaneously locked in Shared (S) and Intent Shared(S) lock modes.

However some of the lock modes could not exist simultaneously with other lock modes on a resource.

Following table illustrates the combination of lock modes which are allowed and which are not.

MongoDB Locks

Conclusion

In this MongoDB TutorialMongoDB Locks, we have learnt about the different lock modes available in MongoDB, different resources on which lock could be applied, and the lock modes that could be applied simultaneously.