Design a Likes Counter
Understanding the Problem
❤️ What is a likes counter? Every post on a social network shows a few numbers under it: likes, views, sometimes upvotes and downvotes. A likes counter is the system that records each like, stops a user from liking the same post twice, and shows everyone a count that is close to right, quickly. It looks like
count = count + 1, and at small scale it is. The problem gets interesting when one post takes 20,000 likes in a second.
This question comes in many forms: "design a distributed counter," "design a global counter," "design views and likes on X," "design YouTube likes," or "design a voting system where the latest vote counts." They are the same problem. There's a record of who did what (a like, a vote, a view), and an aggregate number derived from those records that is read far more often than it is written. Votes add one twist, because a user can change their mind, and views add another, because there are a hundred times more of them and nobody needs to unlike a view. We'll build the design around likes and cover votes, views and the global version along the way.
Functional Requirements
We'll agree with our interviewer that we're designing the counting system, not the feed or the post itself.
Core Requirements
- Users should be able to like and unlike a post (or upvote, downvote and change their vote). One user counts at most once per post.
- Users should be able to see the like count on a post, and whether they have liked it themselves.
- Users should be able to see a view count on a post, which goes up every time the post is viewed.
Below the line (out of scope)
- The list of who liked a post ("liked by Alice and 3,201 others"). We'll store enough data to build it, but we won't design the page.
- Notifications to the author ("your post got 100 likes").
- Ranking feeds by engagement. That reads our counts but is a separate system (see /system-design/trending-hashtags).
- Detailed fraud and bot detection. We'll mention where it plugs in.
Non-Functional Requirements
Ask your interviewer how big this is and how exact the numbers have to be. The second question matters as much as the first. For our design, think of an X-sized product: 500M daily active users, 2B likes a day, and 50B post views a day.
Core Requirements
- The system should never lose a like or count one twice. The count can lag, but it must converge to the exact number of current likes.
- The system should show a new like in the count within a few seconds (eventual consistency is fine for everyone else), and the user who liked should see their own like immediately.
- The system should handle around 70k likes/sec and 1.5M views/sec at peak, including a single viral post taking tens of thousands of likes per second.
- The system should return counts with low latency (under 50ms) at well over 1M count reads/sec, since every post rendered in a feed shows its counts.
Below the line (out of scope)
- Strong, linearizable counts. Nobody can tell whether a post has 1,204,311 or 1,204,317 likes, and the product usually shows "1.2M" anyway.
- Exact view counts. We'll count views approximately and say so.
Here's the math. 2B likes a day is 2B / 86,400 ≈ 23k likes/sec on average, call it 70k/sec at peak. Views: 50B / 86,400 ≈ 580k/sec, so 1.5M/sec at peak. Count reads track views, because every post on screen shows its counts, so they also run at around a million per second.
Storage for the like records: a row is a user ID, a post ID and a timestamp, about 24 bytes of data and on the order of 100 bytes once you add keys, indexes and overhead. 2B a day × 100 bytes is roughly 200 GB/day, or about 70 TB a year before replication. That's a sharded database, not a single Postgres box. The counters themselves are tiny: a few numbers per post.
The key observation is that there are two different pieces of data here. The like record (Alice liked post 42) is the source of truth: it must be exact, durable and unique per user. The count is a derived, cached number that's read a million times a second and can be a few seconds stale. Once you say this out loud, most of the design follows: keep the records correct, and make the count cheap.