Skip to content
SD-42 · Back to the write-up

Design a Flash Giveaway

Let the edge absorb the crowd, admit a steady 50,000 a second, count down 100 stock shards atomically, and issue coupons from a durable log.

Follow a request

Phones
Edge
Services
Data
App20M phones
Restaurant tillScans QR
CDNShell + status
Waiting roomAdmits 50K/s
GatewayToken + bot checks
Claim serversStateless × 100
Coupon workers8,000 / s
Stock shards100 × 60,000
Claim logDurable
Claims DBBy user id
PushCoupon ready
1user → gateway · POST /claims + tokenThe app sends the claim with its token. The gateway checks the session, the token’s signature and user id, and the per-account limit.1 / 6

The numbers, at a glance

Of every 20 people who try……about 6 get a burger
6M win14M see “all gone”
Requests per second at 12:00What each layer sees
The edge does nearly all the work50,000 of about 4M requests a second reach claim servers
That is why 100 claim servers and 100 small counters are enough.

A claim, in order

Happy path for an admitted user. A retry stops at step 4 with the same claim id.

App
Waiting room
Gateway
Claim server
Stock shard
Claim log
Coupon worker
1 join line
2 claim token (when admitted)
3 POST /claims + token
4 verified claim
5 claim script
6 won, claim_id
7 append claim (2 of 3)
8 ack
9 201 won
10 “You got one!”
11 claim → code + push
RequestResponseAsync, does not block

How to count down the stock

The decision this question is really about.

One SQL row
Too slow
50,000 claims/s
UPDATE … WHERE left > 0
row lock queue
+ Exact and simple.− About 2,000 a second.
One memory counter
Hot key
50,000 claims/s
DECR stock
one node
+ Atomic and fast.− One core, one failure point.
100 sharded counters
Our pick
hash(user_id) % 100
check keydecrementset key
one atomic script
+ 5M ops/s; failures touch 1%.− Rebalance the last burgers.

How it grows

Add a box only when a number forces you to. Highlighted boxes are new at each stage.

Stage 1 · ≈ 2,000 claims/sMake it work
App
Claim servers
SQL: stock + claims
Claim servers and one stock row in SQL.
Stage 2 · ≈ 1M attempts/sHold the crowd
App
CDNWaiting roomConfig
Gateway + bot checksClaim servers
SQL: stock + claims
CDN for static, waiting room admitting 50,000 a second.
Stage 3 · ≈ 50,000 claims/sCount fast, exactly
App
CDNWaiting roomConfig
Gateway + bot checksClaim servers
Stock shards × 100Claims DB
100 in-memory stock shards with a per-user key.
Stage 4 · ≈ 50,000 claims/s + 6M couponsIssue calmly
AppPush
CDNWaiting roomConfig
Gateway + bot checksClaim serversCoupon workers
Stock shards × 100Claim logClaims DBTills
Durable claim log, coupon workers at 8,000 a second, push and redemption.
Was this walkthrough useful?Your vote tells us what to improve next.