Consistency is a spectrum, not a switch
"Is the data correct?" has more than two answers. After a write succeeds, when do other readers see it? Immediately? After 5 ms? Eventually? Different models make different promises, and stronger promises cost more latency and availability. Choosing the model per feature is a core design skill.
The main models, strongest to weakest
- Strong (linearizability): every read sees the most recent write, as if there were one copy of the data. Requires coordination (single primary, consensus or quorum), so it is slower and less partition-tolerant. Needed for: balances, inventory, locks, "did my payment go through?"
- Sequential consistency: everyone sees operations in the same order, but maybe slightly delayed. Coordination systems (ZooKeeper/etcd) work here.
- Causal consistency: cause-before-effect is preserved: if a comment replies to a post, nobody sees the comment before the post. Independent events may appear in any order. A sweet spot for social features.
- Eventual consistency: if writes stop, all replicas converge to the same value... eventually. No time bound promised. DNS, Cassandra, DynamoDB defaults, S3 (pre-2020 behavior). Fine for: likes, view counts, profiles, most feeds.
Read-your-writes: the guarantee users actually feel
User edits their profile (write on the primary), page refreshes (read from a lagging replica), old data shows: "the app lost my edit!" The system was eventually consistent; the user experience was broken. Fixes:
- Read your own writes from the primary for N seconds after a write (track a "last write" timestamp or replication position).
- Monotonic reads: a user never sees data go backwards (pin a session to one replica, or remember the highest version seen).
- Session-level stickiness: keep critical reads on the primary for the whole session.
How replicas actually sync
Replication log: the primary records every write in an ordered log (WAL in Postgres, binlog in MySQL, oplog in MongoDB); replicas replay it. Lag = how far behind the replica's replay position is. Monitor it: lag spiking means replicas are falling behind, and reads get staler.
Quorums (Dynamo-style): N replicas, W write acks, R read acks. W + R > N makes reads see the latest write. W=1 gives fast writes with a risk window (write not yet replicated when the primary dies).
Worked example: choosing per feature
E-commerce, one page at a time:
- Stock count at checkout: strong (overselling costs money).
- Product description and images: eventual (stale for a minute, nobody notices; serve from cache/CDN).
- "3,421 people bought this": eventual, updated via a counter pipeline.
- Your own review after posting: read-your-writes (show it instantly, even if others see it seconds later).
Common interview traps
- "We'll use strong consistency everywhere": an availability and latency disaster, and unnecessary.
- Assuming eventual consistency is "broken": it is a deliberate tradeoff with a reconciliation story.
- Forgetting replication lag exists until a user reports stale data.
Interview Q&A
Q: Your system shows users stale balances for up to a second. Acceptable?
A: For a balance display, no: money reads go to the primary or a strongly consistent store. For "total views on a video", staleness is invisible: eventual is fine. The skill is splitting features, not picking one model.
Q: How do you implement read-your-writes on a replicated DB?
A: After a write, record the log position (LSN) returned by the primary; reads wait until a replica has replayed at least that position, else read from the primary. Postgres calls this causal consistency via LSN; MySQL via GTID waits.
Consistency ek spectrum hai, switch nahi
"Data sahi hai?" ke do se zyada jawab hain. Write successful hone ke baad, baaki readers usse kab dekhte hain? Turant? 5 ms baad? Kabhi na kabhi? Alag models alag promises dete hain, aur strong promises ka daam latency aur availability mein chukta hota hai. Har feature ke liye model choose karna core design skill hai.
Main models, strongest se weakest
- Strong (linearizability): har read ko sabse recent write dikhti hai, jaise data ki ek hi copy ho. Coordination chahiye (single primary, consensus ya quorum), isliye slow aur kam partition-tolerant. Kab chahiye: balances, inventory, locks, "payment hua ya nahi?"
- Sequential consistency: sabko operations same order mein dikhte hain, bas thoda delayed ho sakte hain. Coordination systems (ZooKeeper/etcd) yahin kaam karte hain.
- Causal consistency: cause-before-effect preserved: agar comment post ka reply hai, to kisi ko post se pehle comment nahi dikhta. Independent events kisi bhi order mein dikh sakte hain. Social features ke liye sweet spot.
- Eventual consistency: writes ruk jayen to saare replicas same value par mil lenge... eventually. Koi time bound nahi. DNS, Cassandra, DynamoDB defaults. Kab theek hai: likes, view counts, profiles, zyadatar feeds.
Read-your-writes: wo guarantee jo user ko feel hoti hai
User ne profile edit ki (primary par write), page refresh hua (lagging replica se read), purana data: "app ne mera edit kha gaya!" System eventually consistent tha; user experience toota. Fixes:
- Write ke baad N seconds tak apni reads primary se karo ("last write" timestamp ya replication position track karke).
- Monotonic reads: user ko kabhi data peeche jaata nahi dikhta (session ko ek replica par pin karo, ya highest seen version yaad rakho).
- Session-level stickiness: poore session mein critical reads primary par rakho.
Replicas sync kaise hote hain, asal mein
Replication log: primary har write ko ordered log mein likhta hai (Postgres mein WAL, MySQL mein binlog, MongoDB mein oplog); replicas use replay karte hain. Lag = replica primary se kitna peeche hai. Lag monitor karo: lag spike ka matlab reads stale ho rahi hain.
Quorums (Dynamo-style): N replicas, W write acks, R read acks. W + R > N ho to reads latest write dekhte hain. W=1 matlab fast writes par ek risk window (primary marne se pehle write replicate nahi hui to loss).
Worked example: feature-wise choice
E-commerce, ek-ek page:
- Checkout par stock count: strong (overselling = paise ka nuksaan).
- Product description aur images: eventual (ek minute stale, kisi ko farak nahi; cache/CDN se serve).
- "3,421 log ye kharid chuke": eventual, counter pipeline se update.
- Apni review post karne ke baad: read-your-writes (khud ko turant dikhe, baakiyon ko seconds baad).
Common interview traps
- "Sab jagah strong consistency": availability aur latency ki tabaahi, aur zaroori bhi nahi.
- Eventual consistency ko "broken" samajhna: ye ek deliberate tradeoff hai jiska reconciliation plan hota hai.
- Replication lag ka tab tak yaad na aana jab tak user stale data report na kare.
Interview Q&A
Q: System users ko ek second tak stale balance dikhata hai. Theek hai?
A: Balance display ke liye nahi: money reads primary ya strongly consistent store se. "Video par total views" ke liye staleness invisible hai: eventual theek hai. Skill features ko alag karna hai, ek model choose karna nahi.
Q: Replicated DB par read-your-writes kaise implement karoge?
A: Write ke baad primary se returned log position (LSN) record karo; reads tab tak wait karein jab tak replica wo position replay na kar le, warna primary se padho. Postgres mein ye LSN-based, MySQL mein GTID waits se hota hai.