System Design
Learn System Design
Introduction to System Design
How to Learn System Design?
Key Characteristics of Distributed Systems
Scalability
Availability
Latency and Performance
Concurrency and Coordination
Monitoring and Observability
Resilience and Error Handling
Fault Tolerance vs. High Availability
Flashcards Review
Chapter Assessment
Network Essentials
HTTP vs. HTTPS
TCP vs. UDP
HTTP: 1.0 vs. 1.1 vs 2.0 vs. 3.0
URL vs. URI vs. URN
What Happens When You Type a URL into the Browser
Flashcards Review
Chapter Assessment
Long-Polling vs. WebSockets vs. Server-Sent Events
Introduction to Real-Time Communication
What is Long-Polling?
What is WebSocket?
What are Server-Sent Events?
Difference Between Long-Polling, WebSockets, and Server-Sent Events
Flashcards Review
Chapter Assessment
Domain Name System (DNS)
Introduction to DNS
DNS Resolution Process
DNS Load Balancing and High Availability
Flashcards Review
Chapter Assessment
Proxies
What is a Proxy Server?
Uses of Proxies
VPN vs. Proxy Server
Flashcards Review
Chapter Assessment
Load Balancing
Introduction to Load Balancing
Load Balancing Algorithms
Uses of Load Balancing
Load Balancer Types
Stateless vs. Stateful Load Balancing
High Availability and Fault Tolerance
Scalability and Performance
Challenges of Load Balancers
Flashcards Review
Chapter Assessment
API Gateway
Introduction to API Gateway
Usage of API gateway
Advantages and disadvantages of using API gateway
Flashcards Review
Chapter Assessment
API Design
What Is an API?
What Are REST APIs?
Resources, Not Actions
HTTP Methods and Their Semantics
URL Design
Request and Response Shapes
Status Codes and Error Design
Pagination from the Consumer's View
Idempotency Keys
Versioning and Backward Compatibility
Concurrency and Conditional Requests
REST vs gRPC vs GraphQL
Flashcards Review
Chapter Assessment
Rate Limiting and Throttling
What Is Rate Limiting
Rate Limiting Algorithms
Distributed Rate Limiting
Rate Limiting in Practice
Flashcards Review
Chapter Assessment
Caching
Introduction to Caching
Why is Caching Important?
Types of Caching
Cache Replacement Policies
Cache Invalidation
Cache Read Strategies
Cache Coherence and Consistency Models
Caching Challenges
Cache Performance Metrics
Flashcards Review
Chapter Assessment
CDN
What is CDN?
Origin Server vs. Edge Server
CDN Architecture
Push CDN vs. Pull CDN
Flashcards Review
Chapter Assessment
Data Partitioning
Introduction to Data Partitioning
Partitioning Methods
Data Sharding Techniques
Benefits of Data Partitioning
Common Problems Associated with Data Partitioning
Flashcards Review
Chapter Assessment
Redundancy and Replication
What is Redundancy?
What is Replication?
Replication Methods
Data Backup vs. Disaster Recovery
Flashcards Review
Chapter Assessment
CAP & PACELC Theorems
Introduction to CAP Theorem
Components of CAP Theorem
Trade-offs in CAP Theorem
Examples of CAP Theorem in Practice
Beyond CAP Theorem
System Design Trade-offs in Interviews
Flashcards Review
Chapter Assessment
Databases (SQL vs. NoSQL)
Introduction to Databases
SQL Databases
NoSQL Databases
SQL vs. NoSQL
ACID vs BASE Properties
Real-World Examples and Case Studies
SQL Normalization and Denormalization
In-Memory Database vs. On-Disk Database
Data Replication vs. Data Mirroring
Database Federation
Flashcards Review
Chapter Assessment
Indexes
What are Indexes?
How a B-Tree Index Works
Types of Indexes
B-Tree vs. LSM Tree
Indexes in Distributed Systems
Flashcards Review
Chapter Assessment
Bloom Filters
Introduction to Bloom Filters
Benefits & Limitations of Bloom Filters
Variants and Extensions of Bloom Filters
Applications of Bloom Filters
Flashcards Review
Chapter Assessment
Quorum
Why Quorum?
What is Quorum?
Flashcards Review
Chapter Assessment
Leader and Follower
What is Leader and Follower Pattern?
Flashcards Review
Chapter Assessment
Heartbeat
What is Heartbeat?
Flashcards Review
Chapter Assessment
Checksum
What is Checksum?
Uses of Checksum
Flashcards Review
Chapter Assessment
Distributed Messaging System
Introduction to Messaging System
Introduction to Kafka
Messaging patterns
Popular Messaging Queue Systems
RabbitMQ vs. Kafka vs. ActiveMQ
Scalability and Performance
Flashcards Review
Chapter Assessment
Distributed File Systems
What is a Distributed File System?
Architecture of a Distributed File System
Key Components of a DFS
Flashcards Review
Chapter Assessment
Security
What is Security and Privacy?
What is Authentication?
What is Authorization?
Authentication vs. Authorization
OAuth vs. JWT for Authentication
What is Encryption?
What are DDoS Attacks?
Flashcards Review
Chapter Assessment
Misc Concepts
Batch Processing vs. Stream Processing
XML vs. JSON
Synchronous vs. Asynchronous Communication
Push vs. Pull Notification Systems
Microservices vs. Serverless Architecture
Message Queues vs. Service Bus
Stateful vs. Stateless Architecture
Event-Driven vs. Polling Architecture
Flashcards Review
Chapter Assessment
Quiz - System Design Fundamentals
Quiz
System Design Trade-offs
Importance of Discussing Trade-offs
Strong vs Eventual Consistency
Latency vs Throughput
ACID vs BASE Properties in Databases
Read-Through vs Write-Through Cache
Batch Processing vs Stream Processing
Load Balancer vs. API Gateway
API Gateway vs Direct Service Exposure
Proxy vs. Reverse Proxy
API Gateway vs. Reverse Proxy
SQL vs. NoSQL
Primary-Replica vs Peer-to-Peer Replication
Data Compression vs Data Deduplication
Server-Side Caching vs Client-Side Caching
REST vs RPC
Polling vs. Long-Polling vs. WebSockets vs. Webhooks
CDN Usage vs Direct Server Serving
Serverless Architecture vs Traditional Server-based
Stateful vs Stateless Architecture
Hybrid Cloud Storage vs All-Cloud Storage
Token Bucket vs Leaky Bucket
Read Heavy vs Write Heavy System
Quiz
How to Approach a System Design Interview
System Design Interviews - A step by step guide
Functional vs. Non-functional Requirements
What are Back-of-the-Envelope Estimations?
Things to Avoid During System Design Interview
System Design Master Template
System Design Master Template
Quiz
Designing a URL Shortening Service like TinyURL
Designing a URL Shortening Service like TinyURL
Quiz - Designing URL Shortner
Designing Pastebin
Designing Pastebin
Quiz - Designing Pastebin
Designing Instagram
Designing Instagram
Quiz - Designing Instagram
Designing Dropbox
Designing Dropbox
Quiz - Designing Dropbox
Designing Facebook Messenger
Designing Facebook Messenger
Quiz - Designing Facebook Messenger
Designing Twitter
Designing Twitter
Quiz - Designing Twitter
Designing Typeahead Suggestion
Designing Typeahead Suggestion
Quiz - Designing Typeahead Suggestion
Designing an API Rate Limiter
Designing an API Rate Limiter
Quiz - Designing an API Rate Limiter
Designing Twitter Search
Designing Twitter Search
Quiz - Designing Twitter Search
Designing a Web Crawler
Designing a Web Crawler
Quiz - Designing a Web Crawler
Designing Facebook’s Newsfeed
Designing Facebook’s Newsfeed
Quiz - Designing Facebook’s Newsfeed
Designing Yelp or Nearby Friends
Designing Yelp or Nearby Friends
Quiz - Designing Yelp or Nearby Friends
Designing Uber backend
Designing Uber backend
Quiz - Designing Uber backend
Designing Ticketmaster
Designing Ticketmaster
Quiz - Designing Ticketmaster
Dynamo: How to design a key value store?
Dynamo: Introduction
High-Level Architecture
Data Partitioning
Replication
Vector Clocks and Conflicting Data
The Life of Dynamo’s put() & get() Operations
Anti-entropy Through Merkle Trees
Gossip Protocol
Dynamo Characteristics and Criticism
Summary: Dynamo
Quiz: Dynamo
Mock Interview: Dynamo
Designing YouTube Likes Counter (medium)
YouTube Likes Counter
Quiz
Cassandra: How to Design a Wide-column NoSQL Database?
Cassandra: Introduction
High-level Architecture
Replication
Cassandra Consistency Levels
Gossiper
Anatomy of Cassandra's Write Operation
Anatomy of Cassandra's Read Operation
Compaction
Tombstones
Summary: Cassandra
Quiz: Cassandra
Mock Interview: Cassandra
Kafka: How to Design a Distributed Messaging System?
Messaging Systems: Introduction
Kafka: Introduction
High-level Architecture
Kafka: Deep Dive
Consumer Groups
Kafka Workflow
Role of ZooKeeper
Controller Broker
Kafka Delivery Semantics
Kafka Characteristics
Summary: Kafka
Quiz: Kafka
Mock Interview: Kafka
Chubby: How to Design a Distributed Locking Service?
Chubby: Introduction
High-level Architecture
Design Rationale
How Chubby Works
File, Directories, and Handles
Locks, Sequencers, and Lock-delays
Sessions and Events
Master Election and Chubby Events
Caching
Database
Scaling Chubby
Summary: Chubby
Quiz: Chubby
Mock Interview: Chubby
HDFS: How to Design File Storage System?
Hadoop Distributed File System: Introduction
High-level Architecture
Deep Dive
Anatomy of a Read Operation
Anatomy of a Write Operation
Data Integrity & Caching
Fault Tolerance
HDFS High Availability (HA)
HDFS Characteristics
Summary: HDFS
Quiz: HDFS
Mock Interview: HDFS
GFS: How to Design a Distributed File System Storage?
Google File System: Introduction
High-level Architecture
Single Master and Large Chunk Size
Metadata
Master Operations
Anatomy of a Read Operation
Anatomy of a Write Operation
Anatomy of an Append Operation
GFS Consistency Model and Snapshotting
Fault Tolerance, High Availability, and Data Integrity
Garbage Collection
Criticism on GFS
Summary: GFS
Quiz: GFS
Mock Interview: GFS
BigTable: How to Design a Wide Column Storage System?
BigTable: Introduction
BigTable Data Model
System APIs
Partitioning and High-level Architecture
SSTable
GFS and Chubby
Bigtable Components
Working with Tablets
The Life of BigTable's Read & Write Operations
Fault Tolerance and Compaction
BigTable Refinements
BigTable Characteristics
Summary: BigTable
Quiz: BigTable
Mock Interview: BigTable
Designing Reddit (medium)
Design Reddit
Quiz
Designing Notification Service (medium)
Designing a Notification System
Quiz
Design Google Calendar (medium)
Design Google calendar (Medium)
Quiz
Design a Recommendation System (medium)
Design a Recommendation System for Netflix
Quiz
Designing Gmail (medium)
Design Gmail
Quiz
Designing Google News (medium)
Design Google News, a Global News Aggregator System (Medium)
Quiz
Designing Unique ID Generator (medium)
Design Unique ID Generator (Easy)
Quiz
Designing Code Judging System (medium)
Design Code Judging System like LeetCode (Medium)
Quiz
Designing Payment System (hard)
Design Payment System
Quiz
Designing Flash Sale System (hard)
Design a Flash Sale for an E-commerce Site (Hard)
Quiz
Designing Reminder Alert System (hard)
Design a Reminder Alert System
Quiz
System Design Patterns
Introduction: System Design Patterns
1. Bloom Filters
2. Consistent Hashing
3. Quorum
4. Leader and Follower
5. Write-ahead Log
6. Segmented Log
7. High-Water Mark
8. Lease
9. Heartbeat
10. Gossip Protocol
11. Phi Accrual Failure Detection
12. Split Brain
13. Fencing
14. Checksum
15. Vector Clocks
16. CAP Theorem
17. PACELC Theorem
18. Hinted Handoff
19. Read Repair
20. Merkle Trees
Quiz
Designing Youtube or Netflix
availability
caching
distributed systems
cdn
+1
Every case study in this chapter follows the same seven steps from System Design Interviews: A step by step guide, and builds its design from the components in the System Design Master Template. Keep both open beside you, and try each problem yourself before reading the solution.
Why YouTube?
YouTube is one of the most popular video sharing services in the world. Users can upload, view, share, and rate videos. They can also report videos and add comments. A video-on-demand service like Netflix shares most of this design.
Try it yourself
Before reading the solution, try designing it. Aim to produce four things: the requirements, a rough load estimate, the API, and a first drawing of the components.
Sketch it here
Designing YouTube (video)
Here is a video discussing how to design YouTube:
Step 1: Clarify the Requirements
💡 Always clarify requirements at the beginning of the interview. Ask questions until you know the exact scope of the system the interviewer has in mind.
We will design a simpler version of YouTube.
Functional requirements
- Users can upload videos.
- Users can share and view videos.
- Users can search for videos by title.
- The service records stats for each video, like likes, dislikes, and total views.
- Users can add and view comments on videos.
Non-functional requirements
- High reliability. Any video that is uploaded must never be lost.
- High availability. We accept weaker consistency in exchange. If a user does not see a new video for a short time, that is fine.
- Low latency on playback. Watching a video should feel real time, with no lag.
Out of scope: video recommendations, most popular videos, channels, subscriptions, watch later, and favorites.
Requirement 2 trades immediate consistency for availability. Step 6 shows exactly where that staleness appears.
Step 2: Estimate the Scale
Ask the interviewer for the size of the user base and the ratio of uploads to views. Assume 1.5 billion total users, of whom 800 million are daily active users. Assume an average user views five videos per day.
800M x 5 / 86,400 seconds = ~46K video views per second
Assume an upload:view ratio of 1:200. For every video uploaded, 200 videos are viewed.
46K / 200 = ~230 videos uploaded per second
Storage. Assume 500 hours of video are uploaded every minute. Assume one minute of video needs 50 MB of storage. That figure is high because we store each video in multiple formats.
500 hours x 60 mins x 50MB = 1,500 GB per minute (25 GB per second)
These estimates ignore video compression and replication, which would change the real numbers.
Bandwidth. 500 hours per minute is 30,000 minutes of video per minute. Assume uploading one minute of video moves about 10 MB over the network.
500 hours x 60 mins x 10MB = 300 GB per minute (5 GB per second) incoming
Every uploaded byte is sent out again once per view. With a 1:200 ratio, outgoing traffic is 200 times incoming traffic.
5 GB/s x 200 = 1 TB per second outgoing
Three of these numbers change the design:
- 46K views against 230 uploads per second. The system is read heavy. Every expensive improvement belongs on the read path.
- 25 GB of new video per second. No single machine can hold this. Video storage must be distributed from the start.
- 1 TB per second outgoing. One data center cannot serve this, which is why a CDN appears in step 6.
Step 3: Define the API
💡 Defining the API early forces you to be concrete about what the system actually does, and it usually exposes a requirement nobody has stated yet.
We can expose the service through REST APIs. Three calls cover the core product: upload, search, and stream.
uploadVideo(api_dev_key, video_title, video_description, tags[], category_id, default_language, recording_details, video_contents)
api_dev_key is the API developer key of a registered account. We use it, among other things, to throttle users based on their allocated quota. The next parameters describe the video: a title, an optional description and tags, and a category_id like Film or Song. Then come the default language and the recording location. video_contents is the video stream itself.
Returns: a successful upload returns HTTP 202 (request accepted). Encoding runs later in the background. When it completes, the user gets an email with a link to the video. We can also expose a queryable API that reports the current status of an uploaded video.
searchVideo(api_dev_key, search_query, user_location, maximum_videos_to_return, page_token)
search_query holds the search terms, and user_location is the optional location of the searching user. maximum_videos_to_return caps the results in one request, and page_token names which page of the result set to return.
Returns: a JSON list of the video resources that match the query. Each resource has a video title, a thumbnail, a creation date, and a view count.
streamVideo(api_dev_key, video_id, offset, codec, resolution)
video_id identifies the video. offset is a time in seconds from the beginning of the video. We store the offset on the server. That lets a user pause on one device and continue on another from the same point. The client also sends codec and resolution. A codec is the format used to compress and decompress video. A TV and a phone use different codecs and resolutions, so both values are needed to resume playback across devices.
Returns: a media stream, one video chunk, starting at the given offset.
Step 4: Define the Data Model
The system stores two very different kinds of data. Video files are large binary objects, and they never go into a database. Step 6 shows where they live. Metadata rows are small and structured, and they go into a SQL database.
The entities come directly from the requirements. Users upload videos, and users comment on videos. That gives three tables: Video, Comment, and User. The video files sit in a separate file store, and each Video row points into it.
Video metadata storage (MySQL). Store the following with each video:
- VideoID
- Title
- Description
- Size
- Thumbnail
- Uploader/User
- Total number of likes
- Total number of dislikes
- Total number of views
For each video comment, store:
- CommentID
- VideoID
- UserID
- Comment
- TimeOfCreation
User data storage (MySQL). UserID, name, email, address, age, and registration details.
The schema below shows the three tables, their keys, and the file store the video rows point into:
Video holds one row per video, keyed by VideoID. This is the same ID that streamVideo takes as a parameter. Title, Description, and Size describe the upload, and search by title reads the Title column. Thumbnail points at the video's thumbnail files. Uploader/User holds the UserID of the uploading account, so one user maps to many videos. The row never holds the video file itself. It keeps the file path into the file store.
The last three columns are the totals of likes, dislikes, and views. Requirement 4 asked for exactly these stats. They are the only Video columns that keep changing after the upload. Step 6 places a cache in front of these hot rows.
Comment holds one row per comment, keyed by CommentID. VideoID names the video the comment belongs to, so one video maps to many comments. UserID names the author, and TimeOfCreation lets us show comments in time order. These five fields are the whole record. We store no location with a comment. The user_location in the search API is a request parameter, not a stored field.
User holds one row per account, keyed by UserID. Both other tables point at it: a Video row names its uploader, and a Comment row names its author. This table grows only when someone registers.
Where does each record live? All three tables live in MySQL. The video and thumbnail files live in the separate file store, shown as the cylinder. Step 6 picks object storage for it, with HDFS as the alternative, and then gives thumbnails their own store.
The other two tables grow with traffic. Step 2 estimated 230 video uploads per second, so Video gains about 230 new rows per second. Comment grows with every comment users write. A metadata row is tiny next to the video file it describes. The files take 25 GB of new storage every second, per step 2. Still, the metadata soon outgrows one database server. Step 6 shards it across many machines, by UserID or by VideoID.
Why a SQL database? These rows have fixed fields and clear relationships, like a comment belonging to a video. That is the shape relational databases are built for. A wide-column store only becomes attractive at extreme scale, and step 6 handles scale by sharding instead.
Step 5: Draw the High-Level Design
At a high level, we need the following parts:
- Processing queue: Each uploaded video is pushed into a processing queue. Videos wait there to be de-queued for encoding, thumbnail generation, and storage. The queue separates the upload itself from that heavy background work.
- Encoder: Encodes each uploaded video into multiple formats.
- Thumbnails generator: Creates a few thumbnails for each video.
- Video and thumbnail storage: Stores the video and thumbnail files in distributed storage.
- User database: Stores user information, like name, email, and address.
- Video metadata storage: Stores everything about a video: title, file path in the system, uploading user, total views, likes, and dislikes. It also stores all the video comments.
The diagram below shows how these parts connect.
Step 6: Go Deep
The service is read heavy. The read:write ratio is 200:1, meaning for every video upload there are 200 video views. So we focus on making the system retrieve videos quickly.
Where are the videos stored?
A video file is written once and read many times. After encoding, it is never edited in place. Two families of storage fit that pattern.
Object storage, like Amazon S3, is the standard answer today. An object store keeps files as named blobs and serves them over HTTP. It replicates every object across machines on its own. That replication is what our reliability requirement demands. Object storage also connects naturally to a CDN, which we add later in this step.
A distributed file system, like HDFS or GlusterFS, also works. HDFS is built for batch jobs that scan very large files. It favors total throughput over the latency of one request. It also makes you operate the replication and the name servers yourself. Choose it when the same files must feed heavy analytics jobs. For serving videos to viewers, object storage is the simpler and cheaper choice.
How should we manage read traffic?
Separate the read traffic from the write traffic. We keep multiple copies of each video, so video reads can spread across many servers.
For metadata, use a primary-secondary configuration. Writes go to the primary first and are then applied to all the secondaries. The secondaries serve reads. This adds read capacity, but it also adds staleness. A new video's metadata reaches the primary first. A secondary that has not applied that write yet returns stale results. Here the staleness lasts a few milliseconds, so the user sees the new video after a very short delay. We already accepted this trade in the non-functional requirements.
Where are thumbnails stored?
There will be far more thumbnails than videos. Assume every video has five thumbnails. Two facts decide the storage choice:
- Thumbnails are small files, say a maximum of 5 KB each.
- Their read traffic is huge compared to videos. A user watches one video at a time but sees a page with 20 thumbnails of other videos.
First, evaluate storing all the thumbnails on plain disk. With billions of small files, reading them means many seeks to different disk locations. A seek is the physical movement of a disk head to a new position. Many scattered seeks are inefficient and produce higher latencies.
Bigtable is a reasonable choice instead. It combines multiple files into one block on disk. It is also very efficient at reading small amounts of data. Those are exactly our two requirements. Keep hot thumbnails in an in-memory cache as well. Thumbnail files are small, so the cache can hold a large number of them.
Video uploads
Videos can be huge. If the connection drops while uploading, we should support resuming from the same point. The user should never have to restart a large upload from the beginning.
Video encoding
A newly uploaded video is stored first, and a task is added to the processing queue. The encoder de-queues the task and encodes the video into multiple formats. Once all the encoding completes, the uploader is notified. The video then becomes available for viewing and sharing.
The diagram below shows the detailed component design, with all of these parts in place.
Metadata sharding
Millions of new videos arrive every day, and the read load is extremely high. One database server cannot handle both, so we distribute the metadata across many machines. There is more than one way to split it.
Sharding based on UserID. Pass the UserID through a hash function. The result names the server that stores all metadata for that user's videos. To read one user's videos, hash the UserID again and query that one server. Searching by title is harder. We must query every server, and each server returns a set of videos. A centralized server then aggregates and ranks these results before returning them to the user.
This approach has two problems:
- What if a user becomes popular? The server holding that user receives a large share of the queries. That one server becomes a performance bottleneck, and it slows the whole service.
- Over time, some users store far more videos than others. Keeping the data evenly distributed becomes difficult.
To recover, we either repartition the data or use consistent hashing to balance the load between servers.
Sharding based on VideoID. The hash function maps each VideoID to a random server, which stores that video's metadata. To find one user's videos, we query all servers, and a centralized server aggregates and ranks the results. This solves the popular-user problem but shifts it to popular videos. A cache in front of the database servers holds hot videos and absorbs most of that load.
Video deduplication
With this many uploaders, the service receives many duplicate videos. Duplicates often differ in aspect ratio or encoding. They may carry overlays or extra borders, or be excerpts from a longer original. Duplication hurts at several levels:
- Storage: we waste space keeping multiple copies of the same video.
- Caching: duplicates take cache slots that unique content could use.
- Network: duplicates increase the data sent to in-network caching systems.
- Energy: extra storage, weaker caches, and extra traffic all waste energy.
Users see the cost too: duplicate search results, longer video startup times, and interrupted streaming.
Deduplication makes the most sense early, while the user is still uploading. This is called inline deduplication, as opposed to finding duplicates later in post-processing. Deduplicating inline saves the resources a duplicate would consume in encoding, transfer, and storage. As soon as an upload starts, the service runs video matching algorithms, like Block Matching or Phase Correlation. If we already have a copy, we can stop the upload and use the existing copy. Or we can finish the upload and keep the new video if its quality is higher. If the new video is a subpart of an existing video, or the reverse, we divide the video into smaller chunks. Then we upload only the parts that are missing.
Load balancing
Use consistent hashing among the cache servers. It balances the load between them, and it handles a cache server dying. One problem remains. A static hash maps each video to one hostname, but video popularity is very uneven. A popular video sends heavy traffic to its one logical replica. That uneven load then appears on the physical server underneath.
To resolve this, a busy server can redirect a client to a less busy server in the same cache location. We can use dynamic HTTP redirections for this. A redirection is the server telling the client to request the video from a different address.
Redirections have drawbacks. If the server receiving the redirection cannot serve the video either, the client is redirected again. Each redirection is one extra HTTP request, so the video starts playing later. Redirections across tiers, or across data centers, can also send a client to a distant cache location. Higher-tier caches exist in only a small number of locations.
Cache
Introduce a cache for the metadata servers to hold hot database rows. Application servers check a cache like Memcache before querying the database. Least Recently Used (LRU) is a reasonable eviction policy here. Under LRU, we discard the least recently viewed row first. LRU fits because a video's popularity fades as it ages. The rows being read right now stay in memory.
How can we build a more intelligent cache? Apply the 80-20 rule: 20 percent of the daily read volume generates 80 percent of the traffic. In other words, a small set of popular videos receives most of the views. So we can cache 20 percent of the daily read volume of videos and metadata.
Content Delivery Network (CDN)
A CDN is a system of distributed servers that delivers content based on the user's location and the content's origin. The Caching lesson covers CDNs in more detail.
Our service moves popular videos to CDNs:
- CDNs replicate content in many places. A video is more likely to sit near its viewer, and it crosses fewer networks on the way.
- CDN machines use heavy caching and can mostly serve videos out of memory.
Less popular videos, say 1 to 20 views per day, are not cached by CDNs. Our own servers in various data centers serve them.
One property makes all this caching safe. An encoded video file never changes, so a cached copy of it can never become stale. Only the metadata, like view counts, keeps changing. We already accepted small staleness there.
Step 7: Bottlenecks and Failure Points
The interviewer does not expect this design to be limitless. They expect you to know where it ends.
- A database server dies. Use consistent hashing to distribute data among the database servers. It replaces a dead server with minimal data movement, and it spreads the load while doing so.
- A video goes viral. The cache, the CDN, and dynamic HTTP redirections absorb most of the surge. The metadata shard holding that VideoID is still the hottest single point.
- The processing pipeline falls behind. The queue, the encoder, and the thumbnail generator can all be slow or down. New uploads then take longer to appear, but playback keeps working. Watching a video depends only on the stores, the caches, and the CDN. Say this isolation out loud, because it is a strength of the design.
- Stale metadata reads. A secondary can serve rows a few milliseconds old, so a viewer may briefly miss a brand new video. We chose this in step 1, so present it as a choice, not a defect.
Where AI Fits in This Design
After the main design, interviewers often ask one more question: where would AI fit in this system?
For a video platform, the answer sits in the processing pipeline. That pipeline is already asynchronous. An upload enters the queue, the encoder and the thumbnail generator do their work, and the user waits for none of it. An AI step is simply one more consumer of the same queue.
Two fits matter here. The first is automatic captions. A speech-to-text model (one that turns spoken audio into written text) transcribes each new video, and the result is stored with the metadata. The second is moderation. A classifier (a small model that assigns labels) scores frames and audio for content that breaks the rules. It sends what it finds to human review.
Now apply the same judgment the rest of the design uses. Captions must never block publishing. If the caption job fails, the video still goes live, and the captions can be added later. Moderation is the opposite choice: publishing may wait for it, and that is a product decision, not a technical one. Name the cost too. These models are priced by the minute of video processed, so the bill grows with every upload, whether anyone watches it or not.
Playback needs no AI. The storage split, the caches, and the CDN stay exactly as designed.
💡 In the interview: spend your time on storage and the read path. Say early that the system is read heavy, with 200 views for every upload. The moment the interviewer is listening for is the storage split: video files in object storage, metadata in sharded MySQL, thumbnails in Bigtable. Have the object storage versus HDFS trade ready, and the reason an upload returns HTTP 202 instead of finishing inline. The most likely follow-up is a viral video: answer with the cache, the CDN, and dynamic redirections.
Key takeaway: YouTube is a storage and delivery problem under a very uneven load, with 200 views for every upload. Split the data by shape. Large immutable video files go to object storage, or to a distributed file system like HDFS. Small structured metadata goes to sharded MySQL, and tiny hot thumbnails go to Bigtable. Push every upload through a processing queue, so encoding never blocks the user, and return HTTP 202. Serve popular videos from a CDN and hot metadata from an LRU cache, following the 80-20 rule. Deduplicate inline during upload, and use consistent hashing to spread both database and cache load.
Discussion
On This Page