Document- and collection-level locking is mentioned throughout this chapter and also in several other chapters in this book. It is important to understand how locking works and why it is important.
Database systems use the concept of locks to achieve ACID (atomicity, consistency, isolation, durability) properties. When there are multiple read or write requests coming in in parallel, we need to lock our data such that all readers and writers have consistent and predictable results.
MongoDB uses multi-granularity locking. The available granularity levels in descending order are as follows:
- Global
- Database
- Collection
- Document
The locks MongoDB and other databases use are the following in order of granularity:
- IS: Intent shared
- IX: Intent exclusive
- S: Shared
- X: Exclusive
If we use locking at a granularity level with shared (S) or exclusive (X), then all higher...