Preparing your design workspace

Loading the problem and your latest saved attempt…

Speed Up Daily Trending Videos on a Viewing Log

Make a per-day most-watched query fast on an 800-million-row PostgreSQL viewing table without slowing the per-user history query or the insert rate.

Problem statement

A video streaming service stores every viewing in one PostgreSQL table. Change the database and its indexes so that the daily trending query becomes fast, while the per-user history query stays fast and inserts keep their current throughput.

The table today. Each row is one viewing with `user_id`, `video_id`, `watch_starttime` and `watch_duration`. The primary key, with its index, is `(user_id, watch_starttime)`. The table holds about 800 million rows and takes about 5,000 writes per second.

The two queries that matter. - *Recent history:* the last 10 videos one user watched. It takes about 12 ms today. - *Daily trending:* the most-watched videos for a given day. It times out today.

For this exercise, assume: - The primary key is the only index today. - Each write inserts one finished viewing, when the viewing ends. Rows are never updated or deleted. A viewing can be written up to 12 hours after it started; writers reject anything older.

- Daily trending returns the 50 videos with the most viewings that started on one calendar day in UTC, with each video's count, ties broken by the lower `video_id`. The day can be today or any earlier day. - All history stays in the database, so the table keeps growing.

- PostgreSQL 14 or later stays the only data store. You may add tables, columns, indexes, partitions, scheduled jobs and read replicas, but every change must be made while the service keeps running.

Out of scope: recommendations, rules for what counts as a valid view, and moving the data to another kind of database.

What to build

  1. The service records each finished viewing with one insert, at the current rate and without waiting on anything new.
  2. A user can see the last 10 videos they watched, newest first.
  3. Anyone can see the 50 most-watched videos for a chosen day, with each video's number of viewings.
  4. Each trending answer says whether the day's counts are final or still changing, and up to what time they include viewings.

At scale

  • The viewing table holds about 800 million rows and takes about 5,000 writes per second. At that rate one full day adds up to about 430 million rows.
  • For this exercise, assume up to about 1 million different videos are watched on one day.
  • For this exercise, assume daily trending must answer within 100 ms for 95% of requests (p95), and today's ranking may be at most 5 minutes behind the writes.
  • For this exercise, assume recent history must stay within about 20 ms at p95, and inserts must keep sustaining 5,000 per second with no higher latency than today.
  • For this exercise, assume a viewing is written up to 12 hours after it starts, so a day's counts can still change until 12 hours after the day ends.