Flash sale
0-day streakReview

Your progress

Level 1

0 XP100 XP to level 2
0day streak
0best streak
0topics started
0cards reviewed
    Review cards

    Progress is stored only in this browser. No account is required.

    Design a flash sale: the 60-second story

    Design a flash sale

    Noon. 10,000 phones at ₹9,999. Five million people waiting.

    Almost everyone must hear “sold out”, cheaply. Exactly 10,000 hear yes, never 10,001, and the rest of the store mustn’t notice.

    400

    shoppers for every phone

    Assume 5 million on the page and 4 million tapping Buy in two seconds, 2 million claims a second for 10,000 phones. 99.75% must lose.

    Why one database row sells only 500 a second

    “Subtract one if any are left” never oversells, but locks the row through a 2 ms commit. 10,000 phones take 20 seconds while 40 million requests arrive.

    Move the counter to Redis. One key, or eight?

    Bad: One key

    One script, one thread: about 40,000 claims a second.

    Good: Eight buckets of 1,250

    ≈ 320,000 a second. Routed by user, so one script checks one-per-customer.

    4 million taps in two seconds, 10,000 phones. A race rewards bots and fast networks.

    The trick

    Draw lots at the edge. Each pass gets one fixed random number; about 11,000 of the lowest get in.

    5 million on the page. 11,000 reach the stock.

    Each layer answers most of what reaches it. Every “no” comes from the edge in milliseconds; the counter sees about as many attempts as units.

    Redis says yes; the database decides

    1. A script on your bucket says yes
    2. Wait for a replica and Kafka
    3. Only then tell you “pay by 12:10”
    4. Orders at 1,000/s; the database caps each bucket

    If 15% don’t pay, 1,500 phones return at 12:10

    Deadlines collide, so they return within two seconds, a second flash sale. Send them to the waitlist; only scripts still hammer the open counter.

    Remember

    Say no at the edge, yes in one script on one bucket, and make the yes real behind a queue, with the database as referee.