WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

MongoDB · Expert · question 61 of 100

What are the differences between the various MongoDB consistency models (strong, eventual, and session)?

📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

MongoDB supports three types of consistency models: Strong consistency, Eventual consistency and Session consistency.

**Strong Consistency (Default):** In MongoDB, strong consistency refers to the guarantee that a read operation always returns the most recently written data on the primary node. In other words, strong consistency ensures that a write operation is propagated to all nodes before a read operation is processed. When a write operation is performed, it is acknowledged immediately only after the data is replicated to a majority of nodes within the cluster. In other words, an acknowledged write operation guarantees that the write operation applied to a majority of nodes within the cluster. Thus, when a read operation is performed, MongoDB verifies the latest data across all nodes and returns it. Strong consistency provides intra-document consistency; this means that if two fields are updated in one document, the fields will be consistent across all reads.

An example of how strong consistency works is when a write operation 1 updates the value of a field F in a document. The write operation is an acknowledged write operation, indicating that the write operation applied to a majority of the cluster. After this, a read operation for the same document will always return the most recently written data with the updated value of field F.

**Eventual Consistency:** In MongoDB, eventual consistency refers to a consistency model that provides weak guarantees of the order in which write operations will be visible. Write operations are applied immediately to the node where the write operation occurred. However, this write operation is eventually propagated to all other nodes in the cluster asynchronously.

In other words, eventual consistency allows some delay in the replication of data between nodes where read operations can receive stale data. Distributed systems using eventual consistency can handle node disconnection or network disruptions to certain nodes without causing extended periods of system inaccessibility.

An example of how eventual consistency works is when write operations 1 and 2 are performed on a document, and the read operation occurs between these writes. The possible results can be read 1 (before write 1), read 2 (after write 1 but before write 2) or read 3 (after write 2).

**Session Consistency:** Session consistency is a MongoDB consistency model that provides a balance between strong and eventual consistency. This consistency model maintains consistency within the session between a client and a single node using read-your-writes guarantees. In other words, this consistency model guarantees that any read operation performed within a session sees the results of all previous write operations within the same session, providing stronger consistency guarantees than eventual consistency.

An example of how session consistency works is when a client application initiates a session to a node, write operations 1 and 2 are performed on the document, and the read operation occurs between these writes within the same session. The only possible result will be read 2 (after write 2). Any subsequent operations executed within the same session will read the updated value.

In summary, MongoDB offers three types of consistency models: strong, eventual, and session consistency. Strong consistency guarantees that read operations always return the most recently written data, while eventual consistency provides weaker guarantees but allows for some delay in replication. Session consistency provides a balance between strong and eventual consistency by maintaining consistency within a session between a client and a single node using read-your-writes guarantees.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MongoDB interview — then scores it.
📞 Practice MongoDB — free 15 min
📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

All 100 MongoDB questions · All topics