Design TikTok
TikTok serves a feed of short clips, one after another, for as long as a viewer keeps watching. The order of the clips comes from the ranking system.
The ask
A viewer opens the app and a clip plays at once. Swiping moves to the next clip, with no search and no loading pause.
Creators upload clips with a caption and sounds. Uploads are few beside views.
The feed feels endless and personal. Each viewer's next clips differ.
Likes, follows and comments sit around the feed. The feed is the core, and the rest supports it.
Estimate the scale
Views reach 10,000,000,000 a day. 10,000,000,000 views / 86,400 seconds = about 115,741 views per second.
Uploads reach 1,000,000 a day. 1,000,000 uploads / 86,400 seconds = about 12 uploads per second.
Views outnumber uploads by a wide margin. The design is a serving system with a small intake.
One uploaded clip file takes 5 MB in this design. 1,000,000 uploads x 5 MB = 5,000,000 MB of new clips a day.
That is 5 TB of new video a day.
API and data
A clip record holds the creator ID, the caption and the file locations. It also holds the length and the upload state.
The feed operation takes a viewer ID and a position marker. It returns the next small set of clip IDs, in the ranking system's order.
A watch event carries the viewer, the clip and the watched time. Watch events are inputs to the ranking system.
The upload operation takes the clip file and its caption. It returns a clip ID before processing finishes.
A follow operation links a viewer to a creator. Follows shape the followed list, not the main feed order.
High-level design
The feed service answers feed requests. It asks the ranking system for ordered candidates, then turns IDs into playable clips.
The ranking system is a named input to the feed. It takes the viewer and recent watch signals, and returns clips in order.
Clip files live in blob storage and serve through a CDN. The feed service never moves video bytes itself.
Upload processing runs behind a queue. Transcoding and checks finish after the upload answers.
How watching runs
The feed is a pull feed. The app asks for the next set when the current set runs low.
Each answer carries a few clips, not one. One round trip serves several swipes.
The app prefetches the next clips while one plays. A swipe then starts sound and picture at once.
Feed answers cache for seconds, not minutes. A cached answer that is slightly stale still plays well.
A viewer who stops mid-clip still sends the watch event. The watched time, not the swipe alone, is the signal.
Deep dive: serving the view firehose
About 115,741 views start every second. Most views are of clips that are already popular somewhere.
Popular clips sit at the edges near their viewers. A new clip spreads to edges as its views grow.
Watch events leave the play path. They stream to storage and to the ranking system as signals, after the clip starts.
A failed feed answer falls back to a cached set. Popular clips in a stale order beat an empty screen.
Deep dive: the upload path
Only about 12 uploads arrive per second. The intake can afford slow, careful work on each one.
Transcoding makes several sizes of each clip. Phones on weak connections play a smaller size without stopping.
A moderation component checks each clip before wide release. It takes the clip and returns a verdict, clear or held.
A held clip stays visible to its creator only. The creator hears the reason and can appeal.
Freshness and the new clip
A new clip has no watch history. The ranking system decides its first chances, and the feed only serves what it returns.
Fresh clips must reach edges fast when they spread. A miss at the edge fetches from the origin and caches on the way back.
Follows give a second, simpler path. A viewer's followed creators list reads in time order, apart from the main feed.
Evaluation and trade-offs
Pull trades a little staleness for control. The viewer asks when ready, so the service never pushes clips nobody reaches.
Serving and ordering stay apart. The feed scales on copies and caches, and the ranking system scales on its own.
Careful upload checks trade speed for safety. At 12 uploads per second, the design can pay that cost.
Prefetch trades wasted bytes for instant swipes. Some prefetched clips are never watched, and that waste is the price of no pauses.
TikTok in numbers
| Item | Figure |
|---|---|
| Views a day | 10,000,000,000 |
| Views a second | 115,741 |
| Uploads a day | 1,000,000 |
| Uploads a second | 12 |
| Average clip | 30 seconds |
| One clip file | 5 MB |
| New clip files a day | 5,000,000 MB |
Exercise
Where does the order of clips in the feed come from?