Design a Like Counts at Scale
Design the like button for a large social app. A user taps the heart on a post, the heart turns red, and the number under the post goes up by one. Tapping again takes the like back. Every post in every feed shows its like count, the app remembers which posts *you* liked, and anyone can open the list of people who liked a post.
The app has 500 million daily users who make about 2 billion likes and unlikes a day, and they see a like count on 20 billion post views. The hard part is that the load is not spread out: when a famous person posts, one post can draw 20,000 likes and 100,000 count reads a second, all aimed at a single number. A user must never be counted twice, a count may lag by a few seconds but must never drift for good, and a like must never be lost once the heart turns red.
Good questions to ask first
Decide what you would assume for each before you look at ours.
- 1Can a user like a post more than once?
- 2How exact must the count be?
- 3Must the user see their own like at once?
- 4Do we need the list of who liked a post?
- 5How much traffic?
- 6How hot can one post get?
- 7Do likes on comments and stories work the same way?
- 8Can likes be anonymous or hidden?
How to spend the 45 minutes
- 5minPart 1: Requirements and how exact the count must be
- 5minPart 2: Estimates, especially the hottest post
- 5minPart 3: API
- 15minPart 4: Architecture, stage by stage
- 15minPart 5: Hot keys, exactly-once, recounts