Practice: Design a Page View Counter
View counter inspired by Reddit and YouTube view counts: record millions of views without hammering one database row, using in-memory counters, a durable event queue and batched writes.
Build this system yourself on the canvas below. Add components, connect them, and run traffic simulations to see where your design holds up and where it breaks.
Functional requirements
- Record a view for a page or video, ignoring obvious repeats and bots
- Show the current view count for a page, quickly and cheaply
- Count views in memory and write them to the database in batches, so one popular page does not hammer a single database row
- Keep views in a durable queue so a crash loses at most the last few seconds of counts
- Never let a count go backwards, even after a restart or a retry
- Throttle bots and repeated views from the same client
Non-functional requirements
- Recording a view < 50ms p99; reading a count < 50ms p99 from the counter cache
- Handle 10K requests per second at peak, about 90% views; a single viral page can take 5K views per second
- Counts may lag by up to a minute but never go backwards
- 99.9% of views are eventually counted (at-least-once delivery with de-duplication)
- Database writes are batched, so total write load grows with pages, not with views
- Scale to 1B views per day
- 99.9% availability for recording; reading a count can fall back to the last known total