โ 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.
- All changes go to head office, which keeps the master ledger. It also writes each change in a diary.
- Copy rooms read the diary and repeat each change in their own copy of the ledger.
- Lots of people only want to look things up. They can ask any copy room, so head office isn't swamped.
- You change your address at head office. The copy room hasn't heard yet.
- You look up your details a second later, and the copy room shows your old address. Confusing.
- A simple fix. Right after you change something, read your own details from head office for a short while.
- Head office suddenly closes. Nobody can record changes.
- The most up-to-date copy room becomes the new head office.
- Everyone is told the new address, and the other copy room copies from there now.
- The clerk records order 41 at head office. The change is copied into the copy room's diary straight away.
- Order 42 is written at head office, and the clerk is told "done". The copy has not been made yet.
- Head office closes before the copy is made. The only copy of order 42 is stuck inside.
- The copy room becomes the new head office. The clerk records order 43 there, and order 42 is nowhere in its diary.
- 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.