~learn.
0:00 / 1:32
Side quest 25 · Dream11

Ten million spots, 15,000 joins a second, never oversold

transcript

Ten million spots in one contest. 15,000 people grab one every second. Sell even one spot too many, and the contest is wrong.

A contest is just a number. Down by one per join. Never below zero. But 15,000 joins a second on one counter? Far too hot.

So split the ten million into 100 shards of 100,000. Each on its own node. A join picks a random shard. It takes a spot only if that shard is above zero. Shard empty? Try a few others. Then debit the wallet, with the same join id. A retry never charges twice.

But here's the catch. Why can't two joins grab the last spot? No shard ever goes below zero. And the shards add up to exactly ten million. So there is no shared last spot to fight over. No lock needed.

And if the wallet debit fails? The spot goes straight back to its shard.

Now the opposite problem. A contest with four spots, for 49 rupees. It fills in milliseconds. Everyone else sees contest full. So make it a template. When one copy fills, the next is already open.

One hot counter? Split it into shards. Just make sure the pieces add up to exactly the whole.

Full breakdown in the lecture.