Skip to content

Commit 93508fc

Browse files
DOCSP-7387 Lock free reads
1 parent 0ab8a26 commit 93508fc

File tree

4 files changed

+128
-85
lines changed

4 files changed

+128
-85
lines changed

source/faq/concurrency.txt

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ FAQ: Concurrency
1010
:depth: 1
1111
:class: twocols
1212

13-
MongoDB allows multiple clients to read and write the same data.
14-
In order to ensure consistency, it uses locking and other
15-
:term:`concurrency control` measures to prevent multiple clients
16-
from modifying the same piece of data simultaneously.
17-
Together, these mechanisms guarantee that all writes to a single
18-
document occur either in full or not at all and that clients never
19-
see an inconsistent view of the data.
13+
MongoDB allows multiple clients to read and write the same data. To
14+
ensure consistency, MongoDB uses locking and :term:`concurrency control`
15+
to prevent clients from modifying the same data simultaneously. Writes
16+
to a single document occur either in full or not at all, and clients
17+
always see consistent data.
2018

2119
.. _faq-dev-concurrency:
2220
.. _faq-concurrency-locking:
@@ -29,24 +27,24 @@ What type of locking does MongoDB use?
2927
In addition to a shared (S) locking mode for reads and an exclusive
3028
(X) locking mode for write operations, intent shared (IS) and intent
3129
exclusive (IX) modes indicate an intent to read or write a resource
32-
using a finer granularity lock. When locking at a certain granularity,
30+
using a finer granularity lock. When locking at a certain granularity,
3331
all higher levels are locked using an :term:`intent lock`.
3432

3533
For example, when locking a collection for writing (using mode X),
3634
both the corresponding database lock and the global lock must be
3735
locked in intent exclusive (IX) mode. A single database can
3836
simultaneously be locked in IS and IX mode, but an exclusive (X)
3937
lock cannot coexist with any other modes, and a shared (S) lock can
40-
only coexists with intent shared (IS) locks.
38+
only coexist with intent shared (IS) locks.
4139

4240
.. |rarr| unicode:: 0x2192 .. right arrow
4341

44-
Locks are fair, with reads and writes being queued in order. However,
45-
to optimize throughput, when one request is granted, all other
46-
compatible requests will be granted at the same time, potentially
47-
releasing them before a conflicting request. For example, consider
48-
a case in which an X lock was just released, and in which the
49-
conflict queue contains the following items:
42+
Locks are fair, with lock requests for reads and writes queued in order.
43+
However, to optimize throughput, when one lock request is granted, all
44+
other compatible lock requests are granted at the same time, potentially
45+
releasing the locks before a conflicting lock request is performed. For
46+
example, consider a situation where an X lock was just released and the
47+
conflict queue contains these locks:
5048

5149
IS |rarr| IS |rarr| X |rarr| X |rarr| S |rarr| IS
5250

@@ -75,8 +73,8 @@ How granular are locks in MongoDB?
7573
How do I see the status of locks on my :binary:`~bin.mongod` instances?
7674
-----------------------------------------------------------------------
7775

78-
For reporting on lock utilization information on locks, use any of the
79-
following methods:
76+
For reporting on lock utilization information on locks, use any of these
77+
methods:
8078

8179
- :method:`db.serverStatus()`,
8280
- :method:`db.currentOp()`,
@@ -110,7 +108,7 @@ Does a read or write operation ever yield the lock?
110108
In some situations, read and write operations can yield their locks.
111109

112110
Long running read and write operations, such as queries, updates,
113-
and deletes, yield under many conditions. MongoDB operations can
111+
and deletes, yield locks under many conditions. MongoDB operations can
114112
also yield locks between individual document modifications in write
115113
operations that affect multiple documents like
116114
:method:`~db.collection.update()` with the ``multi`` parameter.
@@ -209,15 +207,16 @@ use for document level locking storage engines:
209207
Which administrative commands lock the database?
210208
------------------------------------------------
211209

212-
Certain administrative commands can exclusively lock the database for
213-
extended periods of time. In some deployments, for large databases,
214-
you may consider taking the :binary:`~bin.mongod` instance offline so that
215-
clients are not affected. For example, if a :binary:`~bin.mongod` is part
216-
of a :term:`replica set`, take the :binary:`~bin.mongod` offline and let
217-
other members of the set service load while maintenance is in progress.
210+
Some administrative commands can exclusively lock the database for
211+
extended time periods. For large database deployments, you may consider
212+
taking the :binary:`~bin.mongod` instance offline so that clients are
213+
not affected. For example, if a :binary:`~bin.mongod` is part of a
214+
:term:`replica set`, take the :binary:`~bin.mongod` offline and let
215+
other members of the replica set process requests while maintenance is
216+
performed.
218217

