Problem
500 phones go on sale at a fixed time, and over 100,000 requests reach checkout in the first 10 seconds. Exactly 500 must be sold, winners need an answer in milliseconds, and the slow parts of the system must not feel the crowd. It builds on High-Throughput Ticketing with a gateway and a real storefront.
How it works
- Admission. One atomic Lua script in Redis reserves stock. Winners get
202 Acceptedand a queue ticket; everyone else gets410 Gonejust as fast. - Only winners go deeper. Kafka, order processing, payment and PostgreSQL see roughly 500 requests, not 100,000. PostgreSQL decides every outcome, with a conditional
UPDATEas a second line of defence. - Isolation. NGINX sends checkout to three stateless Order Service replicas. Sessions, search and admin go to a separate site service, so they can never take capacity from checkout. Rate limiting and a bot filter sit at the edge.
- Storefront. A 47 kB Preact app (16.7 kB gzipped), served as static files, with a countdown, a buy button that locks on the first click, and status polling.
Five ways to sell 500 units to 5,000 buyers
| Strategy | Oversold | Seconds |
|---|---|---|
Naive SELECT then UPDATE |
4,500 | 3.87 |
SELECT ... FOR UPDATE |
0 | 4.25 |
Optimistic version column |
0 | 10.64 |
UPDATE ... WHERE stock > 0 |
0 | 0.55 |
| Redis Lua script | 0 | 0.02 |
