Lesson 1 of 17
14. Replication

The picture to keep in your head

3 min read+10 XP

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 bankIn the system
Head office that writes the noticesLeader (primary): takes all writes
Branch photocopiesFollowers (replicas): copies that serve reads
Couriers carrying copies in orderThe replication log
Copies arriving lateReplication lag
A branch becoming the new head officeFailover
Two offices both issuing ordersSplit brain
Several offices allowed to issue noticesMulti-leader replication
Asking several branches and trusting the majorityLeaderless replication with quorums

the bank's head office and branches mapped to leader, followers, lag, failover and split brain.

Figure: copies make you faster and safer; keeping copies in step is the whole difficulty.