219-
The following administrative operations require an exclusive
220-
lock at the database level for extended periods:
218+
These administrative operations require an exclusive lock at the
219+
database level for extended periods:
221220

222221
.. list-table::
223222
:header-rows: 1
@@ -261,7 +260,7 @@ lock at the database level for extended periods:
261260
exclusive (W) lock when renaming a collection across
262261
databases and blocks other operations until it finishes.
263262

264-
The following administrative operations lock the database but only hold
263+
These administrative operations lock the database but only hold
265264
the lock for a very short time:
266265

267266
.. list-table::
@@ -289,8 +288,8 @@ Which administrative commands lock a collection?
289288

290289
.. versionchanged:: 4.2
291290

292-
The following administrative operations require an exclusive lock at
293-
the collection level:
291+
These administrative operations require an exclusive lock at the
292+
collection level:
294293

295294
.. list-table::
296295
:header-rows: 1
@@ -375,8 +374,8 @@ until the operation completed.
375374
Does a MongoDB operation ever lock more than one database?
376375
----------------------------------------------------------
377376

378-
The following MongoDB operations may obtain and hold a lock on more
379-
than one database:
377+
These MongoDB operations may obtain and hold a lock on more than one
378+
database:
380379

381380
.. list-table::
382381
:header-rows: 1
@@ -429,10 +428,9 @@ How does sharding affect concurrency?
429428
-------------------------------------
430429

431430
:term:`Sharding <sharding>` improves concurrency by distributing
432-
collections over multiple :binary:`~bin.mongod` instances, allowing shard
433-
servers (i.e. :binary:`~bin.mongos` processes) to perform any number of
434-
operations concurrently to the various downstream :binary:`~bin.mongod`
435-
instances.
431+
collections over multiple :binary:`~bin.mongod` instances, allowing
432+
shard servers (specifically, :binary:`~bin.mongos` processes) to run
433+
concurrently with the downstream :binary:`~bin.mongod` instances.
436434

437435
.. include:: /includes/extracts/lock-sharding.rst
438436

@@ -447,7 +445,7 @@ which is a special collection in the ``local`` database. Therefore,
447445
MongoDB must lock both the collection's database and the ``local``
448446
database. The :binary:`~bin.mongod` must lock both databases at the same
449447
time to keep the database consistent and ensure that write operations,
450-
even with replication, are "all-or-nothing" operations.
448+
even with replication, are all or nothing operations.
451449

452450
.. include:: /includes/extracts/lock-replica-set-primary.rst
453451

@@ -483,6 +481,18 @@ Depending on the read concern, clients can see the results of writes
483481
before the writes are :term:`durable`. To control whether the data read
484482
may be rolled back or not, clients can use the ``readConcern`` option.
485483

484+
.. _faq-concurrency-lock-free-read-operations:
485+
486+
What are lock-free read operations?
487+
-----------------------------------
488+
489+
.. versionadded:: 5.0
490+
491+
A lock-free read operation runs immediately: it is not blocked when
492+
another operation has an exclusive (X) write lock on the collection.
493+
494+
.. include:: /includes/lock-free-commands.rst
495+
486496
For information, see:
487497

488498
- :doc:`/core/read-isolation-consistency-recency`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Starting in MongoDB 5.0, the following read operations are not blocked
2+
when another operation holds an exclusive (X) write lock on the
3+
collection:
4+
5+
- :dbcommand:`find`
6+
7+
- :dbcommand:`count`
8+
9+
- :dbcommand:`distinct`
10+
11+
- :dbcommand:`aggregate`
12+
13+
- :dbcommand:`mapReduce`
14+
15+
- :dbcommand:`listCollections`
16+
17+
- :dbcommand:`listIndexes`
18+
19+
When writing to a collection, :dbcommand:`mapReduce` and
20+
:dbcommand:`aggregate` hold an intent exclusive (IX) lock. Therefore, if
21+
an exclusive X lock is already held on a collection,
22+
:dbcommand:`mapReduce` and :dbcommand:`aggregate` write operations are
23+
blocked.

0 commit comments

Comments
 (0)