SYSTEM DESIGN SKETCHBOOK / PRACTICE SHEET / NO. 49
Design a live viewer count for a page
A product page, article or listing shows a small badge: "1,240 people are viewing this". The number should feel alive: it rises as people arrive and falls as they leave, including people who just close their laptop or lose signal without ever telling the server.
Requirements
- Show an approximate count of people currently viewing a page.
- The count drops when viewers leave, including when they disappear without any goodbye signal.
- While the page is open, the displayed count refreshes about every 15 seconds.
- A person with the same page open in several tabs counts once.
Constraints and assumptions
- 50 million concurrent viewers spread across 20 million pages; most pages have 0 to 5 viewers, the most popular has 3 million.
- A viewer who leaves should stop counting within 60 seconds.
- Counts above 1,000 may be off by 2% and are shown rounded; counts under 100 should be exact or off by one.
- Nothing about who is viewing is shown or stored beyond what counting needs.
Out of scope for the first sketch
Showing who the viewers are · Historical traffic analytics · Pushing updates over a persistent connection (polling is fine to start)
Draw your design
Rough math and notes
SYSTEM DESIGN SKETCHBOOK / FOLLOW-UPS
Design a live viewer count for a page
Finish your first sketch before answering these. Cross out freely.
- Why not keep one counter per page that goes up by one when a page opens and down by one when it closes?
- A viewer closes their laptop lid without leaving the page. When do they stop counting, and why then?
- The same person has the page open in three tabs. How do you count them once, and what does that cost?
- One page has 3 million viewers. Which key is hot, and how do you spread it?
- A counting node restarts and loses everything in memory. What do people see?
Compare afterwards: worked approaches and reading at sketchbook /questions/page-viewer-count/review