
After a profile update, the user refreshes and sees the old name. The write succeeded, but the read reached a lagging replica. “The database is consistent” is not specific enough: consistency is a contract about which order and visibility of operations readers are guaranteed to observe.
This consistency models guide compares strong, eventual, causal, read-your-writes, and monotonic-read behavior and shows how to choose a model per endpoint. Continue from CAP theorem, database replication, and isolation levels.
Table of Contents
Open Table of Contents
What Consistency Specifies
Consistency describes what values a read may observe relative to completed writes and other reads. It is different from database isolation, which describes what concurrent transactions can see while they execute. A system can use serializable transactions on one primary and still expose stale reads from a replica.
flowchart TD
A[Client writes version 8] --> B[Primary commits]
B --> C[Replica propagation]
C --> D[Replica version 8]
E[Client reads] --> F{Read target}
F -->|Primary or synchronized replica| G[Strong result]
F -->|Lagging replica| H[Stale result]
E --> I[Session token: version 8]
I --> J[Read-after-write routing]
Common Models
| Model | Guarantee | Typical use |
|---|---|---|
| Strong/linearizable | Reads reflect the latest completed write in a real-time order | Balances, ownership, uniqueness |
| Eventual | Replicas converge if writes stop; reads may be stale | Feeds, analytics, caches |
| Causal | Cause-and-effect operations are observed in order | Conversations, collaboration |
| Read-your-writes | A session sees its own successful writes | Profile and settings pages |
| Monotonic reads | A session never goes backward to an older value | Progress and status views |
These models form a spectrum, not mutually exclusive database products. A system can provide eventual global convergence with read-your-writes sessions by routing a user to a replica at least as fresh as the write token.
Session Guarantees
Read-your-writes can use a primary route, a session cookie containing a commit version, or a replica wait condition. Monotonic reads can use a minimum observed version in the session context. Causal consistency can propagate a vector, logical timestamp, or dependency token with the request.
The token must be bounded and trustworthy. Do not let clients forge a version that grants access to future data, and do not store sensitive business state in a browser token. If the required replica cannot satisfy the token before the deadline, return a clear stale or retryable response instead of silently violating the contract.
Choosing Per Operation
An inventory reservation needs strong reads and conditional writes; a recommendation list can be eventual; an order status page benefits from read-your-writes; and a chat timeline often needs causal ordering. Ask what a stale read causes, what latency is acceptable, and whether a user can repair an incorrect answer.
Document the model in the API contract. “Eventually consistent” should include a freshness target such as “95% of reads converge within two seconds” and a fallback. Without an age or session guarantee, clients cannot decide whether to show, refresh, or reconcile a result.
Implementation Techniques
Use primary reads, quorum reads, synchronous replication, version checks, sticky sessions, cache invalidation, or change streams depending on the model. Stronger guarantees often increase latency, reduce availability during partitions, or consume more coordination resources. Measure replica lag and stale-read rate, not only database CPU.
Interview Questions
1. Is eventual consistency the same as no consistency?
No. Eventual consistency promises convergence when updates stop, while weaker designs may provide no ordering or convergence guarantee. Define the convergence window, conflict policy, and session behavior explicitly.
2. How do you implement read-your-writes?
Route the session to the primary, wait for a replica to reach the write’s version, or carry a commit token that the read path honors. Each option trades latency, load, and availability. The guarantee must survive retries and failover.
3. What is causal consistency?
Causal consistency preserves the order of operations that could have influenced one another while allowing unrelated operations to be observed in different orders. It is useful for conversations and workflows where a reply must not appear before the message it replies to. It is weaker and often more available than global linearizability.
4. How is consistency different from isolation?
Consistency is the distributed visibility contract across reads and replicas. Isolation is the database transaction contract for concurrent operations. Serializable isolation on one node does not prevent a remote replica from serving a stale committed value.
5. How would you choose a model for checkout?
Use strong consistency for inventory ownership, payment state, and uniqueness decisions. Use eventual projections for analytics, recommendations, and notifications. Keep the user-visible order status read-your-writes so a successful submission does not immediately appear missing.
Conclusion
Consistency models let a system make freshness and ordering explicit instead of hiding replica lag behind a vague guarantee. Use strong guarantees where stale data can cause a business error, session guarantees where user experience needs continuity, and eventual or causal models where latency and availability matter more than immediate global agreement.
References
YouTube Videos
- “Consistency Models in Distributed Systems” - Distributed Systems Concepts [https://www.youtube.com/watch?v=DOFflggE_0Q]