SYSTEM DESIGN SKETCHBOOK / PRACTICE SHEET / NO. 33
Design like counts for very popular posts
Every post shows a like count and a heart that is filled in if you liked it. Most posts collect a handful of likes, but when a very popular account posts, hundreds of thousands of people tap like within a minute, and every one of the millions of people viewing the post sees the count.
Requirements
- Like and unlike a post; a user can like a given post at most once.
- Show the like count on every post view, and whether the viewer has liked it.
- The viewer's own like or unlike is reflected immediately on their screen.
- The displayed count must converge to the true number of current likers.
Constraints and assumptions
- 2 billion like or unlike actions a day: about 23,000 per second on average and 100,000 at peak.
- A single post can receive 50,000 likes per second for several minutes.
- Count reads run about 50 times higher than likes: up to 1 million per second at peak, with p99 under 20 ms.
- A count may lag by a few seconds. Above 10,000 it is displayed rounded (12.4K), so small drift is invisible; below that it should be exact once settled.
Out of scope for the first sketch
Pushing count changes live to open screens · The full list of who liked a post · Reactions other than like
Draw your design
Rough math and notes
SYSTEM DESIGN SKETCHBOOK / FOLLOW-UPS
Design like counts for very popular posts
Finish your first sketch before answering these. Cross out freely.
- One post gets 50,000 likes per second. What is the hottest row or key in your design, and how do you cool it down?
- Where does the filled-in heart come from, and how do you make it fast for a feed of 20 posts?
- A user taps like, unlike, like in quick succession on a flaky connection, and the requests arrive out of order. What state do they end up in, and what does the count say?
- The aggregation worker crashes after applying a batch but before recording its progress. What happens to the counts?
- The displayed count says 1,204,331 but a full recount finds 1,204,350 likers. Is that acceptable, and how would you reconcile it?
Compare afterwards: worked approaches and reading at sketchbook /questions/like-counter/review