Distributed locks & fencing tokens
A worker freezes while it holds a lock, and the lock runs out. See when two workers both write, and how storage that checks a fencing token turns the late write away.
Step through one lock
Two workers want to change one report, and a lock lets one in at a time. Pick how long Worker 1 freezes while it holds the lock, then press Next step. Back takes you to the step before.
Longer than the 8 s lease.
On: the storage refuses a write whose token is lower than one it has already seen.
With these settings: Lost update. Worker 1 wrote after its lease ran out, and the storage could not tell. Worker 2's change was overwritten with no error.
- Lock service — lease: 8 s — healthy — NOW
- Worker 1 — healthy — waiting for the lock
- Worker 2 — healthy
- Storage — the shared report — healthy
- Ask or release
- Lock granted, with a token
- Write
Step 1 of 9 · Lock service
Done: Worker 1 asks for the lock
- What happens
- Worker 1 wants to change the shared report. It asks the lock service for the lock first.
- Why
- Two workers changing one report at the same time can lose a change. A lock lets one in at a time.
- What changes
- Nobody holds the lock, so the lock service can grant it.
- Next
- The lock service grants the lock with token 1
Event log
8 more to go. Press a line to go back to that step.
Step 1 of 9: Lock service. Worker 1 asks for the lock. Worker 1 wants to change the shared report. It asks the lock service for the lock first.
What this simplifies. This is two workers and one lock, and the pause is a setting, not chance. A real worker can also be late for other reasons: a slow network, or a clock that runs fast or slow. A fencing token only helps when the storage can compare tokens on every write, and many storage systems cannot. Here the lock service is one process that never fails. A real one is itself several servers that have to agree on who holds the lock.
Try it yourself
Press the buttons in any order. Worker 1 can freeze while it holds the lock, as a program does during a long garbage-collection pause.
Two workers, one lock service and one shared store
The store checks tokens
When this is on, the store refuses a write whose token is lower than the highest token it has seen.