Scrivio Interview library
ScenarioAdvanced7 min read · updated 13 November 2026

During a flash sale the Redis primary holding the stock counter failed 25 seconds in. A replica was promoted, but asynchronous replication had not copied the last few thousand decrements, and 1,800 more orders were accepted than units existed.

Why did the Redis failover cause overselling, how do you handle the 1,800 extra orders now, and how do you design the stock gate so a failover cannot oversell again?

Redis replicates asynchronously, so decrements the primary had already acknowledged were not on the replica that was promoted; its counter was higher than the true remaining stock, and it handed out 1,800 units that no longer existed. Now we treat the order database as the record, confirm the first 10,000 orders by acceptance time, cancel the rest quickly with automatic refunds or an alternative offer, and ask suppliers whether they can fill some. For the future, the gate must fail safe: durable winners, a durable stock check before an order is confirmed, and a pause on failover that fences the old primary and recomputes stock before reopening.

Why this question is asked

Most flash-sale designs put the stock counter in Redis because it is fast, and most of them work until the day the primary dies in the middle of the sale. This incident is useful because it shows what asynchronous replication means in practice: an acknowledged write is not a durable write. Interviewers ask this to see whether we understand the failure precisely, can handle the customer impact like an adult, and can redesign so that losing data in the fast layer cannot create stock. In this answer, we will explain why the failover oversold, deal with the 1,800 extra orders, design the gate so that losses only undersell, add a pause on failover, and finish with rehearsal.

1Why the Failover Created Stock

Redis replication is asynchronous. When a client runs DECR on the primary, the primary changes its memory, replies to the client, and sends the command to its replicas a moment later. Under normal load the gap is milliseconds; under flash-sale load, with hundreds of thousands of commands per second, the replica can be thousands of commands behind.

When the primary failed 25 seconds in, the replica that was promoted had never received the last few thousand decrements. Its counter still showed units that had already been given away, so the sale continued and gave those units away a second time. Every one of the extra 1,800 winners was told they had won by a system that sincerely believed it.

Step throughHow acknowledged decrements disappeared
Attempt APIRedis primaryReplicaSentinel
DECR stock (thousands per second)
OK, stock 2,400
replicate (behind by a few thousand)
crash at 25 s
promote to primary
DECR stock
OK, stock 4,200: units given again

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

The replica's counter was higher than the true remaining stock.

2Why WAIT Does Not Solve It

Redis offers the WAIT command, which blocks until a given number of replicas have acknowledged the client's previous writes, or a timeout passes. Calling WAIT 1 50 after each decrement shrinks the window, because most acknowledged decrements will be on a replica.

But it does not make failover strongly consistent. WAIT returns after the timeout even if no replica answered, failover may promote a replica that was not the one that acknowledged, and the write is still applied on the primary before anyone else has it. So WAIT reduces how much we can lose, at the cost of latency on every attempt, but a design whose correctness depends on Redis never losing a write is still wrong. We need a design where a lost write cannot hurt.

3Handling the 1,800 Extra Orders Now

First, decide what is true. The order database recorded every accepted order with its acceptance time, so it is the record. We confirm the first 10,000 orders in acceptance order, because that is what a fair first-come sale promised, and the remaining 1,800 are the ones that should never have been accepted.

Second, act fast and be honest. Before cancelling, ask the supplier or merchandising team whether some extra units can be sourced at the sale price; filling even part of the 1,800 turns complaints into goodwill. For the rest, cancel with an automatic full refund, or void the payment if it was only authorised, and add an alternative such as a discount code or first access to the next sale. Tell customers within hours, with a short explanation that admits our error. Silence until the order 'fails to ship' is what turns an incident into a public story.

4Designing the Gate So Losses Only Undersell

The principle is simple to state: the fast layer may lose units, but it must never be able to create them. Three changes give us that property.

First, winners are durable before we tell anyone they won. After the Redis script says OK, the attempt service writes the win to Kafka with acks=all and a replication factor of 3, or to the database, and only then replies 'won'. Winners are only 10,000 messages, so this costs little. Second, Redis never holds the whole stock. A durable stock row in PostgreSQL hands out batches, for example 500 units at a time, and Redis's counter only covers the current batch. Third, an order is confirmed only by a durable check on the order side: the order worker runs UPDATE sale SET sold = sold + 1 WHERE id = 42 AND sold < units, and refuses the order if it updates zero rows. That single row sees only a few thousand updates per second, at a pace the workers control, which PostgreSQL handles easily.

