โ Back to Redis persistence and replication
๐งพ Redis persistence and replication: cheat sheet
๐จ The analogyA till that keeps everything in memory, with photos and a receipt roll to rebuild it after a power cut.
๐ The regular explanationHow an in-memory store like Redis or Valkey survives restarts with RDB snapshots and the append-only file, and survives server loss with asynchronous replicas and Sentinel failover.
- Ana buys sweets on her tab. The till remembers it straight away, in its own memory.
- Asking for her tab is instant, because the answer is right there in the till.
- The power goes off. The till goes dark, and its memory goes blank.
- The power comes back, but nobody wrote anything down in the back room, so the till starts the day with no tabs at all.
- A busy morning. A hundred things change in the till, which is the shop's sign to take a photo.
- A helper steps in to take the photo, so the cashier doesn't have to stop serving.
- The helper puts the photo in the album. Meanwhile Bob buys something, but he's not in this photo.
- The helper leaves. Cy buys something too, and then the power goes off.
- The shop restores the till from the 10 o'clock photo. Quick, but Bob's and Cy's sales are gone.
- Ana's tab goes up by 5. The till prints the sale on the receipt roll, then tells her it's done.
- More sales go on the roll, one line each. The printer inks whatever is waiting once a second.
- A sale goes through and Ana is told it's done, then the power goes off. Its line was still waiting in the printer, so it never reached the paper.
- The power comes back. The shop reads the roll from the top and redoes every sale, so the tabs are right again.
- The roll is getting long, so the shop writes a short summary on a fresh roll and throws the long one away.
- The trainee starts from a photo of the main till, so both tills know the same tabs.
- Ana buys something. The main till tells her it's done first, then calls the sale out to the trainee.
- Customers can check their tab at the trainee's till. But the trainee won't take a new sale.
- For an important sale, the customer asks the cashier to check with the trainee, and waits for a nod.
- Dan pays and is told it's done, but the main till breaks before calling his sale out. The trainee never heard about it.
- The managers keep checking that both tills answer.
- The main till breaks. Manager 1 gets no answer and suspects it's broken, but one manager's hunch isn't enough.
- Manager 2 checks too and agrees. Two of three is enough to call the till broken.
- The managers pick one of themselves to deal with it. Most of the managers vote for Manager 1.
- Manager 1 tells the trainee "you're the main till now". Sales go there from now on.
- The old till is fixed and comes back, as the new trainee. It copies from the new main till.
- The customer doesn't remember which till is the main one today. They ask a floor manager, who says Till A.
- They pay at Till A, and check their tab at Till B, where the trainee works.
- Till A breaks. The managers open Till B for sales.
- A manager announces that the main till has moved, so the customer walks over and pays at Till B.
๐งฉ Key questions
- The shop only takes photos of the till. The last photo was four minutes ago, then the power goes off. What happens to this morning's tabs?
- The till comes back as it was in the photo, and the last four minutes of sales are gone. A photo only shows the moment it was taken. Anything sold after it isn't in it.
- The printer inks the receipt roll once a second. The power goes off. What can be lost?
- About the last second of sales, which were still waiting in the printer. Lines only count once they're on paper. Waiting for the ink after every sale would lose nothing, but slows every sale down.
- Dan pays and is told it's done. The main till breaks before calling his sale out, and the managers open the trainee's till. What about Dan's sale?
- It's gone, even though Dan was told it was done. The main till says 'done' before the trainee hears about the sale, so a sale can slip through the gap.
- Why does the shop have three floor managers instead of one?
- So one manager's hunch can't open the trainee's till, and the shop still copes if one manager is away. One manager suspecting a broken till is only a hunch. Enough of them must agree, and most of them must pick who acts.
- The shop keeps both photos and a receipt roll. After a power cut, which does it use to fix the till?
- The receipt roll, because it has the most recent sales. The roll misses at most about a second, while a photo can miss minutes. Photos are still handy as copies to keep elsewhere.