Scrivio Interview library
ConceptFoundations6 min read · updated 13 November 2026

Teams use distributed locks for very different reasons. Some locks only avoid doing work twice, while others protect data from corruption, and the difference decides what kind of lock is good enough.

Classify these uses as efficiency or correctness locks and say what each needs: a nightly report generator, rebuilding a cache entry after a miss, charging a customer's card for an order, and appending to a shared file in object storage.

The nightly report and the cache rebuild are efficiency locks: they only avoid duplicate work, an occasional duplicate is harmless, and a simple Redis SET NX PX lock or ShedLock is enough. Charging a card and appending to a shared file are correctness locks: two holders cause real damage, and since a lock holder can pause past its expiry, they need fencing, idempotency (one payment per order plus the provider's idempotency key) or conditional writes (write only if the object's ETag still matches) rather than a lock alone.

Why this question is asked

The phrase "we use a distributed lock" hides two very different intentions. Sometimes we just want to avoid doing the same work twice, and sometimes we are relying on the lock to keep data correct. Interviewers ask this classification question because the right tool depends entirely on which of the two we mean, and because many production bugs come from using an efficiency lock for a correctness job. In this answer, we will define both kinds, see why a lock alone cannot guarantee correctness, and then go through the four examples and what each one needs.

1Two Reasons to Take a Lock

An efficiency lock exists to save work. If the lock fails and two processes do the job, we waste some CPU or send one extra query to the database, but nothing is wrong afterwards. A correctness lock exists to protect data. If it fails and two processes act at the same time, we get a corrupted file, a lost update or a customer charged twice.

A simple test separates them: imagine the lock silently failed once and two holders ran together. If the result is merely wasteful, it is an efficiency lock. If somebody has to clean up data or refund money, it is a correctness lock. Martin Kleppmann popularised this distinction in his analysis of Redis-based locking, and it is the most useful first question to ask about any lock.

2Why a Lock Alone Cannot Guarantee Correctness

A distributed lock is really a lease: it has an expiry, so that a crashed holder does not block everyone forever. That expiry is also its weakness. A holder can be paused by garbage collection, CPU throttling or a slow network for longer than the lease. While it is paused, the lease expires, another process takes the lock and does its work, and then the first process wakes up and carries on, still believing it holds the lock.

Step throughA paused holder writes after its lease expired
Client 1Lock serviceClient 2Storage
acquire lock (30 s lease)
GC pause of 40 s
lease expired: acquire lock
write version B
wakes up, writes version A
accepted: B is overwritten

6 messages in order. Play them, or step through them one at a time.

Without a check at the storage, the stale holder's write silently wins.

The fix is to let the resource itself reject stale holders. A fencing token is a number that increases every time the lock is granted. The holder sends it with every write, and the resource remembers the highest token it has seen and rejects any write with a lower one. In the diagram, client 1 would carry token 33 and client 2 token 34, so the storage would refuse client 1's late write. Fencing requires the lock to come from a strongly consistent store (a database, etcd or ZooKeeper) and the resource to check the token. Often the better option is to remove the need for a lock altogether, as we will see.

3The Two Efficiency Locks

The nightly report generator runs on several pods, and we only want one of them to build the report. If the report job is idempotent, meaning it overwrites the same output for the same date, then a rare duplicate run only wastes time. ShedLock is a common choice in Spring applications: it stores the lock in a database table or Redis and wraps a @Scheduled method.

Java
@Scheduled(cron = "0 0 2 * * *")
@SchedulerLock(name = "nightlyReport", lockAtMostFor = "PT30M", lockAtLeastFor = "PT1M")
public void buildReport() {
reports.writeFor(LocalDate.now().minusDays(1)); // overwrites the same file: safe to repeat
}
// Cache rebuild after a miss: only one caller rebuilds, others wait or serve stale data
Boolean got = redis.opsForValue().setIfAbsent("lock:product:42", podId, Duration.ofSeconds(5));
// SET lock:product:42 pod-7 NX PX 5000 -> true for the first caller, false for the rest

