Handling locks in MongoDB

Handling locks in MongoDB

Locking is a mechanism used to maintain concurrency in the databases. MongoDB uses multi-granularity locking in different levels and modes of locking to achieve this. In this article we are going to discuss about locks and how we can monitor them.

Locking levels:-

There are four different levels of locking in MongoDB.

Global: It is a MongoDB instance level lock where all the databases will be locked. 

Database: This is a database level lock in which the mentioned database will be locked. 

Collection: Here the locking is handled in the collection level 

Document: It is a document level locking where only that particular document will be locked.

Locking modes:-

Below are the various locking modes available.

Shared (R)

  • The resource will be shared with the concurrent readers.
  • This mode is used for the read operations

Exclusive (W)

  • The resource will not be available for the other concurrent readers.
  • This mode is used for the write operations

Intent Shared (r)

  • Intent locks are higher level locks acquired before lower level locks. 
  • It indicates that the lock holder will read the resource at a granular level.
  • If Intent Shared (r) lock is applied to a database, then it means that lock holder is willing to apply a Shared (S) lock on Collection or Document level.

Intent Exclusive (w)

  • It indicates that the lock holder will modify the resource at a granular level.
  • If Intent Exclusive (w) lock is applied to a database, then it means that lock holder is willing to apply an Exclusive (X) lock on Collection or Document level.

How to avoid locks?

When two operations are accessing the same resource locking happens and other processes will have to wait for the same resource. However we can reduce the lock wait by using the below method.

maxTransactionLockRequestTimeoutMillis        
No alt text provided for this image

This parameter the maximum amount of time in milliseconds that multi-document transactions should wait to acquire locks required by the operations in the transaction.

By default it will wait for 5 milliseconds, configuring an optimal value will enhance a better locking.

How to monitor the various locks?

We have various parameters that can be monitored periodically to prevent the locking.

Global locks:

db.serverStatus().globalLock        
No alt text provided for this image

totalTime: If this value is higher than the total database uptime, the database has been in a lock state for too long.

currentQueue: It gets increased when lot of requests are waiting for a lock to be released.

Database locks:

db.serverStatus().locks.Database        
No alt text provided for this image

acquireCount: Number of times the lock was acquired

acquireWaitCount: Number of times the locks.acquireCount encountered waits because of conflicting locks

timeAcquiringMicros: Cumulative wait time for lock acquisitions in microseconds

Collection locks:

db.serverStatus().locks.Collection        
No alt text provided for this image

Oplog locks:

db.serverStatus().locks.oplog        
No alt text provided for this image


I hope this article helps us to handle MongoDB locks in a better way

This is simple, neat & good. Thanks for creating this article.

Like
Reply

To view or add a comment, sign in

Others also viewed

Explore content categories