Master distributed system consistency with detailed explanations and simple analogies
Consistency models define the rules about when and how updates to a distributed system become visible to different parts of the system. Think of consistency models as "rules of engagement" for how multiple processes can interact with shared data in a distributed system.
A distributed system has a logical state that changes over time. For example:
0
, 5
, 42
{user: "Alice", score: 100}
A process is a single-threaded program that performs operations. Think of it as one person using the system at a time.
An operation changes the system from one state to another:
read(x)
→ returns current value of xwrite(x, 5)
→ sets x to 5increment(counter)
→ adds 1 to counterOperations have:
The strongest consistency model that combines serializability (for transactions) with linearizability (for real-time ordering).
Like a high-security bank vault where:
In a banking transfer scenario where $100 is moved from Account A to Account B, strict serializable consistency ensures that any concurrent read operation sees either the complete before-state or the complete after-state, never a partial state that would violate the transfer invariant.
Every operation appears to take effect atomically at some point between its start and completion, respecting real-time ordering.
A single-window bank teller serving customers one at a time. No matter how many people are in line, each transaction happens completely before the next one starts, and everyone sees the same account balance.
If Process A writes a value and completes before Process B starts reading, linearizable consistency guarantees that Process B must see the value written by Process A, maintaining real-time ordering.
All operations appear to execute in some sequential order, and each process sees operations in the same order, but real-time ordering is not required.
Like a group chat where everyone receives messages in the same order, but the order might not match exactly when messages were sent due to network delays.
In sequential consistency, if Process A writes x=1 and then Process B writes x=2, all nodes might see either order (x=1 then x=2, or x=2 then x=1) as long as all nodes see the same order. Real-time ordering is not required.
Two transactions T1 and T2 start simultaneously and see the same snapshot of accounts A and B. T1 updates account A while T2 updates account B. Both transactions can commit successfully since they don't conflict, resulting in both updates being applied to the final state.
If Process A writes to x and then writes to y (causally related operations), all nodes must see x's update before y's update. However, if Process B concurrently writes to z, that operation can appear at any point in the sequence since it's not causally related to A's operations.
Process A performs a sequence of writes to variable x, while Process B writes to variable y. PRAM consistency ensures that all nodes see A's writes in the order A issued them, and B's writes in the order B issued them. However, the interleaving of A's and B's operations can vary across different nodes.
If a process reads x=5 at time T1, then later reads at T2, T3, T4 will return x=5, x=7, x=7 (never a smaller value like x=3). The process can only see the same value or newer values, never going backwards.
If a process writes x=10, then immediately reads x, it must return 10 (or a newer value if another process has updated it). The process never experiences amnesia about its own writes.
When we say model A "implies" model B, it means every history that satisfies A also satisfies B. A is "stronger" than B.
The stronger the consistency model, the less available the system can be during network partitions.
Consistency models form a hierarchy from strongest (Strict Serializable) to weakest (Eventual Consistency). The choice of consistency model involves trade-offs:
Understanding these models helps you choose the right consistency level for your application's needs, balancing correctness requirements with performance and availability constraints.