Grokking the System Design Interview, Volume II
Vote

0% completed

​

Step 1: System Definition

Image
YouTube Likes Counter: System Definition in System Design Interview II: Step 1: System Definition (Video)

YouTube no longer shows viewers how many people disliked a video, yet the dislike button still works and still records every click. The creator sees the total in their dashboard, and the recommendation system uses it. The number under a video is therefore only a summary. The real data is a much larger record of which user reacted to which item, and how.

In this case study, we design the backend that keeps both. It stores likes and dislikes on videos and on comments, keeps a running total for every item, and returns them fast enough that a page never waits.

A single counter would be trivial to build. The difficulty comes from popularity: when a video goes viral, millions of people update the same counter within a few minutes, and each expects the number to respond to their click.

The diagram below shows the four kinds of data the design stores.

Key entities and flow
Key entities and flow
  • User. A viewer with a unique userID, for example, user 123.
  • Video. An uploaded item with a unique videoID, for example, XYZ1, and its own like and dislike totals.
  • Comment. A reply under a video, with a unique commentID, for example, cmt99, and its own like total.
  • Reaction (Like or Dislike). The record that one user reacted to one video or comment, with its type and a timestamp. The user can change or remove it at any time.

The reaction is the most important entity, even though no viewer ever sees one. A total cannot tell the service whether user 123 has already liked XYZ1; only the reaction record can. The service needs that answer before it decides whether a click should move a count at all. Most later steps keep the totals consistent with the reactions while both change constantly.

Here is one minute on video XYZ1:

  1. Alice (user 123) likes the video. The system stores a reaction that links her to XYZ1 and raises the video's like total by one.
  2. She likes comment cmt99 under it. That is a second reaction and a second total.
  3. At the same moment, Bob dislikes the video. His request must not wait for hers, even though both change the same video's totals.
  4. Thirty seconds later, Alice switches her like to a dislike. One click now moves two totals, likes down by one and dislikes up by one. Only her reaction record tells the service that she had liked the video.

Multiply that minute by a few hundred million daily users, many of them on the same popular video, and you have the load this design must handle.

Reading Progress

0%


Vote for new content