System Design: Recommendation System (Walnut Tube Recommendations: Home Feed and Up Next)
We design the recommendations of a large video app: the home feed and the Up next list. Our version is called Walnut Tube Recommendations. The facts its models read come from Walnut Feature Store, our companion post.
As in any system design interview, the numbers are assumptions for planning. Every number here is our own assumption, and the design is our own. All times are Denver time.
How to read this post. It takes about 80 minutes in full, so it is built to be read in parts:
1. Requirements
1.1 What we are building
It is Thursday 10 Dec. Jordan Lee in Denver opens the Walnut Tube app at 9:00 p.m. She sees two lists:
- The home feed: 20 videos picked for her, shown when she opens the app.
- Up next: a list of 20 videos beside the player, shown when she plays a video. The first one plays when her video ends.
In this post, a page means one list of 20 videos that we build and send back: one home feed or one Up next list. Each time the app asks for a page, we build it fresh for that viewer, in under 200 ms of server time.
Each video has two buttons for saying no:
- Not interested hides that one video. If the video's topic is one she rarely watches, it also hides that topic for 30 days.
- Do not recommend channel hides every video from that channel.
Jordan mostly watches basketball highlights. Yesterday evening she said no twice:
- She tapped "Not interested" on a pasta video, because she wants basketball, not recipes.
- A minute later she blocked Starwatch Daily, a gossip channel, because she does not like celebrity gossip.
Tonight her home feed shows a Hoops Recap video first, video V-1042, and no pasta or Starwatch Daily videos. Then her evening goes like this:
- She plays the Hoops Recap video.
- When it ends at 9:12, Up next plays a basketball analysis video. A new Up next list appears beside it.
- At 9:13 that list shows a sports betting video. She does not bet, so she taps "Not interested" on it.
- At 9:13:40 she opens the home feed again. There is no betting video on it.
1.2 Words we use
These words come up on every page of this post. Here is what each one means here.
Pages and models
| Word | Meaning |
|---|---|
| Page | One list of 20 videos we build and send back: one home feed or one Up next list. |
| Slot | A position on the page, 1 to 20. Slot 1 is the top. |
| Impression | One video shown on screen once. |
| Session | One sitting with the app. Two watches less than 30 minutes apart are in the same session. |
| Model | A program that learned from past data to make a guess, such as "how long will Jordan watch this video?" |
| Training | Running a model over millions of past examples, such as "this video was shown, and she watched 12 minutes", and adjusting it until its guesses match what really happened. |
| Feature | One fact a model reads, such as "Jordan's basketball minutes this week". |
| Score | One number that says how good a video is for Jordan right now. Higher is better. |
| Candidate | A video that might suit her, found in the first step. About 1,100 per page. |
| Funnel | The steps that cut 300 million videos to 20: find candidates, cut to 300, order them, arrange 20. |
| Pre-ranker | A small, quick model that cuts the candidates left after the filter, about 1,000, to 300. It learned by copying the Ranker's order. |
| Ranker | A large, careful model on GPUs. It predicts six things about each of the 300, such as minutes watched. |
Towers and vectors
| Word | Meaning |
|---|---|
| Vector | A list of numbers. Here, 128 numbers that describe a viewer's taste or a video. Interviewers also say embedding. |
| User tower | A small model that turns what Jordan watched, searched and tapped into 128 numbers: her taste vector. It runs on every request; a nightly copy is also stored in the Feature Store. |
| Video tower | A small model that turns one video's title, channel and other facts into 128 numbers: its video vector. It runs each night, inside the Index builder. Also called the item or product tower. |
| Two towers | The User tower and the Video tower, trained together so that a video she will enjoy gets a vector close to hers. |
| Vector version | The name of one night's towers, such as v57 for Thursday. Numbers from v57 mean nothing next to numbers from v56. |
| Fresh tower | A simpler video tower for uploads too new to have plays. |
| ANN search | The job of finding the video vectors closest to a taste vector fast, without checking all 300 million. ANN stands for approximate nearest neighbor. It may miss a few of the truly closest, which is why it is called approximate. |
| HNSW | The method we use for ANN search. It links each video vector to its nearest vectors, in layers, so a search can hop toward the closest ones. |
| FAISS | A free code library that builds and searches HNSW. Our own programs call it; it is not a service or a database. |
| ANN index | The data that HNSW builds: the 300 million video vectors plus their links. It is saved each night as 8 shard files. It is not a model, and it is not a search engine such as Elasticsearch. |
| ANN service | Our own program, running on many machines. Each machine loads one shard file into memory with FAISS and answers searches from memory. The index is the data; the service is the program and machines around it. |
| Vector database | A ready-made product, such as Pinecone or Milvus, that bundles all of the above. We build the same thing ourselves from FAISS; the trade-offs say why. |
Storage and serving
| Word | Meaning |
|---|---|
| Key-value store | A store that returns one key's row, such as viewer U-5120, in about a millisecond. It spreads keys over many machines. DynamoDB and Cassandra are examples. |
| Feature Store | Walnut Feature Store, our companion post. It keeps ready-made features about viewers and videos, and the nightly vectors. Our service and the Ranker read it. |
| Event log | A log that keeps every event in order, such as Kafka. Many jobs read the same events, each at its own pace. |
| Pod | One running copy of a program. |
| Shard | One slice of a large dataset, held by one machine. Data too big for one machine is cut into shards. The ANN index has 8 shards: 8 different pieces, each with different videos. |
| Replica | An identical copy of a shard on another machine, so more requests can be answered and one crash loses nothing. Each ANN shard has many replicas at peak. |
| GPU | A chip that does thousands of multiplications at once. Large models run much faster on GPUs than on normal processors. |
| Region | One data-center site that can serve a viewer on its own. We run three: US West, US East and Europe. |
| Zone | One building inside a region, with its own power and network. Each region has 3, so one zone can fail alone. |
IDs in this post
Every ID below is made up for Jordan's story. The start of each ID says what kind of thing it names.
| ID | What it names | Example |
|---|---|---|
| U-5120 | A viewer ID: one Walnut Tube account | Jordan |
| V-1042 | A video ID: one video | V-1042, the Hoops Recap highlights she plays tonight |
| C-1180 | A channel ID: one channel | C-1180, Hoops Recap; C-4402, Starwatch Daily |
| rq_9f31c2 | A request ID: one page the app asked for | Her 9:00 p.m. home feed |
| im_9f31c2_01 | An impression ID: one video shown in one slot of one page | V-1042 in slot 1 of that page |
| ev_5c71 | An event ID: one action sent by the app | Her 9:13 "Not interested" tap |
| v57 | A version name: one night's models | v57, the models built on Thursday morning |
1.3 Every kind of request and change Walnut Tube handles
The story follows Jordan's home feed and one "no". Walnut Tube handles much more than that.
Each row names the part that handles it, and the other team or outside company that takes part. Each part is explained in the stage of the design that adds it.
| Kind | What it is | How big or how often | Which part handles it | Other team or outside company |
|---|---|---|---|---|
| Home feed | 20 videos picked for her when she opens the app, and the next 20 when she scrolls. Jordan's 9:00 p.m. page is this kind | 60,000 requests a second on average, 180,000 at the evening peak; p99 under 200 ms | The Recommendation service runs the funnel: find candidates, filter, rank, arrange | The subscriptions and videos tables, other teams'. The Feature Store, our companion post |
| Up next | 20 videos beside the player, built from the video she is playing | 3.5 billion plays a day: 40,000 a second on average, 120,000 at peak | The same funnel, starting from that video. Its watched-together neighbors lead, and the Ranker scores 200, not 300 | The same as the home feed |
| Signed-out or brand-new viewer | A page with no history to use | The same call, with no token or no saved rows | The Trending store gives her country's most popular videos | None |
| "Not interested" and "Do not recommend channel" | Her "no" taps, and "undo" | Saved within 5 seconds at p99, then left out on every device | The Event collector, the Event log and the Feedback job save it. The filter checks every page twice | None |
| Plays and watch progress | What she plays, and how much of it she watches | 20 billion actions a day | Stream jobs keep her seen list and the trending lists | The Feature Store's own stream jobs read the same events |
| Impressions | One video shown on screen once, with its slot | 50 billion a day | The page record, the Label joiner and the Data lake | The Feature Store's Serving log keeps the features each page read |
| A new upload | A video too new to have plays, such as video V-9120 at 6:00 p.m. | Can be recommended within about 15 minutes: 5 for its numbers, up to 10 for the fresh index | The Fresh tower, the fresh index in the ANN service, the Channel uploads store, and slot 6 | The upload service, another team's, tells us about each upload |
| Trending | The most played videos in each country in the last hour | Rewritten every minute; the top 500 per country | Stream jobs and the Trending store | None |
| Live features | Counts such as her watch minutes in the last hour | Updated within about 2 seconds | Not ours: the Feature Store's own stream jobs | The Feature Store |
| Nightly training and the switch | New towers, a new Pre-ranker and Ranker, a new index and a new graph | Every night. Training starts at 4:00 a.m. The towers, the index and the Pre-ranker go live together at 7:00 a.m.; the new Ranker goes live in steps | The Graph builder, the Training pipeline, the model registry and the Index builder | The Feature Store's Training data service builds the training rows |
| A/B tests | Some viewers get a new version, and the rest keep the old one | At least 14 days per test | Every pod hashes the viewer ID to find her test group, with no lookup | None |
| Safety and country blocks | A video the safety team marked unsafe, or one not allowed in her country | Checked on every page, before ranking and again just before the reply | The filter and the final check | The videos table, another team's, holds each video's safety mark and countries |
| Bots and fake plays | Bots and broken apps that call too often, and plays meant to push a video up | On every call and every play | The API gateway's rate limit. Trending skips accounts flagged as bots, and the graph skips accounts under 30 days old | None |
| Kids, Europe and deleting an account | Rarely asked. The follow-ups give one line each | Deletion within 30 days | A kids index; European viewers' data stays in Europe | None |
At peak the two kinds of page add up to 180,000 + 120,000 = 300,000 requests each second. The events add up to 20 billion + 50 billion = 70 billion a day. That is 810,000 a second on average, and 2.4 million at peak.
This post follows Jordan's home feed and her "no" in depth. The other kinds use the same parts.
1.4 Functional requirements
- Return a home feed of 20 videos for a viewer, and the next 20 when she scrolls.
- Return an Up next list of 20 videos for the video she is playing.
- Never show a video she said no to, a video from a channel she blocked, or a video she already watched.
- A new "no" works on her very next page, a few seconds later.
- A new upload can be recommended within minutes.
- A new or signed-out viewer, with no history, gets the most popular videos in her country.
Out of scope: search, ads and comments. Uploading and streaming the videos are also left out.
1.5 Non-functional requirements
- Fast. A page is ready in under 200 ms for 99 of every 100 requests. This is written p99 under 200 ms.
- We measure in our servers, from the moment the request arrives to the reply.
- The phone network adds its own time on top.
- Her "no" is a promise. After a tap is saved (within 5 seconds at p99), every page on every device leaves it out. Before that, the phone that took the tap applies it itself.
- Always a page. If a part fails, we use a fallback: a simpler way to build the page, such as popular videos only. A page is returned for 99.95% of requests; a fallback page counts.
- Enjoyed minutes, not clicks. We aim for minutes she enjoys, not taps.
- A video with a shocking picture and nothing inside gets many taps and makes viewers leave. That is clickbait, and we must not reward it.
- We measure success as minutes from videos viewers enjoyed, and how many viewers come back within 7 days.
- "Enjoyed" comes from a one-question survey that the app shows after some videos: "How was this video?", answered with 1 to 5 stars. A 4 or 5 counts as enjoyed.
1.6 Back of the envelope
These are rough sums, the kind a candidate does at a whiteboard in a few minutes. Every number is our own assumption.
| Number | How we get it | Result | What it decides |
|---|---|---|---|
| Pages | Home: we plan for 60,000 a second. 60,000 × 86,400 s ≈ 5.2 billion a day: with 300 million viewers a day, out of 500 million accounts, about 17 each. Up next: 3.5 billion plays a day ÷ 86,400 s ≈ 40,000 a second. Evenings run 3 times the average. The app also holds 300 million videos. That is a different 300 million from the viewers | 180,000 home + 120,000 Up next = 300,000 a second at peak, under 200 ms each | Every part on the page path must carry this. Up next is almost as big as home, so both share one design |
| Score every video? | 300 million videos × 1 microsecond | 300 seconds | Impossible. Find about 1,100 candidates first: the funnel |
| Search index size | 300 million videos × 600 bytes (128 numbers × 2 bytes, plus links to nearby videos) | 180 GB | Too big for one machine: 8 shards of 22.5 GB |
| Index searches | Every page asks all 8 shards: 300,000 × 8 | 2.4 million shard searches a second | The largest load, bottleneck 1. Each shard has many identical copies, and any copy can answer |
| Ranker work | Home ranks 300 videos per page. Up next ranks 200, because it starts from one video and fewer candidates fit it. 180,000 × 300 + 120,000 × 200 | 78 million videos a second | Bottleneck 2, on GPUs. This is why the Pre-ranker cuts the list to 300 before the Ranker |
| Watched-together graph | 100 million videos × 50 neighbors × 12 bytes. A neighbor is a video often watched in the same session. Only about 100 million of the 300 million videos are watched enough to have neighbors | 60 GB | Fits one machine's memory: whole graph on each machine, no network hop |
| Block lists | 500 million viewers × 0.5 KB | 250 GB | Key-value store by viewer, with a Redis cache in front. Redis keeps data in memory, so it is fast |
| Logs | 20 billion actions × 300 B = 6 TB, + 50 billion impressions × 100 B = 5 TB, + 17 TB of features. The Feature Store's Serving log keeps the features each page's models read, so training later sees exactly the same values | 28 TB a day, about 5.6 TB once compressed, 5 times smaller; 810,000 events a second, 2.4 million at peak | An Event log, and cheap file storage for training |
What these numbers tell us: the hard parts are the index searches and the Ranker's work, both on the page path, 300,000 times a second. Storage is large but ordinary.