BOOK DEEP-READ · DDIA · CHAPTER 12

The Future of Data Systems

Designing Data-Intensive Applications · Ch 12 · Martin Kleppmann · 2017

中文 →

What is this chapter about?

When you change your shipping address on a shopping site, that one edit has to reach several different systems: the one behind the order page, the one behind the search box, the one support staff look at, the one that runs reports overnight. The first eleven chapters were all about doing one system well. In this last one, the author finally says the whole thing out loud: how should all these systems be joined together so they stop contradicting each other? This is not a summary — it is his own argument.

A strange thing, first

The usual approach is "change it everywhere": the program updates the main database, then goes and updates the search copy too. Sounds obvious, but it has a hidden flaw. When two people edit the same record at almost the same moment, the main database may see Ann first and Bob second, while search happens to see Bob first and Ann second. The two sides end up with different final answers — and neither will notice, let alone fix it.

The hard part is not writing the data. It is that nobody is in charge of the order: each system only sees the edits that reach it, and acts on whatever sequence it happened to observe. It is like a dozen departments each keeping their own ledger — every book looks fine alone, but they never reconcile. And the more systems you have, the more wires between them: ten systems wired pairwise is up to ninety connections.

The chapter's core idea: queue first, then copy

The author's answer is almost disappointingly plain: keep one single ledger, make every change line up and get written into it first, and treat every other system as nothing more than a copy of that ledger. The order is fixed once, in the ledger; nobody gets to invent their own. Downstream systems copy from that one sequence — some faster, some slower, but what they end up with is guaranteed to match.

The same move solves two chronic headaches. Adding a new system stops being scary: want to launch a new recommender? Let it copy the ledger from page one. And mistakes become undoable: replay the ledger with the fixed logic, build a fresh copy alongside, and switch over once it checks out — the old copy sits untouched, so you can always go back. The author puts it more boldly still: what we call a "database" is really just storing, indexing and looking things up sold together in one box. Take the box apart, hand each job to the tool best at it, and thread the ledger between them — now you have one enormous database spread across the whole company.

But what if something gets counted twice?

You still have to guarantee a thing happens exactly once. A user double-taps the pay button, and de-duplication at any middle layer can quietly fail. The author's answer is to put the two ends in charge: issue a unique ticket number with the operation at the moment it starts, carry it all the way through, and have the far end honour the ticket, not the request — the same number only ever counts once.

The other half of the answer sounds more like a merchant than an engineer: not every rule has to be enforced on the spot. Airlines overbook flights anyway; they reconcile afterwards and compensate whoever got bumped. "Never be wrong" is traded for "notice when we're wrong and make it right." And that is exactly where this architecture's honest cost lies: every copy lags a little by design — you get rebuildability and room to grow, and you pay with "just because you wrote it doesn't mean everyone can see it yet." The book closes on a turn few engineering books take, asking not what we can build but what we should: models trained on history quietly copy old prejudice into the future, and the user data you hoard is less an asset than a liability waiting to go off — delete it once you no longer need it.

Remember this one line

Stop letting each system do its own writing in its own order. Keep one ledger, make every change queue up in it, and treat the other systems as copies; for correctness, trust no middle layer — have the two ends honour a ticket number; and for rules you cannot enforce on the spot, reconcile afterwards and make amends.

Want the actual mechanisms, notation and diagrams? → Switch to the deep read