TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to Replication and read replicas

๐Ÿ“š Replication and read replicas: cheat sheet

๐ŸŽจ The analogyA head office keeps the master ledger and a diary of changes that copy rooms replay, sometimes a little behind.
๐ŸŽ“ The regular explanationKeeping copies of a database on other servers by replaying the primary's change log. Replicas scale reads and take over on failure, but asynchronous copies can lag behind.
The master ledger and its copiesStep 3 of 3
Change diaryo1key INSERT order 1: o101234๐Ÿง‘โ€๐Ÿ’ผClerk๐ŸขHead office๐Ÿ Copy room 1๐Ÿ Copy room 2
  1. All changes go to head office, which keeps the master ledger. It also writes each change in a diary.
  2. Copy rooms read the diary and repeat each change in their own copy of the ledger.
  3. Lots of people only want to look things up. They can ask any copy room, so head office isn't swamped.
The copy is a little behindStep 3 of 3
๐Ÿง’Customer๐ŸขHead officeLeeds๐Ÿ Copy roomLeeds
  1. You change your address at head office. The copy room hasn't heard yet.
  2. You look up your details a second later, and the copy room shows your old address. Confusing.
  3. A simple fix. Right after you change something, read your own details from head office for a short while.
Head office burns downStep 3 of 3
๐Ÿง‘โ€๐Ÿ’ผClerkโœ• โœ•๐Ÿ’ซHead office๐Ÿ Copy room 1new primary๐Ÿ Copy room 2
  1. Head office suddenly closes. Nobody can record changes.
  2. The most up-to-date copy room becomes the new head office.
  3. Everyone is told the new address, and the other copy room copies from there now.
The acknowledged write that vanishedStep 5 of 5
Head office diary ยท o42 dropped, followingo41key order 41: o410o42key order 42: o42123Copy room diary ยท in charge nowo41key order 41: o410o43key order 43: o43123๐Ÿง‘โ€๐Ÿ’ผClerk
  1. The clerk records order 41 at head office. The change is copied into the copy room's diary straight away.
  2. Order 42 is written at head office, and the clerk is told "done". The copy has not been made yet.
  3. Head office closes before the copy is made. The only copy of order 42 is stuck inside.
  4. The copy room becomes the new head office. The clerk records order 43 there, and order 42 is nowhere in its diary.
  5. The old head office reopens with order 42 that nobody else has. It can't just rejoin. It must drop that change and copy from the new office.

๐Ÿงฉ Key questions

Why do copy rooms help head office?
Most people only want to look things up, so they ask a copy room and head office isn't swamped. Changes still go to head office, which writes them in the diary.
You change your address at head office, then look it up at a copy room a second later. What might you see?
Your old address, because the diary hasn't reached that room yet. So right after a change, read your own details from head office for a little while.
Head office closes. What happens?
The most up-to-date copy room takes over, and changes not yet copied might be lost. Everyone is told the new address, the way failover updates clients.
Head office tells the clerk a change is done, then closes before it is copied, and a copy room takes over. What happens to that change?
Nobody has it, so it is lost even though the clerk was told it was done. Holding the 'done' until a copy room has the change avoids this, but every change gets slower.