Back to blog

When the network splits, what does your database give up?

Thắng Đoàn
Thắng Đoàn

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

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.