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
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 Youtube or Netflix
Designing Youtube or Netflix
Quiz - Designing Youtube
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
What is CDN?
cdn
caching
distributed systems
availability
+3
A video website stores all its videos on servers in New York. A viewer in Paris presses play. The video starts slowly and pauses to load, even though the servers in New York are fast and not busy.
The problem is distance. Every piece of the video must cross the Atlantic Ocean, and each round trip between Paris and New York takes about 80 ms. No faster server can remove that travel time.
The fix is to keep copies of the video close to the viewers. This lesson explains the system that does that, how it serves each request, and the terms used to describe it.
What a CDN Is
A Content Delivery Network (CDN) is a distributed network of servers placed in many geographic locations. It is used to deliver web content efficiently by serving each user from a server near them.
A CDN mostly delivers static content, which is content that is the same for every user. Examples are images, videos, stylesheets, scripts, and files for download.
The primary purpose of a CDN is to reduce latency and improve performance, by serving content from the server nearest to the user. Most other benefits of a CDN come from this idea.
Why Serving Everyone From One Place Is Slow
Suppose every request goes to one origin server, the server that stores the original content. Two problems appear for a global audience.
- Distance. Users far from the origin wait longer, because each request and response travels farther.
- Load. One server must answer every user in the world, so it can become overloaded during busy times.
A CDN solves both. It keeps copies of the content near users, and it answers most requests itself, so the origin does much less work.
Key Terms: PoP, Edge Server, and Origin Server
Three terms describe where things are in a CDN.
| Term | What it is |
|---|---|
| Point of Presence (PoP) | A physical location, usually a data center, where a group of CDN servers is deployed |
| Edge server | A CDN server in a PoP that caches content and delivers it to nearby users |
| Origin server | The primary server where the original content is stored, and where the CDN fetches it from |
A PoP is a place, not a single machine. Each PoP usually holds several edge servers for capacity and redundancy. PoPs are placed close to large groups of users to keep latency low. A large CDN has hundreds of PoPs around the world.
The origin server belongs to the website. The PoPs and edge servers belong to the CDN provider. Common CDN providers include Cloudflare, Akamai, Amazon CloudFront, and Fastly.
How a CDN Serves a Request
Here is what happens when the viewer in Paris requests the video on a website that uses a CDN.
- The request is routed to a nearby edge server. The CDN picks a good edge server for the user, usually in a PoP in or near Paris. The choice is based on network latency and how busy each server is, not only on distance.
- The edge server checks its cache.
- If the content is cached, the edge server serves it immediately from the cache. This is the fast path.
- If the content is not cached, the edge server fetches it from the origin server in New York. It stores a copy in its cache, and then serves it to the user.
So the first request for a piece of content in a region must wait for the trip to the origin. The requests after it are served from the nearby edge server. This is also why a CDN reduces the load on the origin server: the origin no longer answers every request.
How Users Reach the Nearest Edge Server
CDNs use several routing methods to send each user to a good edge server.
- DNS-based routing. The CDN's DNS server answers each lookup with the IP address of a suitable edge server for that user.
- Anycast. Many edge servers share the same IP address. The network sends each request to the nearest one, based on the lowest latency or the shortest network path.
The DNS load balancing lesson explains both methods. The Origin Server vs. Edge Server lesson covers routing in more detail.
Controlling What Stays in the Cache
A CDN must decide how long to keep each copy, and what to do when the original changes. These terms describe those decisions.
Time to Live (TTL). TTL is a value that sets how long a piece of content stays in the cache. After that time, the content is considered stale, and it must be refreshed from the origin server. The origin usually sets the TTL with a response header, like this one.
Cache-Control: public, max-age=86400
Here, max-age=86400 means the content may be cached for 86,400 seconds, which is one day.
Cache warming. Cache warming means preloading content into edge servers' caches before any user requests it. It makes sure the very first request is already fast. Teams often warm the cache before a big launch.
Content invalidation. Content invalidation means removing or updating cached content when the original content on the origin server changes. Without it, users would keep getting the old version until the TTL runs out.
Cache purging. Cache purging means forcibly removing content from an edge server's cache. It is usually triggered manually, or automatically when certain conditions are met.
Many teams avoid stale files in a simpler way. They put a version in each file name, like app.v42.js. When the file changes, its name changes too. The CDN treats it as new content, so no invalidation is needed.
Benefits of a CDN
A CDN brings five main benefits.
- Reduced latency. Content travels a shorter distance, so pages and videos load faster.
- Improved performance. The CDN delivers the static content, so the origin server has more resources for dynamic content, and the origin's load drops.
- Reliability and availability. A CDN has many edge servers in many places. If one server becomes unavailable, requests are sent to another, and users still get the content.
- Scalability. A CDN handles sudden traffic spikes and very large numbers of requests, so a website can grow more easily.
- Security. Many CDNs add protection against denial of service attacks, which try to overload a website with fake traffic. They can also run a Web Application Firewall (WAF), which blocks harmful requests, and handle SSL/TLS encryption at the edge.
Here is the effect on the origin in numbers. Suppose a website receives 1,000,000 requests per hour, and the CDN serves 95 percent of them from its caches. Only 50,000 requests per hour reach the origin server.
What to Put on a CDN
A CDN works best for content that is the same for many users and changes rarely.
- Good fits: images, videos, stylesheets, scripts, fonts, and files for download.
- Sometimes a fit: public pages that change slowly, like a news article, cached for a short TTL.
- Poor fits: personal content, like a user's shopping cart or account page, which is different for every user. The CDN can still pass these requests to the origin, but it should not cache them.
Key Takeaways
- A CDN is a distributed network of servers in many geographic locations that delivers web content efficiently.
- Its primary purpose is to reduce latency and improve performance, by serving content from the server nearest to the user.
- A PoP is a physical location with a group of CDN servers. An edge server in a PoP caches and delivers content. The origin server stores the original content.
- A request goes to a nearby edge server. The edge server serves cached content immediately. Otherwise it fetches the content from the origin, caches it, and serves it.
- TTL sets how long content stays cached. Cache warming preloads content, content invalidation removes outdated copies, and cache purging forcibly removes content.
- CDNs reduce latency, lower the origin's load, improve reliability and scalability, and add security.
A CDN moves copies of content close to users, so distance stops slowing users down. The next lesson, Origin Server vs. Edge Server, looks more closely at these two kinds of server.
Practice Questions
Try each question first, then open the answer.
1. A user in Paris requests a video. The website's origin server is in New York, and the site uses a CDN. What most likely happens?
<details> <summary>Show answer</summary>The request goes to an edge server in or near Paris. If that edge server has the video cached, it serves the video immediately. If not, it fetches the video from the origin in New York, caches a copy, and serves it. Later viewers near Paris get the video from the cache.
</details>2. A website sets a TTL of one day on its logo, and then changes the logo. Users still see the old logo. Name two ways to fix this.
<details> <summary>Show answer</summary>Invalidate or purge the cached logo, or give the new logo a new file name. Invalidation or purging removes the old copy from the edge servers before its TTL ends, so they fetch the new logo. A new file name, like logo-v2.png, is treated as different content, so the edge servers fetch it fresh.
3. A company launches a new video at 9:00 and expects millions of views in the first few minutes. How can it make sure the first viewers do not wait for trips to the origin?
<details> <summary>Show answer</summary>By warming the cache before the launch. The team preloads the video into edge servers' caches before 9:00. The first viewers are then served from nearby edge servers. The origin also avoids a sudden large number of cache misses at launch time.
</details>4. A website receives 1,000,000 requests per hour, and its CDN serves 95 percent of them from cache. How many requests per hour reach the origin server?
<details> <summary>Show answer</summary>50,000 requests per hour. 95 percent are served by the CDN, so 5 percent reach the origin. 5 percent of 1,000,000 is 50,000. The origin handles one twentieth of the traffic it would handle without the CDN.
</details>5. Which of these should not be cached by a CDN: (a) product images, (b) a user's shopping cart page, or (c) the website's JavaScript files?
<details> <summary>Show answer</summary>(b) the shopping cart page. It is different for every user, and it changes often, so a cached copy could show one user's cart to another. Product images and JavaScript files are the same for everyone and change rarely, which makes them good fits for a CDN.
</details>Discussion
On This Page