Book Deep Read · Fundamentals of Software Architecture · Chapter 15

Space-Based Architecture: Take the Database off the Hot Path and Ride the Spike in Memory

Fundamentals of Software Architecture · Ch 15 · Mark Richards & Neal Ford · 2020

中文 →

What is this chapter about?

Tickets for a superstar's concert go on sale at 10 a.m. A ticketing site that is quiet most of the day suddenly gets hundreds of thousands of visitors in a single second. Most websites spin, throw errors, or simply fall over. The space-based architecture in this chapter is a way of building software for exactly this situation: "usually idle, suddenly packed."

A strange thing first: more machines don't help

When a site is slow, the first instinct is to add servers. But past a certain point, adding more does nothing. The front servers can be multiplied freely, yet they all end up asking the same database. It is like a store opening a hundred checkout lanes while a single stockroom clerk answers every "is this in stock?" question. More lanes just make the line at the clerk's door longer.

Why the old world was hard

The database is the hardest part of a system to copy into several pieces: it has to keep every account balanced, and extra copies must constantly check with each other, which makes things slower, not faster. So however much you grow the front, the bottleneck sits at that one point, and a surge takes the whole thing down.

The core trick: every counter keeps its own ledger

Space-based architecture makes a bold move: while doing the work, don't talk to the database at all. The system is split into many identical "processing units." Each one is like an independent ticket counter with a complete copy of the ledger on the desk, kept in memory so it can be flipped through instantly. When it sells a ticket, it writes it in its own ledger first, then calls out so the other counters update theirs too.

And the database? It moves to the back office and becomes the archive. After a counter records a sale, it drops a slip on a conveyor belt, and back-office clerks copy the slips into the archive in order. The archive can lag a little, but the counters never wait in line for it.

When the crowd grows, a "shift manager" opens new counters on the spot; when it thins out, some are closed to save money. A new counter just copies the ledger from the counter next door and starts selling.

What it buys you / when to choose it

It gives perhaps the strongest surge-handling of any style in the book, ideal for ticket sales and auctions where crowds swing wildly and answers must be instant. The honest cost: when two counters change the same line at the same moment, one change can occasionally overwrite the other, and the whole setup is hard to build, hard to test and not cheap. If your ledger is huge and changes heavily all day, this is not your tool.

Remember it in one line

Space-based = every processing unit carries an in-memory ledger and keeps it in sync with the others, while the database catches up in the background: you trade consistency and simplicity for the elasticity to survive sudden spikes.

Want the processing unit and the four middleware roles, how data pumps and readers/writers turn the database into a back office, how to estimate the collision rate, replicated vs distributed caching, and the book's star ratings? → Switch to Deep