Design a Distributed Lock
Understanding the Problem
🔒 What is a distributed lock? A lock lets one worker at a time act on a shared resource. Inside one process, a mutex does that. Once the workers are separate service instances on separate machines, there's no shared memory to put a mutex in, so they need an outside service to agree on who holds the lock. That service has to deal with things a mutex never sees: holders that crash without unlocking, networks that drop messages, and processes that freeze for seconds and then carry on as if nothing happened.
You'll often get this as part of a bigger question, most famously ticket booking: "while one user is paying for seat 14F, how do we stop every other instance of the booking service from selling it?" That's a distributed lock with a 10-minute TTL, and we'll use it as the running example.
Functional Requirements
Core Requirements
- Clients should be able to acquire a lock on a named resource, with at most one holder at a time.
- Locks should expire automatically after a TTL if the holder disappears, and the holder should be able to extend its lock while it's still working.
- Only the current holder should be able to release its lock.
- Clients should be able to wait for a lock that's taken and get it when it frees up.
Below the line (out of scope)
- Read/write (shared/exclusive) locks. We'll mention them but design exclusive locks.
- Deadlock detection across multiple locks. Clients take locks in a fixed order or one at a time.
- Locks across regions with cross-region latency.
Non-Functional Requirements
The first question to ask your interviewer is what the lock protects, because it changes the design. Is it an efficiency lock (two holders means some wasted work, like two workers rebuilding the same cache entry) or a correctness lock (two holders means corrupted data or a double-sold seat)? We'll design for correctness, and show where you can relax it.
For scale: a ticketing platform during a big on-sale, with on the order of 50,000 seats for one event and 20,000 lock acquisitions per second at peak, plus a steady stream of background jobs taking locks to avoid running twice.
Core Requirements
- Safety: at most one client may act on a resource as its holder at any moment, even if clients pause or the network misbehaves.
- Liveness: a lock held by a crashed client must become available again, within the TTL.
- Availability: the lock service should survive the loss of any one node.
- Latency: acquiring an uncontended lock should take a few milliseconds.
Below the line (out of scope)
- Fairness guarantees stronger than first-come, first-served.
The math here is short. A lock is a key, an owner ID and an expiry: under 100 bytes. 50,000 seat locks is a few megabytes, and 20,000 acquisitions per second is well within what a single Redis node or a well-tuned etcd cluster can do. So neither memory nor throughput is the hard part. The hard part is what "at most one holder" means when a holder can't tell that its lock has expired. Most of this design is about that.
A distributed lock can't be made perfectly safe by the lock service alone. A client that freezes for 30 seconds still believes it holds a lock that expired 20 seconds ago. Safety needs the resource being protected to take part, and that's where fencing tokens come in.