Skip to content

DOCSP-46686: Causal consistency #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions source/write/transactions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ all data changes before they ever become visible.

In MongoDB, transactions run within logical **sessions**. A
session is a grouping of related read or write operations that you intend to run
sequentially. Sessions enable **causal consistency** for a
group of operations and allow you to run operations in an
sequentially. Sessions allow you to run operations in an
**ACID-compliant transaction**, which is a transaction that meets an expectation
of atomicity, consistency, isolation, and durability. MongoDB guarantees that the
data involved in your transaction operations remains consistent, even if the
Expand All @@ -47,6 +46,64 @@ creating a new client each time.
``ClientSession`` with a different ``MongoClient`` results in operation
errors.

Causal Consistency
~~~~~~~~~~~~~~~~~~

MongoDB enables **causal consistency** in client sessions.
The causal consistency model guarantees that operations within a session
run in a causal order. Clients observe results that are consistent
with the causal relationships, or the dependencies between
operations. For example, if you perform a series of operations where
one operation logically depends on the result of another, any subsequent
reads reflect the dependent relationship.

The following table describes the guarantees that causally
consistent sessions provide:

.. list-table::
:widths: 40 60
:header-rows: 1

* - Guarantee
- Description

* - Read your writes
- Read operations reflect the results of preceding write operations.

* - Monotonic reads
- Read operations do not return results that reflect an earlier data state than
a preceding read operation.

* - Monotonic writes
- If a write operation must precede other write operations, the driver
runs this write operation first.

For example, if you call ``insert_one()`` to insert a document, then call
``update_one()`` to modify the inserted document, the driver runs the
insert operation first.

* - Writes follow reads
- If a write operation must follow other read operations, the driver runs
the read operations first.

For example, if you call ``find()`` to retrieve a document, then call
``delete_one()`` to delete the retrieved document, the driver runs the find
operation first.

In a causally consistent session, MongoDB ensures a causal relationship between the
following operations:

- Read operations that have a ``MAJORITY`` read concern
- Write operations that have a ``MAJORITY`` write concern
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: since it looks like you set these to lowercase strings in PyMongo

Suggested change
- Read operations that have a ``MAJORITY`` read concern
- Write operations that have a ``MAJORITY`` write concern
- Read operations that have a ``"majority"`` read concern
- Write operations that have a ``"majority"`` write concern


.. tip::

To learn more about the concepts mentioned in this section, see the
following {+mdb-server+} manual entries:

- :manual:`Causal Consistency </core/read-isolation-consistency-recency/#causal-consistency>`
- :manual:`Causal Consistency and Read and Write Concerns </core/causal-consistency-read-write-concerns/>`

Sample Data
~~~~~~~~~~~

Expand Down
Loading