โ Back to Distributed locks with Redis
๐ Distributed locks with Redis: cheat sheet
๐จ The analogyOne key to a shared meeting room, tagged with the borrower's name and a time it must come back by.
๐ The regular explanationHow several servers take turns on a shared resource using a Redis key as a lock. SET with NX and PX takes it, a token check releases it, the TTL frees it after a crash, and fencing tokens protect against holders that outlive their lease.
- Ana and Ben each want the meeting room. Each takes the key from their own team's drawer, and each drawer says yes.
- Both walk into the room at the same time and talk over each other. Two keys meant no real turn-taking.
- Now there is one key on one hook by the door. Both ask for it, but the hook gives it to Ana and tells Ben it's taken.
- Ana has the room to herself, and Ben waits for the key to come back.
- Ana asks for the key, but only if it's on the hook. It is, so she takes it and writes her name and a back-by time on the tag.
- Ben asks for the key a moment later. The hook is empty, so he's told it's taken.
- Ana trips on the stairs and never comes back. The key is gone, but the time on its tag keeps running down.
- The time on the tag runs out, and the key goes back on the hook by itself.
- Ben asks again and gets the key, with his own name and time on the tag.
- Ana takes the key. Her name is on the tag.
- Ana gets held up. Her time runs out, the key goes back on the hook, and Ben takes it with his name on the tag.
- Ana comes back to hang up the key. The tag is read first, it says Ben, so nothing happens and Ben keeps his turn.
- Ben finishes and hands the key back. The tag says Ben, so it goes on the hook.
- Ana takes the key. This loan is ticket number 33.
- Ana dozes off in the room without knowing it. While she sleeps, the time on her tag runs out.
- Ben takes the key as ticket 34 and files his papers. The cabinet notes that 34 is the highest ticket it has seen.
- Ana wakes up and files a paper as if it were still her turn. The cabinet sees ticket 33, lower than 34, and refuses it.
- Ana sees the refusal and knows her turn is over. She waits for Ben to hang the key back up, takes it as ticket 35 and redoes her filing. No papers got mixed, but the work she did before her nap is wasted.
- Ana notes the time and asks all five hooks, in five buildings, for the key, using the same name on every tag.
- Hooks 1, 2 and 3 hand over their key. Hook 4 has fallen off the wall, and hook 5 is too slow to answer.
- Three of five is more than half, and it only took a moment, so the room is Ana's. She counts her time from the start, minus a little in case the clocks differ.
- But if Ana dozes off for longer than her time, all three tags run out anyway. Five hooks protect against a lost hook, not against a sleeping borrower.
- You ask your helper for the room key, saying you'll wait up to ten seconds for it. The helper goes to the hook.
- The key was free, so the helper takes it, writes your name on the tag and tells you to go ahead.
- Your work takes longer than planned. While you're still awake and working, the helper keeps going back and writing a later time on the tag.
- You finish and tell the helper. It checks the tag still has your name, hangs the key back up, and stops renewing.
๐งฉ Key questions
- Two teams each keep a room key in their own drawer. Why do they still end up in the room together?
- Each drawer only knows about its own key, so both say yes. Taking turns only works if everyone uses the same key on the same hook.
- Ana's turn ran out and Ben now has the key. Ana comes back and hangs up the key. What should happen?
- The tag says Ben, so nothing happens and Ben keeps his turn. Reading the name first stops Ana from cancelling Ben's turn by accident.
- Why does the key come with a time it must be back by?
- So a borrower who never comes back can't keep the room forever. When the time runs out, the key goes back on the hook by itself.
- Ana dozes off in the room past her time, and Ben takes the key. What stops Ana's papers from mixing with Ben's when she wakes?
- The cabinet refuses papers with a ticket number lower than one it has seen. Ana could doze off right after checking. A longer time only makes that rarer, and five hooks only help when a hook falls off, not when Ana sleeps. The cabinet's check works whenever she wakes.
- Ben holds the key in the room. The hook that hands out keys is replaced by a spare that never heard Ben took it. What can happen?
- Someone else takes the key from the spare hook and now two people are in the room. The spare only knows what it was told before the old hook fell off, and the news of Ben's loan may not have reached it.