Make the right trade-off / CHAPTER 43
Strong versus eventual consistency
Explain which saved version a read may return, then show the coordination and failure behavior each promise requires.
Builds on ACID and BASE database properties, Functional and non-functional requirements.
- Define strong consistency using real-time operation order.
- Distinguish eventual convergence from bounded staleness.
- Explain partitions, sessions, and freshness-sensitive reads.
01What may a read observe?
A user saves a new profile photo and receives success. Someone then reads that photo. What may the API return?
- Linearizable read/write behavior: the later-starting read cannot return the earlier photo unless another write changed it again. Operations behave as though they occurred at single instants in one order respecting completed-before-started timing. This is this chapter's strong consistency for one object.
- Eventual consistency: a replica may return an older photo while updates are still spreading. If new writes stop and delivery/repair assumptions hold, copies eventually agree.
Eventual does not mean “within five seconds,” “your session immediately sees its own edit,” or “all writes have one global order.” Specify those extra promises and their mechanisms separately.
A transaction changing several objects asks another question: whether concurrent transactions behave like a valid one-at-a-time order. That is serializability; ordering one photo alone does not automatically protect a multi-record purchase.
02Coordination and failure determine the cost
Use increasing photo versions to make the read promise observable. Version 8 finishes saving at time 10; a read begins at time 11 with no later write.
| Read promise | What must the service establish? | What may still fail? |
|---|---|---|
| Linearizable read | Return the value from a valid current ordered history, not old version 7. | Required coordination may be slow or unavailable. |
| Read your writes | This session's chosen source has applied at least its saved version 8. | Other sessions may still see older data. |
| Eventual read | Old observations are permitted while copies converge. | No fixed delay or session freshness is implied. |
| Bounded staleness | Prove the stated maximum age or version delay. | Reject or use a fresher source when that bound cannot be established. |
For a session that must see its own edit:
- Return version 8 with the save result.
- Carry required version 8 on the next session read.
- Choose a source known to have applied at least 8, or wait/fail under the documented policy.
A healthy nearby replica is not evidence that it has applied 8. Receiving update bytes is not the same as exposing the change to queries.
Another session guarantee, monotonic reads, prevents the session moving backward between versions it has already seen; it still need not see every latest global write. During a network partition, isolated sides cannot both guarantee successful service for the same data and linearizable operations. State which requests wait, reject or explicitly accept weaker reads.
03Worked example: profile and ticket inventory
Follow profile version 7 → 8:
- The current profile-writing service commits 8 and returns success. One asynchronous replica still has 7.
- A browsing read chooses that replica. Returning 7 is valid only if the endpoint permits an old observation. It is not a successful linearizable read starting after version 8 completed.
- The editor asks to see their own change. Their request requires version 8. The service selects a sufficiently updated source, waits for one or returns the documented failure.
- Another user may still see 7. Read-your-writes for the editor does not force every other session onto the same source.
- The product adds a five-second maximum age. One API worker can start a timer when it requests a linearizable database read. That returned value was current at some point after the timer began, so the worker reuses it only until five seconds from that start. A four-second validation leaves at most one second of reuse. If the timer expires, validate again or reject. This example uses one clock; sharing timestamps across machines also needs a bound on their clock difference. A response from an old replica does not perform this validation.
Now replace the photo with one remaining seat. Two disconnected sellers each hold an old “available” value. If both independently confirm, later agreement cannot make the original no-double-sale promise true.
- Use one coordinated inventory operation that grants the seat only if it is still free, or allocate exclusive seats to sellers in advance.
- If a seller cannot establish its right to this seat, some confirmations must wait or fail.
- Do not use a later timestamp winner to justify two earlier promises. Last-write-wins can make copies agree while discarding one meaningful change; convergence is not preservation of business intent.
Strong versus eventual consistency — decision flow
Follow the numbered arrows
- Profile writer → Current profile storesave version 8
- Current profile store → Profile replicacopy the update, possibly with delay
- Current-value read → Current profile storeread after the save of version 8 succeeds
- Old-value-permitted read → Profile replicaread under the permitted old-data rule
Trace the diagram in words
- Save photo version 8 and state what a successful response promises.
- For the editor’s next read, compare the source’s applied version with required version 8. Linearizable reads additionally need the database’s current-primary check.
- Return an older replica value only when that read explicitly permits it.
04Choose guarantees per action
Choose what each action may observe:
- Profile browsing: a little old data may be acceptable, while the editor's session sees its own write.
- Search stock hints: an approximate count may help discovery; purchase confirmation still requires a valid stock allocation.
- Strict freshness: identify where success is acknowledged, which source reads use and how required progress is proven.
- Network failures: say which operations wait or reject instead of silently weakening their promises.
- Repair: explain how delayed or conflicting copies catch up under the chosen data rules.
Stronger guarantees can add communication or reduce availability, but no universal delay penalty applies to every topology and protocol. In an interview, draw a completed write followed by a read, an overlapping write and a disconnected replica. Show which specific old response is permitted or rejected.
DRAW. CALCULATE. EXPLAIN.
Design two stock reads
20 minutes · guided practice
A browsing projection says one item remains. The inventory service just confirmed a purchase of that last unit, but a replica has not caught up. Define browse and confirmation behavior, a read-your-writes path for the buyer, and what happens during a partition.
Compare the worked solution
- Browsing can show approximate stock only under its declared old-data policy. Labeling it approximate does not reserve the item.
- Confirmation asks the inventory service to conditionally allocate current stock, using one request identity across retries. A stale search hint cannot grant a second purchase.
- The successful buyer retains the result/version and reads a source known to include that purchase when read-your-writes is required.
- During a partition, confirmation waits or fails if the service cannot establish the required allocation right. Permitted browsing may continue.
- A maximum-age promise needs measured freshness evidence and fallback or rejection, not merely eventual convergence.
The checkboxes record your own assessment. The worked solution gives you a way to check the reasoning.
RETRIEVE BEFORE YOU REVEAL
Keep the important ideas.
Say the answer aloud, then turn the card over.
CARD 01What does a linearizable read require?
- A write finishes successfully.
- A later read starts.
- Without another intervening change, that read cannot return the old value.
Overlapping operations must still fit one valid ordered history.
CARD 02What does eventual consistency not promise?
- Copies need not catch up within five seconds.
- Your next read need not immediately show your write.
- Stronger session or age limits need explicit checks and failure behavior.
CARD 03How should strict seat sales handle disconnection?
- Establish the seller’s exclusive reservation right.
- If that cannot be proven, wait or reject.
- Later agreement cannot undo two earlier confirmations.
Permitted old browsing data does not grant a seat.
CHECK THE REASONING
Can you explain the choice?
Answer every question, then finish for an immediate score and an explanation for each answer. Aim for 80%, then revisit any gaps.
CLOSE THE LOOP
Explain it without the page.
Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.