Quick revision
The short answer
Redis locks depend on timing: a GC pause, a VM freeze or a clock jump can let the lock expire while the holder still believes it holds it, and asynchronous replication means a failover can lose a lock that was just acquired; Redlock across independent nodes reduces but does not remove those timing assumptions. For a charge, we therefore make correctness independent of the lock: the resource checks a fencing token, a monotonically increasing number such as a version column in the database update, and the payment itself is idempotent through an idempotency key and a unique constraint. Redis locks remain fine for efficiency, avoiding duplicate work, while safety belongs to the database or a consensus system such as etcd or ZooKeeper.
What to remember
In this answer, we saw that Redis locks are leases that rely on timing, so process pauses, VM freezes or clock jumps can let a lease expire while its holder keeps working, and asynchronous replication can lose a newly acquired lock in a failover. Redlock reduces but does not remove those assumptions. For charging a customer, we make correctness independent of the lock with a fencing check, a version condition in the database update, and with idempotency keys and a unique constraint, keeping Redis locks for efficiency and using the database or etcd or ZooKeeper for safety.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Redis locks depend on timing: a GC pause, a VM freeze or a clock jump can let the lock expire while the holder still believes it holds it, and asynchronous replication means a failover can lose a lock that was just acquired; Redlock across independent nodes reduces but does not remove those timing assumptions. For a charge, we therefore make correctness independent of the lock: the resource checks a fencing token, a monotonically increasing number such as a version column in the database update, and the payment itself is idempotent through an idempotency key and a unique constraint. Redis locks remain fine for efficiency, avoiding duplicate work, while safety belongs to the database or a consensus system such as etcd or ZooKeeper.
Why this question is asked
Charging a customer twice is one of the worst bugs a system can have: it costs money, trust and support time, and refunds take days. Teams often protect the charge with a Redis lock, through Redisson or a Redlock-style algorithm, and assume that settles it. Interviewers ask this question to see whether we understand why a lease-based lock in an asynchronous system cannot guarantee mutual exclusion by itself, and what actually makes the operation safe. In this answer, we will recap how Redis locks work, look at the timing and replication weaknesses with concrete timelines, see what Redlock does and does not fix, and then add fencing tokens and idempotency, ending with when a Redis lock is still the right tool.
1A Quick Recap of Redis Locks as Leases
A Redis lock is a key created with SET key token NX PX ttl. Whoever creates it holds the lock until it is deleted or its expiry passes. That expiry makes it a lease: a lock that is granted for a limited time so that a crashed holder cannot block everyone forever. Redisson adds a watchdog that renews the lease while the holder is alive, and Redlock acquires the same lock on a majority of several independent Redis nodes.
A lease only works if the holder stops acting before the lease runs out. Nothing in Redis can enforce that, because Redis cannot stop a process that is paused or slow. It simply lets the key expire and grants the lock to the next caller.
2Weakness 1: Pauses and Clocks
Imagine worker A acquires the lock with a 10-second lease, reads the order, and calls the payment provider. Then the JVM stops for a 15-second full garbage collection, or the virtual machine is frozen by the host for live migration. The lease expires during the pause. Worker B acquires the lock, sees the order unpaid, and charges it. Then A wakes up, with no idea that time passed, and finishes its own charge. The customer is charged twice, and each worker followed the lock protocol correctly.
The watchdog does not help, because it runs in the same paused process. Checking the lock again before charging does not help either, because the pause can happen right after the check. Clock problems make it worse: lease expiry depends on Redis's clock, and if a server's clock jumps forward, a lease can end early.
8 messages in order. Play them, or step through them one at a time.
Each worker believed it held the lock when it charged.
3Weakness 2: Replication and What Redlock Changes
With a single Redis primary and replicas, replication is asynchronous. Worker A's SET is acknowledged by the primary, the primary crashes before replicating it, a replica is promoted without the key, and worker B acquires the same lock immediately. Two holders again, this time without any pause.
Redlock addresses this by using five independent Redis masters: a client holds the lock only if it acquired it on at least three within a time well below the lease. Losing one node no longer loses the lock. But Redlock still assumes bounded pauses, bounded network delay and clocks that run at roughly the same rate, so the pause scenario above still breaks it. It reduces the failure modes; it does not remove the timing assumptions. For efficiency that is fine. For a charge, it is not a guarantee.
4Fencing Tokens: Let the Resource Say No
The way out is to stop relying on the lock alone and let the resource reject stale writers. 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 refuses any write with a lower one. When A wakes up from its pause carrying token 33, the resource has already accepted B's token 34 and rejects A.
The token must come from something that never goes backwards, and Redis's asynchronous replication makes a Redis counter a weak source, because a failover can lose increments. The most robust choice is to let the database be the fence: a version column on the row we change, checked in the same UPDATE that records the charge.
- Acquire the Redis lockoptional: avoids most duplicate work
- UPDATE order to CHARGING where version matchesthe fence
- Exactly one row updated?YesCall the payment API with the idempotency keyNoStop: another worker owns this charge
- Record the result and mark PAIDin the same database
4 steps in order. Play them, or step through them one at a time.
Notice that the condition also checks the state, so only one worker can move the order from UNPAID to CHARGING. The worker that gets 0 rows does not call the payment provider at all. The lock now only reduces how often two workers race; the database decides who wins.
5Idempotency for the Payment Itself
One more gap remains: the payment call can succeed while our process crashes before recording it, and a retry would charge again. So the payment itself must be idempotent. We create an idempotency key, for example charge-order-7, store it in a payments table with a unique constraint, and pass it to the payment provider, most of which deduplicate requests carrying the same key.
With the unique constraint, a duplicate attempt fails safely in the database, and with the provider's idempotency key, a retried call returns the original result instead of charging again. Now correctness no longer depends on the lock at all.
6When a Redis Lock Is Still the Right Tool
The distinction to remember is between efficiency and safety. A lock for efficiency prevents duplicate work, such as two instances both rebuilding the same report; if it fails occasionally, we waste some CPU. Redis locks are excellent for that: fast, simple and good enough.
A lock for safety must never fail, because failure means corrupted data or a double charge. For that, the guarantee must come from the database, with fencing, constraints and transactions, or from a consensus system such as etcd or ZooKeeper, which offer locks with monotonically increasing revision numbers that serve directly as fencing tokens. For a charge, we use the database guarantees and keep the Redis lock, if at all, only to reduce contention.
7Follow-up questions
Show a fencing check in an SQL update.
With a version column: UPDATE orders SET status = 'CHARGING', version = version + 1 WHERE id = 7 AND version = 5. A worker that read version 5 succeeds only if nobody changed the row since; a stale worker gets 0 rows updated and must stop. With an explicit token from a lock service, the shape is UPDATE resource SET ..., fence = :token WHERE id = :id AND fence < :token, so any write carrying an older token than the last accepted one is rejected.
Is Redisson's watchdog enough to prevent double holders?
No. The watchdog renews the lease from a thread inside the holder's process, so a pause of the whole process stops renewals too and the lease can expire. It prevents expiry during a normal long job, not during a stop-the-world pause or a network partition.
Why not hold a database row lock during the payment call instead?
Holding SELECT ... FOR UPDATE across a slow external call keeps a transaction and a connection open for seconds, which hurts throughput and can exhaust the pool. The pattern above uses short transactions: one to claim the charge with a conditional update, the external call outside any transaction, and one to record the result, with idempotency covering retries.
8Conclusion
In this answer, we saw that Redis locks are leases that rely on timing, so process pauses, VM freezes or clock jumps can let a lease expire while its holder keeps working, and asynchronous replication can lose a newly acquired lock in a failover. Redlock reduces but does not remove those assumptions. For charging a customer, we make correctness independent of the lock with a fencing check, a version condition in the database update, and with idempotency keys and a unique constraint, keeping Redis locks for efficiency and using the database or etcd or ZooKeeper for safety.
Open with
The core weakness is that Redis locks are leases that depend on timing. If the holder pauses longer than the lease, because of a long GC, a VM freeze or a clock jump, the key expires, another worker acquires the lock, and when the first one wakes up it still believes it holds it, so both charge.
Then, if they want more
Redisson's watchdog runs in the same paused process, so it cannot help. Replication adds a second problem: it is asynchronous, so a failover can lose a lock that was just acquired. Redlock across independent masters handles single node loss but still assumes bounded pauses and clock drift, so it reduces the risk without eliminating it.
For money, I would make correctness independent of the lock. First, fencing: every claim carries a monotonically increasing token that the resource checks, and the simplest robust version is a version column, updating the order to CHARGING only where the version and status still match, so a stale worker updates zero rows and stops.
Second, idempotency: an idempotency key per charge, stored under a unique constraint and sent to the payment provider, so any duplicate attempt fails safely. I would still use a Redis lock for efficiency, to avoid duplicate work, but for safety I rely on the database, or a consensus system like etcd or ZooKeeper that provides real fencing tokens.
About this answer
- Last updated
- 12 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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation
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 "A team uses Redis locks (Redisson, Redlock-style) to make sure only one worker charges a...": https://scrivio-interview-library.netlify.app/databases/redis/a4ed038f85/