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 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.
In-process locks vs one shared lockStep 4 of 4
๐Ÿ‘ฉAna๐Ÿ‘จBenwaiting๐ŸชKey hookheld by Ana๐ŸšชMeeting roomcharged once
  1. Ana and Ben each want the meeting room. Each takes the key from their own team's drawer, and each drawer says yes.
  2. Both walk into the room at the same time and talk over each other. Two keys meant no real turn-taking.
  3. 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.
  4. Ana has the room to herself, and Ben waits for the key to come back.
Taking the lock with SET NX PXStep 5 of 5
โœ• โœ•๐Ÿ’ซAna๐Ÿ‘จBenholds lock๐ŸชKey hookBen, 30 s
  1. 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.
  2. Ben asks for the key a moment later. The hook is empty, so he's told it's taken.
  3. Ana trips on the stairs and never comes back. The key is gone, but the time on its tag keeps running down.
  4. The time on the tag runs out, and the key goes back on the hook by itself.
  5. Ben asks again and gets the key, with his own name and time on the tag.
Releasing only your own lockStep 4 of 4
๐Ÿ‘ฉAnaturn was over๐Ÿ‘จBen๐ŸชKey hookfree
  1. Ana takes the key. Her name is on the tag.
  2. 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.
  3. Ana comes back to hang up the key. The tag is read first, it says Ben, so nothing happens and Ben keeps his turn.
  4. Ben finishes and hands the key back. The tag says Ben, so it goes on the hook.
A paused holder and a fencing tokenStep 5 of 5
๐Ÿ‘ฉAnaredone, ticket 35๐Ÿ‘จBen๐ŸชKey hooklent, 35๐Ÿ—„๏ธFiling cabinethighest 35
  1. Ana takes the key. This loan is ticket number 33.
  2. Ana dozes off in the room without knowing it. While she sleeps, the time on her tag runs out.
  3. Ben takes the key as ticket 34 and files his papers. The cabinet notes that 34 is the highest ticket it has seen.
  4. 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.
  5. 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.
Redlock across five independent nodesStep 4 of 4
๐Ÿ‘ฉAnaasleep๐ŸชHook 1expired๐ŸชHook 2expired๐ŸชHook 3expiredโœ• โœ•๐Ÿ’ซHook 4๐ŸชHook 5no answer
  1. Ana notes the time and asks all five hooks, in five buildings, for the key, using the same name on every tag.
  2. Hooks 1, 2 and 3 hand over their key. Hook 4 has fallen off the wall, and hook 5 is too slow to answer.
  3. 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.
  4. 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.
Redisson RLock with the watchdogStep 4 of 4
๐Ÿง‘โ€๐Ÿ’ผYoudone๐Ÿง‘โ€๐Ÿ”งHelper๐ŸชKey hookfree
  1. You ask your helper for the room key, saying you'll wait up to ten seconds for it. The helper goes to the hook.
  2. The key was free, so the helper takes it, writes your name on the tag and tells you to go ahead.
  3. 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.
  4. 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.