At 12:00:00 sharp, a new phone goes on sale at a launch price. There are 10,000 units. A million people have the page open, thumbs over the "Buy now" button. Within a few seconds, a hundred times more people try to buy than can possibly succeed.
Every other system in this course spreads load over the day. A flash sale does the opposite: it squeezes a day's worth of demand into a few seconds, on purpose, onto a single number that must never go below zero. You'll design it from a blank page. Try each step before reading on.
The brief
You're building the flash-sale feature for a large e-commerce site, in the style of Flipkart's big launches. A product is announced in advance with a sale time and a fixed stock. When the sale opens, users press "Buy now", get a reserved unit, and have a few minutes to pay. When stock runs out, everyone else sees "Sold out".
Who uses it?
- Real buyers, mostly on phones, many on slow networks, all pressing the button at once.
- Bots and resellers trying to grab as many units as they can.
- Operations setting up the sale and watching it live.
In scope: the sale page, the buy button, inventory, reservations and their expiry, order creation, and keeping bots out.
Out of scope: the payment gateway itself (see the Razorpay lab), delivery, the normal catalogue and cart, and returns.
Requirements
Functional
- Show the product page, with a countdown and a live "in stock" or "sold out" state.
- At sale time, let users try to buy. A successful try reserves one unit for that user.
- A reservation lasts 10 minutes. If the user pays, it becomes an order. If not, the unit goes back on sale.
- One unit per user for this sale.
- When stock hits zero, new tries get "Sold out" immediately.
Non-functional
- Never oversell. 10,000 units means at most 10,000 orders. This is the requirement that matters most.
- Never sell to the same user twice, even if they tap twice or the app retries.
- Stay up. The rest of the site (search, other products, other people's orders) must keep working while the sale runs.
- Fair enough. Earlier real users should generally beat later ones, and bots shouldn't win.
- Fast answers. Users should know within a few seconds whether they got one. A spinner for 30 seconds feels like a broken site.