Quick revision
The short answer
Redis replication is asynchronous, so the primary acknowledged the lock and crashed before the replica received it; Sentinel promoted a replica without the key, and a second instance legitimately acquired the same lock. WAIT can narrow that window but cannot close it. For billing, safety must come from the data: idempotency keys per charge, unique constraints on invoice and period, and the payment provider's idempotency. If strict mutual exclusion is still needed, it should come from a consensus store or the database with fencing tokens, with Redis locks kept for efficiency only.
What to remember
In this answer, we saw that the lock was lost because Redis replicates asynchronously: the primary acknowledged the lock, crashed before replicating it, and the promoted replica granted the same lock to a second instance. WAIT 1 100 narrows the window but cannot guarantee the lock survives failover. For billing we move correctness into the data with idempotency keys, unique constraints and the provider's idempotency, use a consensus store or database lock with fencing tokens where strict exclusion is needed, and keep Redis locks for efficiency, while refunding customers and publishing the review.
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 replication is asynchronous, so the primary acknowledged the lock and crashed before the replica received it; Sentinel promoted a replica without the key, and a second instance legitimately acquired the same lock. WAIT can narrow that window but cannot close it. For billing, safety must come from the data: idempotency keys per charge, unique constraints on invoice and period, and the payment provider's idempotency. If strict mutual exclusion is still needed, it should come from a consensus store or the database with fencing tokens, with Redis locks kept for efficiency only.
Why this question is asked
Double-charging customers is one of the most serious incidents a billing team can have: money leaves real accounts, support queues fill and trust drops. The lock looked correct, and in daily use it was. Interviewers present this case to test whether we understand what Redis replication promises, and whether we can move from 'fix the lock' to 'make the system safe even when the lock fails'. In this answer, we will explain the failure precisely, look at what WAIT can and cannot do, move correctness into the data, choose a mutual-exclusion mechanism for when we still need one, and handle the people side of the incident.
1Explaining the Failure
Redis replicates asynchronously. When the scheduler sent SET billing-run <value> NX PX 600000, the primary created the key, replied OK, and queued the write to the replica. The replica normally receives it within a millisecond. On this night, the primary crashed inside that millisecond. Sentinel noticed, waited for its failure timeout, and promoted the replica, which never got the key.
A second scheduler instance then asked for the lock. From the new primary's point of view, the key did not exist, so SET NX succeeded. Nothing malfunctioned: each component behaved as documented. The first instance had a valid OK, the second had a valid OK, and both started charging the same customers.
8 messages in order. Play them, or step through them one at a time.
Both instances received a correct OK from the primary of the moment.
2What WAIT Can and Cannot Do
WAIT numreplicas timeout blocks until the given number of replicas have acknowledged all previous writes on this connection, or until the timeout, and returns how many did. After acquiring the lock, a scheduler could run WAIT 1 100 and refuse to proceed unless it returns 1.
This narrows the window a lot, but it is not a guarantee. Sentinel may promote a replica other than the one that acknowledged, and a replica that acknowledged may still lose the write if it restarts without persistence. WAIT makes the write more likely to survive; it does not turn asynchronous replication into a consensus protocol. For money, 'more likely' is not enough.
3Correctness From the Data, Not the Lock
The lasting fix is to make a duplicate charge impossible no matter how many instances run. Three layers do that. First, every charge gets an idempotency key derived from what it is, for example charge:<customerId>:<invoiceId>, not from the run. Second, the database enforces uniqueness: an invoices table with UNIQUE (customer_id, billing_period), and a charges table with UNIQUE (invoice_id, attempt_purpose), so a second instance's insert fails before it calls the provider. Third, the payment provider's own idempotency: many providers accept an idempotency key on the charge request and return the original result for a repeat, which protects the step between our database and the money.
With these layers, two schedulers running at once waste some effort, and one of them gets constraint errors, but no customer is charged twice. The lock is then an efficiency tool, not a safety device.
- Scheduler lock (Redis)efficiency: usually only one run, may fail
- Database unique constraintsone invoice per customer and period, one charge per invoice
- Idempotency key per chargederived from customer and invoice, not from the run
- Payment provider idempotencya repeated request returns the original charge
4 layers, from the top down. Step through them one at a time.
4When We Still Need Strict Mutual Exclusion
Some steps are hard to make idempotent, or running them twice is very expensive even if harmless. For those, we use a lock built on consensus or on the database. etcd and ZooKeeper replicate writes through a consensus protocol, so an acknowledged lock survives the loss of a minority of nodes; they also provide a revision number that serves as a fencing token. The database is often simpler: a row lock with SELECT ... FOR UPDATE on a billing_runs row, or a PostgreSQL advisory lock held inside the transaction with pg_advisory_xact_lock, ties the lock to the same transaction that writes the charges.
Either way, we pass a fencing token to the writes so that a client that lost its lock cannot write. We keep Redis locks for efficiency elsewhere, where an occasional duplicate run is harmless.
5Handling the Incident Itself
Technical fixes are half the work. We identify every affected customer by comparing charges per customer and period with the provider's records, refund the duplicates proactively and tell customers before they find out. We then classify every lock in the billing path as efficiency or correctness, and any correctness lock either moves to the data layer or gets fencing. Finally we publish a blameless incident review that explains the asynchronous replication gap plainly, so other teams with Redis locks can check their own designs.
6Follow-up questions
How would you detect double charges quickly if they happen again?
Add a reconciliation check that runs during and after each billing run: count successful charges per customer and period and alert on any count above one, and compare our charge records with the provider's settlement data daily. Unique-constraint violations on the charges table are also a strong signal that two runs overlapped, so we count and alert on them. With those, a repeat would be noticed in minutes, not from customer complaints.
Would Redlock have prevented this?
It avoids the failover problem by using independent primaries instead of replicas, but it still relies on timing assumptions and cannot stop a paused instance from acting after its lease ends. For charging money it would reduce the risk without removing it, so idempotency and constraints would still be required.
Why derive the idempotency key from the invoice and not from the run?
Because two runs are exactly the situation we are protecting against. A key that includes a run id would differ between the two schedulers and protect nothing; a key built from the customer and invoice is the same in both, so the second attempt is recognised as a repeat.
7Conclusion
In this answer, we saw that the lock was lost because Redis replicates asynchronously: the primary acknowledged the lock, crashed before replicating it, and the promoted replica granted the same lock to a second instance. WAIT 1 100 narrows the window but cannot guarantee the lock survives failover. For billing we move correctness into the data with idempotency keys, unique constraints and the provider's idempotency, use a consensus store or database lock with fencing tokens where strict exclusion is needed, and keep Redis locks for efficiency, while refunding customers and publishing the review.
Open with
The lock wasn't buggy, the replication model was the gap. Redis replicates asynchronously, so the primary created the lock key and replied OK before the replica had it.
Then, if they want more
It crashed in that window, Sentinel promoted the replica without the key, and when the second instance ran SET NX it succeeded legitimately. Both instances had a valid OK and both charged customers.
I could add WAIT 1 100 after acquiring, so we only proceed once a replica has the key, and that narrows the window, but it's not a guarantee, because failover might still promote a replica that missed the write. For money, correctness has to come from the data, not the lock.
Each charge gets an idempotency key derived from the customer and invoice, the database has unique constraints on invoice per customer and period and on charge per invoice, and we pass an idempotency key to the payment provider so a repeated request returns the original charge. Then two runs waste effort but can't double-charge.
If we still need strict mutual exclusion, I'd use etcd or ZooKeeper, or a row or advisory lock in the database transaction, with fencing tokens, and keep Redis locks only for efficiency. I'd also classify every lock in the billing path, refund affected customers proactively and publish the incident review.
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 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 "Explain the failure and decide what the billing system should use for mutual exclusion...": https://scrivio-interview-library.netlify.app/databases/redis-patterns/77e4300615/