When is it acceptable to use a distributed lock without fencing? How do you decide whether a lock is for efficiency or for correctness?
Sketch your own answer first, then compare it with the one below.
A strong answer
Ask what happens if two clients hold the lock at once. If the answer is wasted work, such as two workers computing the same report or two nodes warming the same cache, the lock is for efficiency. A rare double execution costs money or time but nothing is corrupted, and a simple lease from a single lock server is fine.
If the answer is corrupted data, a double charge, or a violated invariant, the lock is for correctness. Then every assumption has to hold under pauses, clock drift and partitions, and a lease without fencing does not. The resource must check a token, or the operation must be made idempotent or conditional, and the lock service itself needs consensus.
Teams often start with an efficiency lock and quietly begin relying on it for correctness. Write the intent down next to the lock. In a design review, ask for the token check at the resource as proof that a correctness lock is really one.
Sources
- Martin Kleppmann (2016). How to do distributed locking.
- Mike Burrows (2006). The Chubby lock service for loosely-coupled distributed systems.