Notes
The problem
The setup from episode twenty. You write to the leader; the change is copied to the replicas, but not at the same instant. Replica one has it, replica two is two seconds behind. A read could land on any of them. The consistency model is the rule for what that read may return.
- one write, several copies
- (episode 20: replication)
- copies update at different times
- a read can hit any copy
- question: which values
- is a read allowed to see?
Strong
The strongest promise: linearizability, usually just called strong consistency. The system behaves as if there were exactly one copy. Once a write is confirmed, every later read, by anyone, sees it. You get it by reading from the leader or a quorum, and you pay with latency and less availability during failures.
- linearizable: as if there
- were exactly one copy
- once a write is confirmed,
- every later read sees it,
- from anyone, anywhere
- cost: coordination
- (leader reads, quorums,
- consensus) = latency
Eventual
The weakest useful promise: eventual consistency. If writes stop, all copies will converge to the same value. That's it. No promise about when, and no promise about the order you'll see things in. It's cheap and always available, which is why DNS, CDN caches and Cassandra at consistency level one work this way.
- if writes stop, all copies
- converge to the same value
- no promise about WHEN
- and no promise about order
- cheap and always available
- DNS, CDN caches, Cassandra
- at ONE, DynamoDB default reads
In between
Most apps live in between. Strong for everything is too slow; pure eventual produces bugs users notice. So there are session guarantees: promises about what one user sees, which are much cheaper than strong consistency. Three matter in interviews: read your own writes, monotonic reads, and consistent prefix.
- strong: slow but simple
- eventual: fast but surprising
- session guarantees fix the
- surprises users actually notice:
- • read your own writes
- • monotonic reads
- • consistent prefix
Read your writes
Read your own writes. The bug: you post a comment, reload, and it's gone, because the reload hit a lagging replica. The fix: after a user writes, route their reads to the leader for a few seconds, or have the client send its last write position and only use a replica that has caught up. Other users may still see it a moment later, and that's fine.
- bug: post a comment, reload,
- it is missing (lagging replica)
- fix: after a write, read from
- the leader for a few seconds,
- or send your last-write
- position and wait for a replica
- that has caught up
- others may still see it later
Monotonic reads
Monotonic reads. The bug: you see five comments, refresh, and now there are three, because the second read went to an older replica. Time went backwards. The fix: pin each user to one replica, for example by hashing their user id, or remember the newest version they've seen and never serve them anything older.
- bug: you see 5 comments,
- refresh, now there are 3
- (second read hit an older copy)
- fix: pin each user to one
- replica (hash the user id),
- or remember the newest version
- you have seen and never accept
- an older one
Consistent prefix
Consistent prefix. The bug: in a chat, you see the reply 'Noon' before the question 'What time is lunch?', because the two messages were stored on different partitions that lag differently. The fix: keep related writes in order, by putting a conversation on one partition or by tracking which write caused which. Causal consistency generalises this.
- bug: you see the answer
- before the question
- A: "What time is lunch?"
- B: "Noon."
- a reader sees B then A
- because they were stored on
- different partitions
- fix: keep causally related
- writes in order (same
- partition, or causal ordering)
The ladder
Put together, it's a ladder. Strong at the top, then causal, then the session guarantees, then eventual at the bottom. Every step up removes a class of surprises and adds coordination, which means latency and less availability during a partition. That's CAP and PACELC from episode twenty-five, seen from the reader's side.
- strong (linearizable)
- ↓ cheaper, more available
- causal
- ↓
- session guarantees
- (read-your-writes, monotonic,
- consistent prefix)
- ↓
- eventual
Decision
Choose per feature. Balances, stock counts, unique usernames and locks need strong consistency. Your own profile and posts need read-your-writes. Threads and chat need consistent prefix. Like counts, feeds and search results are fine eventually. One product will use several of these at once.
- money, stock, unique usernames,
- locks → strong
- your own profile, your own
- posts → read-your-writes
- comment threads, chat
- → consistent prefix / causal
- like counts, feeds, search
- → eventual
Say it like this
In the interview, don't just say 'eventually consistent'. Name the model for each feature, the bug it prevents, the mechanism that provides it, like leader reads, quorums, sticky replicas or version tokens, and what it costs. That shows you understand the trade, not just the vocabulary.
- "What consistency do you need?"
- 1. name the model per feature
- 2. say what bug it prevents
- 3. say how: leader reads,
- quorum, sticky replicas,
- version tokens
- 4. say what it costs