When the network splits, what does your database give up?
FotoFlow has 100,000 users, a copy of its database in Europe and another in the US, and one print left of a popular photo. This morning an undersea cable was cut: both regions and their users are healthy, but the regions cannot talk. This is a network partition, and it is not rare: cables, routers, misconfigurations and overloaded links all cause them.
Two options, no third
A US user wants the last print. The US copy cannot check with Europe, so it must choose.
Option 1: refuse what cannot be confirmed. Europe holds the primary copy and keeps serving; the US rejects writes until the link is back. Nobody sees wrong data, but US users get errors until the link is back. Choose this where wrong answers cost money: payments, inventory, sales.
Option 2: answer with what you have. Both sides keep accepting writes: Ana in Paris buys the last print, Ben in Boston buys the last print. Everyone gets an answer, and when the link returns there are two sales of one print. Choose this where brief disagreement is fine: likes, view counts, carts, feeds.
During a partition, a replicated system must give up either consistency or availability: refuse what cannot be confirmed, or answer and risk being wrong. That is the CAP theorem. You do not "pick two of three": partitions will happen, so the real choice is what to do when they do. CAP's consistency is linearizability, not the C in ACID.
Healing, and whose clock
The cable is repaired and the copies exchange what they missed. Left alone they converge: eventual consistency. But someone must decide what to do with the conflict. Options: last write wins, or the app merges, keeps both sales, refunds one, apologizes.
Last write wins loses data quietly; make sure that is acceptable. It is the default in many databases, and it needs to know which write was last. Each server stamps writes with its own clock, and clocks drift. The US server runs 3 seconds fast: a customer saves a delivery address, then corrects it two seconds later through the EU copy. The correction is newer but its timestamp is smaller, so sync keeps the old address and throws the fix away. Never order events across machines by wall-clock time: use version numbers or logical clocks that count events instead of reading the time.
Quorums: tune it per request
Many databases let you choose per request. With N = 3 copies, a write waits for W copies to confirm and a read asks R copies, using the newest answer. If W + R is greater than N, every read overlaps at least one copy that saw the latest write. W = 1, R = 1 is fast, available, possibly stale. W = 3 is consistent but fragile. Cassandra, Riak and ScyllaDB expose these knobs per request.
FotoFlow applies it split: a like is confirmed by one copy and spreads later (W = 1, R = 1), because nobody minds if a count is briefly off. Recording a payment uses W = 2, R = 2: slower, and it fails if two copies are unreachable, rather than risk anyone reading a stale "unpaid". You do not pick consistency once for the whole system.
Consistency is a spectrum
Between linearizable and eventual sit useful promises: read-your-writes (you always see your own changes), monotonic reads (time never goes backwards for you), causal consistency (replies never appear before the message they answer). Weaker promises are cheaper and more available. Pick the weakest consistency each operation can tolerate. Many "eventual" systems offer read-your-writes per session, which is enough for most apps.
What this does not solve
Choosing per request still needs the copies to agree on one truth when it matters, like who the leader is. A group of machines agreeing, even when some are unreachable, is consensus, the next post.
Which of your data could answer "probably" for an hour, and which must answer "exactly" or not at all?
If your clocks drifted 3 seconds today, which correction would your database quietly throw away?
The network will break. Decide now, not then.
Next in the series: Five nodes disagree about the truth: who decides?
Sources
Wikipedia: CAP theorem
Amazon Dynamo (All Things Distributed)
Designing Data-Intensive Applications
Recommended for you
- Craft & Skills
One server, 10 million users: how does a photo app survive that?
A 40-post series that follows FotoFlow, a photo app, from one server and 100 users to 10 million users with direct messages. One problem, one fix, one honest cost at a time.
- Craft & Skills
Ten million users want direct messages: could you design the chat system?
The final design: 20 million open sockets and a promise that every message arrives, in order, exactly once. Dumb gateways, a connection registry, store-first durability and sync by cursor.
- Craft & Skills
Five million home feeds: how do you build the page everyone opens?
The home feed must load in 200 ms from two hundred followed accounts. Fan-out on read melts at 4.6 million lookups a second; fan-out on write melts on celebrities; the hybrid survives both.
- Craft & Skills
FotoFlow wants short share links: could you design one today?
The first full design walkthrough: turn a one-line brief into numbers, the simplest system, then fix codes, caching, storage and abuse one bottleneck at a time. The method is the point.
Enjoyed this article?
Subscribe for new articles. No spam. Unsubscribe anytime.
Typed live, never pasted.