Systems that shipped
Dynamo, Spanner, Kafka, etcd and MapReduce read as sets of choices, and what the wallet would get from building on each.
This chapter lets you read a well-known distributed system as a set of decisions, each made for a particular workload, and judge whether those decisions fit yours. We will read Dynamo, Spanner, Kafka and etcd, with a short look at MapReduce, and for each one ask what the wallet would get if we built on it.
Every system in this chapter uses ideas from the previous eight. None of them uses all of them, and the ones each leaves out are as informative as the ones it adopts. A useful way to read any such paper or design document is to answer five questions in the margin: what failures does it assume, where does order come from, what is a read promised, what happens during a partition, and what did it make cheap at whose expense?
Dynamo: never refuse a write
Amazon’s 2007 Dynamo paper describes the store behind the shopping cart and similar services. Its requirement is stated plainly: a customer must always be able to add an item to the cart, even when servers are failing or the network is partitioned. The paper also sets latency targets at the 99.9th percentile, a choice that pushes every design decision towards avoiding waits on slow nodes.
Data is spread across nodes using consistent hashing. Keys and nodes are both hashed onto a ring, and each key belongs to the next few nodes clockwise. The point is what happens when the cluster changes. With the simpler rule of “node = hash mod N”, growing from 10 nodes to 11 keeps a key in place only when its hash gives the same remainder for both, which is true for about 1 key in 11. The other 91 percent of the data moves. On a ring, the new node takes over only the arcs next to its positions, about one eleventh of the keys. Each physical node holds many positions on the ring, called virtual nodes, so that load and data spread evenly.
Writes go to N replicas with the quorum rules from the replication chapter. When some of the intended replicas are unreachable, Dynamo writes to other healthy nodes instead and records a hint saying where the data really belongs; those nodes hand it back when the intended replicas recover. This sloppy quorum keeps writes available and gives up the overlap guarantee while it is in use. Concurrent versions are tracked with vector clocks, and when a read finds several, it returns them all and the application merges them. For the cart, the merge is the union of items, which occasionally makes a deleted item reappear. Amazon judged that a fair price for never losing an addition.
Suppose we stored wallet balances in a Dynamo-style store. Asha has ₹1,000. During a partition, she pays a shop ₹800 from her phone, which reaches replicas on one side, and a scheduled bill payment of ₹800 runs against replicas on the other side. Both succeed. When the partition heals, the store presents two versions. What should the merge produce?
Show the worked answer
The two versions both say ₹200, each having seen one debit and not the other. A merge that picks either one loses ₹800. A merge that replays both debits gives −₹600, which is the true outcome and violates the rule that balances never go negative.
No merge can fix this, because the damage is not in the data. Both payments were approved and both recipients have been paid. The decision to allow each debit needed to see the other, and a design that never refuses a write cannot provide that.
The cart’s invariant, “do not lose items”, survives concurrent writes; it is preserved by taking the union. The wallet’s invariant, “do not overspend”, does not. That difference, not the scale or the technology, decides whether Dynamo’s design fits.
One point of confusion is worth clearing up. Amazon DynamoDB, the managed service, is a different design from the 2007 paper. Its 2022 USENIX paper describes partitions that each elect a leader using Multi-Paxos, with optional strongly consistent reads and transactions. The name carried over; much of the architecture did not.
Spanner: paying for real time
Google’s Spanner, described in a 2012 paper, makes the opposite choice. It offers transactions across shards and data centres, and a guarantee called external consistency: if transaction T1 commits before T2 starts, by real time anywhere in the world, T2 gets a larger timestamp and sees T1’s effects. The time chapter argued that ordinary clocks cannot support that. Spanner agrees, and changes the clocks.
Its TrueTime API returns not a time but an interval, earliest and latest, guaranteed to contain the true time. GPS receivers and atomic clocks in each data centre keep the interval narrow; the paper reports an uncertainty that varies between about 1 and 7 ms. When a transaction commits, Spanner picks a timestamp at least as late as the current interval’s latest bound, then waits until TrueTime’s earliest bound has passed that timestamp before making the writes visible. After the wait, the timestamp is certainly in the past, so any transaction that starts later, anywhere, gets a larger one. This commit wait is a few milliseconds on average, and it largely overlaps with the time spent replicating the commit.
Underneath, each shard is a Paxos group, and transactions across shards use two-phase commit between groups, the design from the last chapter that does not block on a single coordinator. Because timestamps match real order, a read-only transaction can pick a timestamp and read a consistent snapshot of every shard without taking any locks.
A worked example makes the commit wait concrete. A transfer is ready to commit, and TrueTime reports that the current time lies between 10:00:00.000 and 10:00:00.008, an uncertainty of 4 ms either side of the midpoint. Spanner assigns the commit timestamp 10:00:00.008, the latest possible current time, so no clock anywhere can yet have passed it. It then waits until TrueTime’s earliest bound is later than 10:00:00.008, which with the same uncertainty takes about 8 ms. Only then does it release locks and report success. Any transaction that starts after that moment, in any data centre, will see a TrueTime interval whose latest bound is after 10:00:00.008, and will be given a later timestamp. Meanwhile, the commit has been replicating through Paxos, which across data centres takes tens of milliseconds, so the 8 ms wait is usually hidden inside time the transaction was spending anyway. The cost of the guarantee scales with the clock uncertainty, which is why Google invested in making it small.
For the wallet, this is a close fit. Cross-shard transfers become real transactions with no pending states. A monthly statement becomes a snapshot read at one timestamp, consistent across every shard. The costs are commit latency, which includes Paxos replication across whatever distance the replicas span plus the commit wait, the hot-row ceiling from two-phase commit, and dependence on a managed service, since the clock hardware is not something most teams can build. CockroachDB offers a similar model on ordinary clocks by assuming a maximum clock offset and, when two timestamps are too close to order safely, restarting the read at a later timestamp.
Kafka: order by partition, durability by in-sync replicas
Kafka is the system the wallet’s outbox events most likely flow through. Each topic is split into partitions; each partition is an ordered log with one leader and several followers. Producers write to the leader; followers fetch from it. Order is per partition, which is why the broadcast chapter keyed events by account id.
Durability is controlled by the in-sync replica set: the followers that have kept up with the leader within a configured time. A producer that sets acks=all gets an acknowledgement only when every in-sync replica has the record. Combined with a replication factor of 3 and min.insync.replicas=2, a write is refused if fewer than two replicas are in sync, which is the replication chapter’s choice of refusing writes rather than risking their loss. If every in-sync replica is lost, Kafka can either wait for one to return or elect an out-of-date replica and lose records; the setting for the second option, unclean leader election, has been off by default for years, for the reasons in that chapter.
Kafka’s idempotent producer is the RPC chapter’s idempotency key in another form. Each producer gets an id, numbers its records per partition, and the broker discards a record whose sequence number it has already written. A producer retry after a lost acknowledgement no longer creates a duplicate. Kafka transactions extend this to writes across partitions together with the consumer’s position, which gives exactly-once processing as long as both input and output are Kafka topics. When a consumer’s effect is outside Kafka, such as Ravi’s push notification, duplicates are again the consumer’s problem, solved with event ids.
Deciding which replica leads each partition is consensus. Kafka originally delegated it to ZooKeeper and now uses its own Raft-based controller, KRaft; recent major versions have removed the ZooKeeper option. The simple-looking partition log rests on the equivalence between total order and consensus from the broadcast chapter.
etcd: a small Raft for everyone’s control plane
etcd is the coordination service from the last chapter, and the data store behind Kubernetes. It is a Raft group, usually of three or five members, holding a key-value store. Every change increments a single cluster-wide revision number. That number does two jobs the wallet needs. It is a fencing token, since the revision at which a server acquired shard 7 is larger than any earlier acquisition. And it makes watches reliable: a client can ask for every change since revision 81,200, so a watcher that disconnects and reconnects misses nothing.
Reads are linearizable by default, using the read-index check from the consensus chapter, and can be made serializable, answered locally by any member and possibly stale, when a client prefers speed. Leases attach a time limit to keys, which is how ownership expires. All of this is designed for small amounts of data that change modestly and are read often. etcd clusters get into trouble when they are used as a general database: large values, millions of keys, or thousands of clients each watching broad key ranges. Its documentation sets a default storage quota of a couple of gigabytes for this reason.
MapReduce: making failure cheap by making work repeatable
Google’s 2004 MapReduce paper is about computation, not storage, but its approach to failure applies directly to a job the wallet runs every night: reconciliation. Read every ledger entry, group by account, sum, and compare the result with each stored balance.
A MapReduce job expresses this as a map function that turns each ledger entry into an (account, amount) pair, and a reduce function that sums each account’s amounts. The framework splits the input into thousands of tasks across many machines. When a machine dies, its tasks simply run again elsewhere. When a machine is merely slow, the framework starts a backup copy of its remaining tasks near the end of the job and uses whichever finishes first. Both are safe for the same reason: the functions have no side effects and each task’s output becomes visible in one atomic step, by renaming a finished file. Running a task twice is harmless, so the framework never needs to know whether the first attempt succeeded. It is the first chapter’s ambiguity, removed by making the work idempotent.
What changed after these systems met production
Papers describe a system at one moment. The more instructive record is how designs changed afterwards, because those changes show which costs turned out larger than expected.
The clearest trend is a move towards a leader per partition, chosen by consensus. DynamoDB replaced the 2007 paper’s leaderless writes with a Multi-Paxos leader for each partition. Cassandra, which inherited Dynamo’s leaderless design, added lightweight transactions that run Paxos when a write needs a compare-and-set, such as “create this user name only if nobody has it”. MongoDB moved its replication to a protocol modelled on Raft. Kafka replaced its dependence on ZooKeeper with its own Raft-based controller.
The reasons are visible in the earlier chapters. Leaderless designs push conflict handling onto every application that reads the data, and merge logic is easy to get subtly wrong; the cart’s reappearing items were tolerable, most other data is less forgiving. Meanwhile, consensus implementations matured, and networks inside a region became reliable enough that the availability given up by having a leader is a few seconds of failover now and then. For most workloads within a region, that trade favours the leader: simpler reasoning for every reader, at the cost of short pauses during elections.
The trade still goes the other way in some places. Across regions, where round trips are tens to hundreds of milliseconds and links fail more often, waiting for a remote majority on every write is expensive, and many systems replicate asynchronously between regions and resolve conflicts afterwards. Data with a natural merge, such as counters, sets and collaborative documents, still suits merge-based replication well.
The second trend is the separation of control and data that the coordination chapter argued for. Kafka keeps metadata in a small Raft log and leaves the records themselves in partition logs. Kubernetes keeps cluster state in etcd and leaves application data elsewhere. Spanner places a consensus group per shard rather than one for the whole database. In each case, the expensive agreement is applied to the smallest thing that needs it.
Reading them side by side
| System | What it sells | Order comes from | In a partition | Fit for the wallet |
|---|---|---|---|---|
| Dynamo (2007) | Writes always accepted | Vector clocks; app merges | Both sides accept writes | Carts and preferences, not balances |
| Spanner | Transactions in real-time order, globally | TrueTime timestamps over Paxos logs | Minority side cannot write | Balances and cross-shard transfers |
| Kafka | Durable ordered logs at high throughput | Partition leader; controller by Raft | Refuses writes below min in-sync replicas | Balance-change events |
| etcd | Small, linearizable, watchable state | Raft log and revision numbers | Minority side cannot write | Shard ownership and fencing |
| MapReduce | Large batch jobs that survive failures | Not needed; tasks are independent | Rerun affected tasks | Nightly reconciliation |
A real wallet ends up combining several of these. Balances live in a store with a leader per shard and consensus underneath. Events flow through a partitioned log. Ownership and configuration live in a small coordination service. Batch jobs check the books overnight. Each component sells one guarantee, and the system design is in how they are joined: the outbox that ties a balance change to its event, the fencing token that ties ownership to storage, the idempotency key that ties a retry to its first attempt.
When a famous design is the wrong model
The common mistake with these systems is copying a design without its requirement. Dynamo-style replication is attractive because it never refuses writes, and wrong for a balance for the reason in the worked answer above. Spanner-style guarantees are attractive because they are strong, and unnecessary for a blog’s comment counts, where they would add latency and cost for nothing. A Kafka topic is attractive as a universal integration layer, and a poor fit for request-reply interactions where the caller is waiting for an answer.
The test is the one this chapter started with. Write down your answers to the five questions for your own data, then for the system you are considering, and look for the row where they disagree. That row is where the incident will come from.
Each of these systems also has a public history of bugs found after it shipped, many of them in exactly the failure paths described in this pathway. The final chapter, Breaking it on purpose, is about finding those bugs in our wallet before users do.
Consistent hashing, vector clocks, quorums and hinted handoff — the ancestor of a whole generation of stores.
What becomes possible when you treat clock uncertainty as a number you can bound rather than ignore.
Historically important and still the clearest statement of moving computation to data.