Design a Live Page Viewer Count
Design the small badge that says “1,284 people are viewing this page” on every page of a large site, such as a product page, a news article or a ticket sale. The number goes up when someone opens the page and down when they close it, and everyone on the page sees it change within a few seconds without reloading. Listing pages, such as search results, show the count next to each item too.
At peak, 10 million viewers have a page open at the same time, spread over about 2 million pages. Most pages have one or two viewers, but a breaking story or a ticket drop can hold 1 million viewers on a single page. The hard parts are that people leave without saying goodbye (a closed laptop sends nothing), that one hot page must not melt one machine, and that pushing every change to every viewer can cost far more than counting does.
Good questions to ask first
Decide what you would assume for each before you look at ours.
- 1What counts as a viewer?
- 2How fresh must the number be?
- 3How exact must it be?
- 4How many viewers and pages?
- 5Do logged-out visitors count?
- 6Do we show who is viewing?
- 7Do we keep history, such as peak viewers per day?
- 8Which clients?
How to spend the 45 minutes
- 5minPart 1: Requirements, what counts as a viewer, freshness
- 5minPart 2: Estimates: connections, joins, pushes
- 5minPart 3: API: live connection and batch reads
- 15minPart 4: Architecture, from counter to gateway counting
- 15minPart 5: Deep dives: expiry, fan-out, hot pages, failures