Live match: a goal in the final
8 million fans have the app open for the final. Every goal, card and substitution must reach all of them within a second, be saved, update the live stats and wake up fans who aren't looking. Right now the app checks the database every few seconds for news.
Play this levelPart of Bugpit Pro. It runs in your browser: build the design, press Run, and watch what breaks.
The lesson
Polling means every fan asks "anything new?" again and again. With 8 million fans that's a flood of questions, nearly all answered "no". A Realtime Gateway turns it around: fans hold a connection open, and the server speaks first.
One match event has several readers: save it, update the live stats, and push it to fans who aren't in the app. An Event Stream gives every reader every event, and each moves at its own pace.
Live stats, such as possession over the last 5 minutes, are running totals across many events. That's a Stream Processor's job. A worker only sees one event at a time.
The worst moment is full time. Phones lock, apps close, and a quarter of the connections drop and come back at once. Each gateway only lets so many people in a second, so spread the crowd over enough gateways to absorb it.
What you have to achieve
12,000 requests a second at the busiest hour, 6% of them writes.
- No broken or illegal connections
- Hold 8M open connections
- Use an Event Stream
- Use a Stream Processor
- Use Email & Push
- Every event reaches all 3 services
- No component is overloaded
- Workers clear the queue within the day
- Fewer than 0.1% of requests fail
- p99 response time under 300 ms
What you learn
A goal now reaches 8 million people without anyone asking for it, and three services read every event without knowing about each other. Adding a fourth, like betting odds, is one more wire on the stream.
Check yourself
Why a Realtime Gateway instead of the app asking for news every few seconds?
- Polling is less accurate
- 8 million fans asking again and again is a flood, almost all answered "no"
- Gateways are cheaper than servers
Show the answer
8 million fans asking again and again is a flood, almost all answered "no". With a live connection the server speaks first, and only when there's something to say.
One goal must be saved, counted in stats and pushed to fans. What gives every service every event?
- An Event Stream
- A Queue
- A Load Balancer
Show the answer
An Event Stream. A queue hands each event to one reader. A stream lets every reader follow the whole log.
"Possession over the last 5 minutes" needs which part?
- A Worker
- A Cache
- A Stream Processor
Show the answer
A Stream Processor. It's a running total across many events. A worker only sees one event at a time.