What a lost Redis write does
DesignLost decrement in failoverResult
Counter in Redis is the authorityreplica counter too highoversell (this incident)
Durable winners, recompute on failovercounter reset from durable countno oversell
Plus durable sold < units checkeven a wrong counter is caughtextra attempts rejected, never sold
Batch abandoned on failoverremaining batch units unusedsmall undersell, fixed by reconciling

Notice that every failure now goes the safe way. If Redis forgets a decrement, the durable check still stops the order. If Redis forgets a whole batch, those units simply are not sold until reconciliation returns them. Underselling 300 units for a minute is a mild problem; overselling 1,800 is a crisis.

5Pausing on Failover

When the stock gate fails over, we accept a short pause instead of guessing. The sequence is fixed and automated, so nobody has to improvise during a sale.

Step throughStock gate failover procedure
  1. Primary declared downSentinel or cluster failover starts
  2. Fence the old primaryit must not accept writes if it comes back
  3. Freeze attemptsAPI answers 'please wait', no new wins
  4. Recompute remaining stockunits minus durable winners
  5. Set the new counter and resumetypically 10 to 30 s of pause

5 steps in order. Play them, or step through them one at a time.

Fencing matters because a primary that was only cut off from the network, not dead, may still be accepting decrements from some clients, and two primaries handing out stock is the same bug again. The recompute uses the durable winners from the previous section, so it is correct by construction rather than by luck.

6Rehearsing the Failure

Finally, we test exactly what failed. Before each large sale, we run a rehearsal at sale-level load, around 100,000 attempts per second from synthetic users, and kill the Redis primary in the middle. We measure the pause, confirm that sold never exceeds units in the database, and check that the recomputed stock matches the durable winners. A failover path that has never been exercised under load is a hypothesis, not a design.

We keep the database constraints, the unique key on sale and user and the sold < units check, as the last guard even when everything above works. They are cheap, and they turn the next unknown bug into a rejected order rather than an oversold one.

7Follow-up questions

Would a strongly consistent store like etcd or a Raft-based Redis replace all of this?

A consensus-replicated store does not lose acknowledged writes on failover, so it would remove this specific failure. But it typically handles thousands to tens of thousands of writes per second, not hundreds of thousands, and adds latency to every attempt. Using Redis for fast rejections and a durable layer for the 10,000 wins gets both speed and safety.

Why confirm the first 10,000 by acceptance time instead of, say, by payment time?

Because the sale promised first come, first served, and acceptance time is when each customer was told they won. Choosing a different rule after the fact would look like we were picking winners. Whatever rule we use, we state it publicly.

Is Redis AOF with appendfsync always a fix?

AOF with fsync on every write makes a single node durable across restarts, but it does nothing for a failover to a replica that never received the writes. Durability on one machine and consistency across a failover are different problems.

8Conclusion

In this answer, we saw that asynchronous replication let a promoted replica forget thousands of acknowledged decrements, so the sale gave away units twice, and that WAIT narrows but does not close that gap. We handled the 1,800 extra orders by treating the order database as the record, confirming the first 10,000, sourcing extras where possible and cancelling the rest quickly with refunds and an apology. We then made the gate fail safe with durable winners, batches from durable stock and a durable sold-below-units check, added a fenced pause and recompute on failover, and rehearsed the failure under load.

What to say in the interviewAbout 90 seconds aloud

Open with

Redis replicates asynchronously, so the primary acknowledged decrements that had not reached the replica yet. When it died, the promoted replica had a counter that was higher than the true remaining stock, and it handed those units out again, which gave us 1,800 extra orders.

Then, if they want more

WAIT can shrink that window but it does not make failover strongly consistent.

For the customers now, I treat the order database as the record, confirm the first 10,000 orders by acceptance time, ask the supplier whether we can fill some of the extras, and cancel the rest quickly with automatic refunds or voided authorisations plus a goodwill offer, and we tell people within hours, admitting it was our mistake.

For the design, the rule is that the fast layer may lose units but never create them. Winners are written durably, Kafka with acks all or the database, before we tell anyone they won.

Redis only gets stock in batches from a durable stock row, and the order worker confirms each order with a durable update, sold plus one where sold is below units, so a wrong Redis counter is caught. On failover we pause: fence the old primary, freeze attempts, recompute remaining stock from durable winners, then resume, which costs maybe twenty seconds.

And we rehearse a primary failure at full sale load before every big sale, keeping the database constraints as the last guard.

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 "Why did the Redis failover cause overselling, how do you handle the 1,800 extra orders...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/da1d0847c6/