System Design · Architecture under constraints

How do distributed locks work and when are they unsafe?

Harddistributed lockingfencing tokensleasesUpdated Oct 2026
Quiz this track

Short answer

A distributed lock grants temporary exclusive access through a lease in a shared system such as Redis, etcd or ZooKeeper. A paused holder can outlive its lease, so correctness also requires fencing tokens.

A lock prevents two processes from performing the same exclusive action, such as electing a leader or running a migration. In a single process, a mutex is enough. Across machines, the lock must live in a shared system that both processes trust and that can expire a dead holder.

Common implementations

  • Redis with SET key value NX PX ttl: a single atomic acquire with an expiry
  • Redlock: acquire from a majority of independent Redis nodes, with a contested safety record under clock and pause assumptions
  • etcd or ZooKeeper: leases and ephemeral nodes tied to a session, with quorum-based consensus
  • Database row lock or advisory lock: simple when the database is already the coordination point

Expiry is necessary because a holder can crash, but expiry creates the core hazard. A holder may pause for a garbage-collection stop, lose the lease, and then resume believing it still owns the lock. Another process may have acquired it in the meantime. The result is two writers and a lost update.

The robust fix is a fencing token: the lock service issues a monotonically increasing number on each acquisition, and the protected resource rejects writes with a token lower than the highest it has seen. The lock becomes an optimization for reducing contention; the resource enforces the actual safety invariant. This is why a lock alone is not a correctness guarantee.

Use a distributed lock for efficiency, not as the only defense of a critical invariant. If the consequence of two holders is a double charge or data corruption, the storage layer must enforce a version, conditional write or unique constraint.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Why can a lock holder still cause two writers after a pause?
  2. 2What is a fencing token and where is it enforced?
  3. 3When would you use etcd instead of Redis for locking?
  4. 4Can a distributed lock provide mutual exclusion forever?

Say it in the interview

A distributed lock is a lease in a shared system, and it can expire while the holder is paused, so the holder may resume believing it still owns the lock. I use a fencing token that the protected resource checks, so a stale holder cannot overwrite newer work. For leader election I prefer a quorum-based system such as etcd or ZooKeeper. A lock reduces contention, but the storage layer has to enforce the real invariant.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live System Design mock interview and get scored feedback on depth, clarity and confidence.