Try
↑ ↓ move↵ openesc close

Interview prep / Locks, leases and fencing

Senior · Locks, leases and fencing · System design interview question

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

Learn it by running it

The locks, leases and fencing lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

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