Try
↑ ↓ move↵ openesc close
In this lesson · topic 5 of 7
  1. 1. The lock that lies
  2. 2. Leases and fencing tokens
  3. 3. Trusting storage against token storage
  4. 4. What a 5-second lease and a clock check buy
  5. 5. When the lock's safety depends on the clock
  6. 6. Sessions, jeopardy and sequencers in a production lock service
  7. 7. Check the stale write

Topic 5

When the lock's safety depends on the clock

7 min read

Safety and liveness are different promises

Last topic showed that a lease fails open for a slow holder. A sharper question is which parts of a lock design are allowed to depend on timing.

An algorithm can promise two kinds of thing. Safety: nothing bad ever happens, here that two holders never write at once. Liveness: something good eventually happens, here that someone gets the lock. The sound way to build a distributed algorithm is to make safety hold under any timing at all, with delays, pauses and clock errors only able to slow the system down. This is how Raft works. Timeouts decide when progress happens, never what is decided.

A lease cannot be quite so pure, since expiry is a timing event. The question is whether a wrong clock can make two clients both believe they hold the lock, and whether anything downstream catches it.

A lock with a timing-dependent safety

Kleppmann’s article examines a popular design as a case study. A client sets a key with an expiry on each of five independent key-value servers. If it succeeds on three of them within a time shorter than the expiry, it considers itself the holder. There is no consensus among the five. The majority count adds fault tolerance, and safety relies on every server expiring the key at about the right moment.

The article gives two failure sequences.

A clock jump. Client 1 takes the lock on servers A, B and C. The clock on C jumps forward, for example because it is stepped by a time daemon, and the key expires early. Client 2 takes the lock on C, D and E. Both clients hold a majority.

A pause before the reply. Client 1 sends its requests to all five, then stalls in a long pause while responses sit in its network buffers. The keys expire on all servers. Client 2 takes the lock. Client 1 wakes, reads the old responses, and believes it holds the lock.

Neither sequence needs a bug. They need a clock step or a pause of the kind the previous topics measured.

Predict first

A lock service stores each lock with a 10 s expiry measured on its own clock. Which event can make two clients believe they hold the lock, without any software bug?

What to take from it

The same article makes the main point plainly. Such a lock has no fencing token: nothing in the five random values is guaranteed to increase, so the protected resource cannot tell the older holder from the newer one. And its safety rests on three timing assumptions: bounded network delay, bounded process pauses and bounded clock error, all small compared with the expiry.

The article concludes that for efficiency a single lock server is enough, so the five-server scheme adds cost without adding safety. For correctness the lock needs a consensus-backed service that issues increasing tokens, and a resource that checks them. The designer of the five-server scheme has disputed the critique, so it is worth reading both sides, but the arithmetic of the two sequences above does not depend on who wins the argument.

Use the right clock

Two small habits cut the first failure. Measure elapsed time on a monotonic clock, one that never steps backward and is not adjusted by a time daemon. Wall-clock time can jump. And keep a margin: if the lease is 10 s, the client should stop acting at about 8 s, allowing for the grant’s travel time and a clock rate error. These help, as the lab showed with its drift setting, but they do not make the system safe against a pause, and only the resource’s check does.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms