AlgoViz

Design a Flash Sale System

hard 55 min

Sell 10,000 units when 10 million people come at the same second.

Video lesson · Chapter 1 of 5

Winning entry does not win the final pair of shoes

0:00 / 3:52

Theory

Read or listen first. About 1 min.

What it is

A flash sale offers a small number of units at a fixed moment. Far more people want one than can have one.

A flash sale system shows the page, lines people up, lets them in at a speed the checkout can bear, holds a unit while each one pays, and puts unpaid units back on sale.

Why it exists

An ordinary shop is built for its average day. Here almost the whole day's traffic comes in one minute, and almost everybody must be refused. A refusal therefore has to be the cheapest thing the system does.

Two promises pull against each other: be fast for millions, and be exact about the last unit. The design separates them. The edge is fast, the stock is exact, and a narrow gate stands between.

A short history

  1. 2001

    Vente-Privée, now Veepee, is founded in France and sells stock in short private sales.

  2. 2004

    Woot is founded in the United States.

  3. 2009

    Alibaba begins to use 11 November, Singles' Day, as a one-day shopping event.

  4. 2021

    Cloudflare announces Waiting Room, first offered through Project Fair Shot to vaccine sign-up sites.

Words to know

Overselling
Accepting more orders than there are units.
Hold
A unit kept for one person until a fixed time. Also called a reservation.
Waiting room
A page where people wait outside the shop until they are let in.
Cutoff
The published number up to which people may enter.
Ticket
A signed proof of a person's place in the waiting room.
Conditional update
A change that happens only if a condition still holds, checked in the same step.
Hot row
One record that every request wants to change.

Solve it like an interview

New to it? Start with Approach. Revising? Go straight to the mock.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Absorb. The edge serves the page and the waiting board

Understand it

One hundred balloons
100Before the start

Make the crowd small first. Then counting is easy.

Scope

What are we building?

A sale page with a countdown, a waiting room, a way to hold one unit, a quick checkout and an order. 10,000 units, one per person, at a fixed second.

  • Countdown

    One clock for everyone: the server's.

  • Fair waiting

    One ticket per account, a draw at the start, honest odds.

  • Exact stock

    10,000 units are 10,000 orders or fewer, never more.

  • Quick checkout

    Five minutes to pay. Unpaid units return.

The hard part

Ten million to ten thousand

999 of 1,000 people will not get a unit. If each of them reaches the database to learn that, the database fails for the one who would. So each layer answers as many people as it can and passes on as few as possible.

A funnel, not a wall

Assumptions: 10 million people, 20 requests each in the first minute, peak three times the average.

The finished system

Where the design ends up

This is the last version. The Design tab starts from one service and one database, and shows what forces each extra box.

The whole system

Buyers load the page and the board file from a CDN. Their join requests pass bot checks to the waiting room, which signs tickets. Admitted buyers reach the reserve service, which asks Redis for a quick no and then PostgreSQL for a hold. A payment service moves the hold and calls the card provider. Saved changes flow through Kafka to workers. A sweeper ends old holds in PostgreSQL, and an admission worker reads PostgreSQL and writes the board file to the CDN.
Blue is the crowd, pink is the admitted few, orange is not waited for.

Summary

The whole topic in one minute.

The crowd never touches the stock. Static files and one small board file, both from the CDN, serve millions. Signed tickets and a moving cutoff let in about 100 people a second.

At that speed one database can be exact: a conditional update takes a unit and inserts a hold with an end time. Payment may be slow, repeated or unknown, because the state of the hold decides and not the timing of a message.

  • Refusing must be the cheapest path.
  • Millions read one small file instead of asking millions of questions.
  • Admission speed equals the slowest step of checkout, usually payment.
  • A unit is taken by one conditional change, and a hold is a row with an end time.
  • An expiring key does not give stock back. Something must do it, together with the state.

Sources