SYSTEM DESIGN SKETCHBOOK / PRACTICE SHEET / NO. 13
Design most-shared articles over sliding time windows
A news reading app lets people share articles to friends and other apps. The home page has a "most shared" box with three tabs: last 5 minutes, last hour and last 24 hours. Editors watch the 5-minute tab to spot breaking stories, so it has to move quickly.
Requirements
- Record a share event for an article from a signed-in user.
- Return the top 10 articles by share count for each of three sliding windows: 5 minutes, 1 hour and 24 hours.
- Count a given user sharing the same article at most once per 24 hours.
- Lists update as shares happen; an article falls out of a window as its shares age past the window.
Constraints and assumptions
- 10,000 shares per second on average, 50,000 per second at peak.
- About 10 million distinct articles are shared in a day; popularity is extremely skewed.
- The box is read about 100,000 times per second and may be up to 10 seconds stale.
- Window edges can be approximate by a few seconds, but the top 10 must not include an article that clearly does not belong.
Out of scope for the first sketch
Personalized rankings · Spam classification models · The share delivery itself (sending to friends)
Draw your design
Rough math and notes
SYSTEM DESIGN SKETCHBOOK / FOLLOW-UPS
Design most-shared articles over sliding time windows
Finish your first sketch before answering these. Cross out freely.
- For the 5-minute window, what state do you keep so that a share stops counting exactly when it is 5 minutes old?
- You split counting across 16 workers. When is merging each worker's top 10 exact, and when is it wrong?
- One article goes viral and takes 40% of all shares. What happens to the worker that owns it?
- A counting worker crashes and restarts. How do you rebuild a correct 24-hour window without double-counting?
- Product wants the same three lists for each of 200 countries. Does exact counting still fit, or do you switch to an approximate method?
Compare afterwards: worked approaches and reading at sketchbook /questions/most-shared-articles/review