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.

Worker 1 freezes while holding the lock

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, NOWLock servicelease: 8 sNOWWorker 1Worker 1waiting for the lockWorker 2Worker 2Storage, the shared reportStoragethe shared report
  • 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

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.

Clock
0s
Lock held by
nobody
free
Think they hold it
0workers
Highest token seen
–
the storage does not check
The report holds
no change yet

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.

Lock
Free
Any worker can take it
Token check at the store
On
Highest token seen: 0
The shared report holds
“The first version of the report”
Last written by: System

Two workers, one lock service and one shared store

Worker 1Waiting
Token it holds: none
Lock service
Lends the lock to one worker at a time, with a token number that only goes up
Held by:nobody
Lease left:0s
Next token:1
Worker 2Waiting
Token it holds: none

The store checks tokens

When this is on, the store refuses a write whose token is lower than the highest token it has seen.

Log of what happened

[5:35:56 AM]Nobody holds the lock yet, and the store checks tokens.