Designing a Redis-Backed System End to End
On this page
- The problem: a flash sale that must not oversell
- The design decisions, one at a time
- Preventing overselling with one atomic script
- Delivering every order through a stream
- Keeping the product page fast during the spike
- Persistence and the failover window
- Topology: Sentinel, not Cluster
- What a strong Redis design includes
- Check yourself
The problem: a flash sale that must not oversell
An online store runs a flash sale: 500 discounted headphones go on sale at noon. Expect 50,000 people refreshing the product page and thousands trying to buy in the first minute. Requirements:
- Never sell more than 500.
- The product page must stay fast under the spike.
- Each user can try to buy at most 5 times per minute.
- Every successful purchase must reach the order service, even if a worker crashes.
Sketch your design with Redis before reading on. Decide the data types, how stock is decremented, what's persisted, the topology, and the eviction policy.
The design decisions, one at a time
| Decision | Choice for the flash sale | Lesson |
|---|---|---|
| Data types | Stock as a string counter, orders as a stream, product as a cached string | Days 2, 3, 11 |
| Atomicity | Check stock and record the order in one script | Days 10, 12 |
| Rate limiting | Sliding window per user | Day 10 |
| Persistence | AOF everysec plus RDB on the sale instance | Day 5 |
| Topology | Primary + replicas + Sentinel; Cluster not needed | Days 6, 7, 8 |
| Caching | Cache-aside with stampede lock and TTL jitter for the product page | Day 9 |
| Eviction | noeviction for sale data, separate allkeys-lfu cache instance | Day 13 |
Preventing overselling with one atomic script
Reading the stock in the app and then decrementing it lets two buyers both see 1 left. Do the check, the decrement and the order record in one script:
SET stock:{sale:77} 500
EVAL "local left = tonumber(redis.call('GET', KEYS[1]) or '0')
if left <= 0 then return -1 end
redis.call('DECR', KEYS[1])
redis.call('XADD', KEYS[2], 'MAXLEN', '~', 100000, '*', 'user', ARGV[1])
return left - 1" 2 stock:{sale:77} orders:{sale:77} user:1042Testing with a stock of 3 and four buyers returned 2, 1, 0 and then -1 for the fourth, with exactly three entries in the stream. The {sale:77} hash tag keeps both keys in one slot, so the script would still work if you moved to Cluster later. In production, load it once as a Function (Day 12).
If the sale allows one pair per customer, enforce that in the same script: after the stock check, SADD the user to a buyers set with the same hash tag, and return without decrementing if SADD returns 0. A double-click then can't buy twice.
Delivering every order through a stream
The script appends each purchase to a stream. Order workers read it through a consumer group and acknowledge after the order is saved in the database. A crashed worker's pending entries get claimed by another (Day 11), and the order service ignores duplicate order ids, because delivery is at least once.
Keeping the product page fast during the spike
The page reads product data through cache-aside with a single-rebuilder lock (Day 9), so a cache expiry at noon doesn't send 50,000 requests to the database. The live "items left" number can come straight from the stock counter, which is one GET. If even that key gets too hot, cache it in the app for a second (Day 14).
Persistence and the failover window
Stock and orders exist nowhere else until workers save them, so the sale instance runs AOF with everysec and noeviction. Asynchronous replication can still lose the last moments of writes on failover (Day 6). For the stock counter that's a small oversell risk; reconcile against the order database after the sale, and decide with the business how much risk is acceptable.
Topology: Sentinel, not Cluster
500 counters and a stream are tiny. One primary with two replicas under Sentinel handles the load and gives failover in seconds. Cluster would add complexity for no benefit. Put the cache on a separate instance with allkeys-lfu, so a full cache never blocks sale writes.
What a strong Redis design includes
- An atomic decrement that can't oversell, with the order recorded in the same step.
- A durable hand-off to the order service with acknowledgments and idempotent processing.
- Stampede protection for the one page everyone loads at once.
- Persistence and eviction chosen per instance from what the data is.
- A stated answer for the failover window, rather than assuming it away.
- Monitoring: slow log, memory, evictions, stream pending count and replica lag (Day 14).
Check yourself
Two buyers can read the same remaining stock before either decrements, so both succeed and you oversell. The check and decrement must be one atomic step.
The entry stays pending and another worker claims and processes it again. The order service must treat the order id as idempotent and skip one it has already saved.
Under memory pressure Redis could evict the stock counter or the order stream. Keep data that must not disappear on a noeviction instance, separate from the cache.
The stock counters and orders for one sale fit easily on one node, and the atomic stock script touches keys that must live together. Sentinel gives automatic failover without Cluster's slot rules, cross-slot errors and extra operational work, so it matches the actual size of the problem.
Replication is asynchronous, so the last moments of stock decrements and stream entries acknowledged by the old primary may be missing on the promoted replica, which risks a small oversell. The design runs AOF with everysec and noeviction so nothing is evicted or silently dropped, processes orders idempotently, and reconciles stock against the order database after the sale. How much risk is acceptable is agreed with the business.