Understanding Consistency Patterns: An In-Depth Analysis
The Shared Paper Calendar
Alice and Bob share a paper calendar on the office wall.
π― System Scenario & Requirements
System Goal
Alice and Bob share a paper calendar on the office wall.
Actors & Components
- Application or API tier
- Core service under design
- Operator or SRE observer
Functional Requirements
- Alice and Bob share a paper calendar on the office wall.
- There's only one calendar β when Alice writes "Meeting 2pm Monday," Bob immediately sees it.
- That is strong consistency β one copy, always in sync.
Constraints & Assumptions
- In the presence of a network partition (P), you must choose: - CP: Stay consistent, stop accepting writes (etcd, ZooKeeper, HBase).
- - AP: Stay available, accept stale reads (Cassandra, DynamoDB, CouchDB).
β‘ Execution Stages & Decision Trace
Stage 1 β Consistency as a User Promise: What Your App Actually Sees
Consistency in distributed systems is ultimately a promise made to the user : "When you read data, how fresh is it guaranteed to be?" That promise has a direct impact on correctness and user trust.
If you transfer $500 and immediately check your balance on a different server, what should you see?
Deep Dive
Scenario context
Alice and Bob share a paper calendar on the office wall. There's only one calendar β when Alice writes "Meeting 2pm Monday," Bob immediately sees it. That is strong consistency β one copy, always in sync.
Now imagine Alice and Bob each have their own weekly planner. They sync them once a day. If Alice books 2pm Monday morning and Bob checks his planner at noon, he might not see the booking yet. **Eventual consiβ¦
This walkthrough focuses on The Shared Paper Calendar.
Before making a design choice, clarify:
- Alice and Bob share a paper calendar on the office wall.
- There's only one calendar β when Alice writes "Meeting 2pm Monday," Bob immediately sees it.
- That is strong consistency β one copy, always in sync.
Concepts in play: consistency-replication, api-gateway, system-design-hld-architectures, event-driven-architecture
Stage 1 β Consistency as a User Promise: What Your App Actually Sees
- Consistency in distributed systems is ultimately a promise made to the user : "When you read data, how fresh is it guaranteed to be?" That promise has a direct impact on correctness and user trust.
- If you transfer $500 and immediately check your balance on a different server, what should you see?
- A strongly consistent system guarantees you always see the result of your latest write.
- An eventually consistent system might show your pre-transfer balance for a brief window.
Observable outcome: An eventually consistent system might show your pre-transfer balance for a brief window.
Stage 2 β The Consistency Spectrum
- From strictest to most relaxed: Analogy-to-model mapping: - Alice and Bob on a single wall calendar β Linearizability.
- Two separate planners synced each morning β Eventual.
- π Consistency Level Transitions This state diagram places the four consistency models on a single spectrum from strongest (Linearizable) to weakest (Eventual), with each arrow representing one relaxed guarantee.
- Linearizable systems like etcd and Spanner guarantee that every read reflects the latest write globally.
stateDiagram-v2
direction LR
[*] --> Linearizable
Linearizable --> Sequential : relax real-time order
Sequential --> Causal : relax global order
Causal --> Eventual : relax all sync
note right of Linearizable
etcd, Spanner
always latest write
end note
note right of Sequential
Kafka per partition
same order all nodes
end note
note right of Causal
DynamoDB DAX
own writes visible
end note
note right of Eventual
Cassandra, DNS
fast but stale reads ok
end note
Observable outcome: Linearizable systems like etcd and Spanner guarantee that every read reflects the latest write globally.
Stage 3 β Quorum Reads and Writes: The Knob
- Distributed systems like Cassandra and Dynamo use quorums to tune consistency vs.
- availability: $$R + W N \Rightarrow \text{Strong Consistency}$$ Where: - N = total replicas (replication factor, e.g., 3) - W = nodes that must acknowledge a write before it's considered complete - R = nodes that must rβ¦
Observable outcome: availability: $$R + W N \Rightarrow \text{Strong Consistency}$$ Where: - N = total replicas (replication factor, e.g., 3) - W = nodes that must acknowledge a write before it's considered complete - R = nodes that must rβ¦
Stage 4 β Quorum Read/Write Flow: Visualizing the Math
- The diagram below shows a 3-replica cluster (N=3) with W=2 and R=2.
- The client writes to two nodes immediately; the third node lags behind.
- But when the client reads with R=2, it hits at least one node that has the new value β so the quorum returns the correct, up-to-date result.
- The read quorum (R=2) overlaps with the write quorum (W=2) by at least one node, so the latest write is always visible.
flowchart LR
Client -->|Write X=5| N1[Node 1 X=5]
Client -->|Write X=5| N2[Node 2 X=5]
N3[Node 3 X=old] -.->|Propagating...| Sync[Sync]
Client -->|Read| Q[Quorum Read W=2, R=2, N=3]
Q -->|Returns X=5| Client
Observable outcome: The read quorum (R=2) overlaps with the write quorum (W=2) by at least one node, so the latest write is always visible.
Stage 5 β Deep Dive: Conflict Resolution in Eventual Consistency
- When two clients write to different replicas simultaneously, a write conflict occurs.
- The system must decide which value wins.
Observable outcome: The system must decide which value wins.
Stage 6 β Real-World Applications: Consistency Models in Production
- Consistency isn't just a database-theory concept β every major distributed system picks a point on the spectrum and optimises around it.
- Here is how real systems map to the models covered above: Practical patterns you'll recognise: - DNS is the most widely-used eventually consistent system on earth.
- When you update a DNS record, propagation takes up to 48 hours.
- The internet tolerates this because serving a stale IP for a few minutes is vastly preferable to making DNS unavailable.
Observable outcome: The internet tolerates this because serving a stale IP for a few minutes is vastly preferable to making DNS unavailable.
End-to-end outcome
Alice and Bob share a paper calendar on the office wall.
Alternatives considered
Choosing Your Consistency Level: A Practical Decision Guide
The most common mistake teams make is accepting the database default without thinking through the consistency requirements of each operation.
Failure and degraded modes
Trade-offs & Failure Modes: CAP Theorem
- Detection: In the presence of a network partition (P), you must choose: - CP: Stay consistent, stop accepting writes (etcd, ZooKeeper, HBase).
- User or operator impact: In the presence of a network partition (P), you must choose: - CP: Stay consistent, stop accepting writes (etcd, ZooKeeper, HBase).