The cache rebuild is the second efficiency lock. When a popular entry expires, hundreds of requests might all miss at once and hit the database together. A short Redis lock with SET NX PX lets one caller rebuild the entry while the others wait briefly or serve the stale value; this pattern is often called single flight. If the lock fails, two callers rebuild the same entry and the database sees one extra query, which is fine.

4The Two Correctness Locks and What They Really Need

Charging a customer's card for an order is the clearest correctness case: two holders means two charges. A lock does not fix it, because of the pause problem above. The real protection is idempotency. We create one payment record per order with a unique constraint, so a second attempt for the same order fails at the database, and we send the payment provider an idempotency key derived from the order (most providers accept one in a request header), so even a retried network call creates only one charge.

Appending to a shared file in object storage is the other correctness case. Objects cannot be appended to in place, so an append is really read the object, add a line, write it back. Two writers that both read version 1 will each write their own version 2, and one append is lost. Instead of a lock, we use a conditional write: write only if the object's version or ETag still matches what we read. Amazon S3 supports this with the If-Match header, and Google Cloud Storage with a generation precondition; if the object changed, the write fails with 412 Precondition Failed and we re-read and try again. An even simpler design writes each record as its own object and lets the reader combine them.

Classifying the four uses
UseKindWhat it needs
Nightly reportEfficiencyShedLock or SET NX PX; job overwrites its output
Cache rebuild after a missEfficiencyshort SET NX PX lock (single flight)
Charging a cardCorrectnessunique payment per order + provider idempotency key
Append to shared objectCorrectnessconditional write on ETag, retry on 412

5Follow-up questions

Is Redlock good enough for a correctness lock?

Redlock acquires the lock on a majority of independent Redis nodes, which makes the lock itself more available, but it still has an expiry and no fencing token, so a paused holder can still act after expiry. For correctness we need fencing or idempotency at the resource, whatever lock algorithm we use.

What happens to an efficiency lock if Redis is down?

We decide in advance. For an idempotent report it is reasonable to fail open and let every pod skip or run the job, depending on cost. For the cache rebuild, failing open just means several callers rebuild. Because the work is harmless to repeat, availability matters more than strictness here.

Where does the fencing token come from?

From a store that can increase a number atomically with the lock grant, such as a database row updated in a transaction, an etcd revision or a ZooKeeper node's version or sequence number. A plain Redis SET NX does not provide one, which is another reason it suits only efficiency locks.

6Conclusion

In this answer, we separated efficiency locks, which only avoid wasted work, from correctness locks, which protect data. The nightly report and the cache rebuild are efficiency locks, and ShedLock or a Redis SET NX PX lock is enough. Charging a card and appending to a shared object are correctness locks, and because a paused holder can act after its lease expires, they need fencing, idempotency with a unique payment per order and a provider idempotency key, or conditional writes on the object's ETag.

What to say in the interviewAbout 90 seconds aloud

Open with

I first ask what happens if the lock fails once and two holders run. If the result is just wasted work, it is an efficiency lock; if data is damaged, it is a correctness lock.

Then, if they want more

The nightly report is an efficiency lock, assuming the job overwrites its output for the date, so ShedLock on the scheduled method or a Redis SET NX PX lock is enough.

Rebuilding a cache entry after a miss is also efficiency: a short SET NX PX lock gives single flight and protects the database, and a rare double rebuild is harmless.

Charging a card is a correctness case, and here a lock alone is not enough, because a lock is really a lease with an expiry, and a holder paused by GC can wake up after expiry and still charge.

So I rely on idempotency: one payment record per order with a unique constraint, plus the provider's idempotency key derived from the order, so any second attempt is harmless. Appending to a shared object in storage is also correctness, since an append is read-modify-write and two writers lose one update.

I would use a conditional write, If-Match on the ETag in S3 or a generation precondition in GCS, and retry on a precondition failure. In general, correctness needs a fencing token checked by the resource, or a design that removes the need for the lock.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Classify these uses as efficiency or correctness locks and say what each needs: a...": https://scrivio-interview-library.netlify.app/architecture/distributed-systems/9bcc377543/