14. Replication
The picture to keep in your head
Every question below is answered twice: first at the bank's branches, so you can picture it, then in the system, so you know exactly how it works. Each answer ends with a memory code to lock it in.
The picture to keep in your head
A bank's head office writes every notice: new interest rates, holiday timings, rules. It sends photocopies to every branch by courier, so customers anywhere can read the notice locally instead of travelling to head office. If head office burns down, a branch with a full set of copies can become the new head office. But copies arrive late: a customer at a far branch may read yesterday's rate for a few hours. And if two offices both start issuing notices, branches get contradicting orders.
That's replication: keeping copies of the same data on several machines.
Our running example: the bookstore's main database (users, orders, books). Browsing traffic has grown, the Book Fest is coming (Chapter 6), and losing this one database machine would stop the whole business.
| At the bank | In the system |
|---|---|
| Head office that writes the notices | Leader (primary): takes all writes |
| Branch photocopies | Followers (replicas): copies that serve reads |
| Couriers carrying copies in order | The replication log |
| Copies arriving late | Replication lag |
| A branch becoming the new head office | Failover |
| Two offices both issuing orders | Split brain |
| Several offices allowed to issue notices | Multi-leader replication |
| Asking several branches and trusting the majority | Leaderless replication with quorums |
Figure: copies make you faster and safer; keeping copies in step is the whole difficulty.