ThoughtsInWords

The Anatomy of a Distributed Lock and How it differs from Physical Clocks

Date: February 01, 2026

Distributed systems are notoriously difficult to get right. One of the fundamental primitives used to coordinate access to shared resources is the distributed lock. While it shares the name “lock” with the mutexes we use in multi-threaded programming on a single machine, the mechanics and failure modes are vastly different. A critical misunderstanding in this space is the reliance on physical clocks for correctness, which acts as a hidden trap for many implementations.

The Purpose of a Distributed Lock

In a distributed environment, multiple processes (nodes) often need exclusive access to a shared resource—be it writing to a file in cloud storage, processing a specific transaction, or updating a database row. A distributed lock ensures mutual exclusion: at any given moment, only one node believes it holds the lock.

However, unlike a single kernel managing threads, there is no single observer in a distributed system. Nodes can crash, networks can partition, and packets can be delayed arbitrarily.

The Physical Clock Fallacy

Physical clocks (or “wall clocks”) are the timekeeping devices on each server. We rely on them to tell us it’s 10:00 AM. In distributed systems, relying on them for ordering or coordination is dangerous.

The Problem of Clock Drift

Servers rely on protocols like NTP (Network Time Protocol) to synchronize their clocks. However, clocks drift. A quartz crystal oscillates at a slightly different frequency depending on temperature. NTP updates can step the clock backwards or forwards significantly.

If a distributed lock implementation relies on the client’s local clock to determine when a lock expires (a lease), it is vulnerable. For example:

  1. Node A acquires a lock with a 10-second lease.
  2. Node A’s local clock jumps forward by 5 seconds due to an NTP sync.
  3. Node A thinks it has held the lock for only a short time, but the storage system (the authority) might think the lease has expired or is close to expiring.
  4. If the lease acts as the sole protection, another Node B might acquire the lock while Node A still thinks it is valid.

Logical Order vs. Physical Time

Because physical time is unreliable, robust distributed systems rely on logical clocks or monotonic counters. Instead of saying “This lock is valid until 12:00:05 PM”, systems like ZooKeeper or Etcd use mechanisms that rely on session liveliness or incrementing version numbers (revision numbers).

Fencing Tokens

One of the most powerful concepts to handle the “incorrect” belief of holding a lock is the fencing token, popularized by Martin Kleppmann in his critique of Redis’s Redlock.

When a locking service grants a lock, it should also return a token (e.g., an incrementing number: 33). Every time the client writes to the shared resource, it must present this token. The storage layer checks the token. If it has already processed a write with token 34 (from a newer lock holder), it rejects the write from token 33. This ensures that even if a node thinks it holds the lock (due to a GC pause or clock drift), it cannot corrupt the system.

Conclusion

Distributed locks are not just about setting a flag in Redis. They require a deep understanding of failure modes. Physical clocks are useful for human debugging (logging timestamps) but are generally unfit for defining the correctness of distributed coordination. True safety comes from acknowledging that time is relative and distinct nodes will never agree on it perfectly. Implementations must use fencing tokens or strict session management to prevent race conditions caused by the illusion of synchronized time.