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
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 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
TCP vs. UDP
tcp
udp
reliability
latency
You are on a video call while a large file downloads in the background. Both use the same network, but they need very different things.
The download must arrive perfectly. If one byte is wrong, the file is broken. The call must arrive quickly. A voice that arrives two seconds late is useless, even if every piece of it is correct.
The internet has two main protocols for these two needs: TCP and UDP. This lesson explains how each one works, what each one costs, and how to choose between them.
Where TCP and UDP Fit
Data travels across the internet in small pieces called packets. The Internet Protocol (IP) moves each packet from one machine to another, using IP addresses to find the machines.
IP does not promise much. A packet can be lost, arrive twice, or arrive out of order. IP also knows only machines, not the programs running on them.
TCP and UDP run on top of IP, and they add two things.
- Ports. A port is a number that identifies one program on a machine. For example, a web server usually listens on port 443, so the operating system knows which program should receive each packet.
- Rules for delivery. TCP adds strong promises about delivery. UDP adds almost none.
Applications then run on top of TCP or UDP. For example, HTTP/1.1 and HTTP/2 run on top of TCP, and most DNS lookups run on top of UDP.
TCP
TCP stands for Transmission Control Protocol. It is connection-oriented, which means the two sides set up a connection before any data moves. It delivers a stream of bytes that is reliable, ordered, and error-checked.
The Three-Way Handshake
A TCP connection starts with a three-way handshake.
- SYN. The client sends a packet that asks to open a connection.
- SYN-ACK. The server agrees and acknowledges the request.
- ACK. The client acknowledges the server's reply. The connection is now open.
The handshake costs one round trip, which is the time for a message to go to the server and come back. If a round trip takes 100 ms, the client waits about 100 ms before it can send its first request.
How TCP Makes Delivery Reliable
TCP gives each byte a sequence number, which is its position in the stream. TCP uses these numbers in several ways.
- Acknowledgements. The receiver tells the sender which data has arrived. These messages are called acknowledgements, or ACKs.
- Retransmission. If the sender gets no acknowledgement for some data in time, it sends that data again. So lost or damaged packets are replaced.
- Ordering. Packets can arrive out of order. The receiver puts them back in order by sequence number before it passes the data to the application.
- Error checking. Each packet carries a checksum, a small value calculated from its contents. If the contents were damaged on the way, the checksum does not match, and the packet is thrown away and sent again.
Flow Control and Congestion Control
TCP also controls how fast it sends.
- Flow control stops a fast sender from overwhelming a slow receiver. The receiver keeps reporting how much more data it can accept right now.
- Congestion control stops the sender from overloading the network. A new connection starts by sending slowly and speeds up step by step. When packets are lost, TCP takes that as a sign the network is busy, and it slows down.
The Costs of TCP
All of these promises add work and delay.
- The handshake adds a round trip before any data moves.
- Every piece of data waits for acknowledgements, and lost data waits for retransmission.
- Congestion control slows the sender down when the network is busy.
- The TCP header is at least 20 bytes on every packet.
There is one more cost, called head-of-line blocking. TCP delivers data only in order. So if one packet is lost, every packet after it waits, even if those packets already arrived. The application sees nothing new until the lost packet is sent again. The next lesson shows how this problem led to changes in newer versions of HTTP.
When to Use TCP
Use TCP when accuracy matters more than speed. Common examples are below.
- Web browsing, with HTTP and HTTPS.
- Email, with SMTP, POP3, and IMAP.
- File transfer, with FTP, and any download.
- Database connections and most calls between services.
Loading a web page is the everyday example. Every stylesheet, script, and image has to arrive complete and in order, or the page breaks.
UDP
UDP stands for User Datagram Protocol. It is connectionless. It sends messages called datagrams without setting up a connection first, and it does not promise delivery or order.
- No setup. The first datagram can leave immediately. There is no handshake.
- Low overhead. The UDP header is only 8 bytes. There are no acknowledgements and no retransmission.
- Unreliable. A datagram can be lost, arrive twice, or arrive out of order, and UDP will not fix it. UDP has a checksum, but a damaged datagram is simply thrown away, not sent again.
- No congestion control. UDP does not slow down when the network is busy. It keeps sending at the same rate, and some datagrams may be dropped.
UDP is faster than TCP because it skips the connection setup, the acknowledgements, and congestion control.
Why Real-Time Apps Choose UDP
For some applications, late data is worse than missing data.
Think of an online game that sends each player's position 30 times per second. Suppose one update is lost. With UDP, the next update arrives about 33 ms later, and it replaces the lost one. With TCP, the newer updates would wait until the lost one was sent again. The player would see the game freeze, then jump.
The same is true for voice and video calls, and for live streams. A lost packet causes a short glitch. Waiting for a resend would cause a delay that keeps growing.
When to Use UDP
Use UDP when speed matters more than perfect accuracy, and some loss is acceptable. Common examples are below.
- Live video and audio streaming.
- Online games.
- Voice over IP (VoIP) and video calls.
- DNS lookups. A lookup is one small question and one small answer. If it is lost, the client simply asks again after a short wait.
Some applications need both speed and reliability. They use UDP and build only the reliability they need on top of it. QUIC, the protocol under HTTP/3, works this way.
TCP and UDP Side by Side
| TCP | UDP | |
|---|---|---|
| Connection | Set up first, with a handshake | None, sent immediately |
| Reliability | Lost data is sent again | Lost data stays lost |
| Order | Always in order | May arrive out of order |
| Speed and overhead | Slower, 20-byte header or more | Faster, 8-byte header |
| Flow and congestion control | Yes | No |
| Good fit | Web, email, file transfer | Streaming, gaming, calls, DNS |
Choosing TCP means accepting some delay. A file transfer must deliver every byte accurately and in order. It needs TCP, and it accepts the extra time for acknowledgements and congestion control. A file with a missing piece is useless.
Choosing UDP means accepting some loss. A live audio stream over UDP gets low latency. Because UDP has no congestion control, it keeps sending at the same rate even when the network is busy. Some packets are dropped instead of delayed. For live audio, a short glitch is better than a delay that keeps growing.
A useful question is: "If one piece is missing, does the result break, or does it only have a small glitch?" If it breaks, use TCP. If it only glitches, UDP is often the better choice.
Key Takeaways
- TCP and UDP run on top of IP. They add ports, which identify programs, and their own rules for delivery.
- TCP is connection-oriented. It uses a three-way handshake, and it delivers data reliably, in order, and error-checked.
- TCP uses sequence numbers, acknowledgements, retransmission, flow control, and congestion control. These make it slower, and a lost packet causes head-of-line blocking.
- UDP is connectionless. It sends datagrams with no setup, no acknowledgements, and no congestion control, so it is faster but may lose data.
- Use TCP when accuracy matters more than speed, like web browsing, email, and file transfer.
- Use UDP when speed matters more than accuracy, like live streaming, online games, VoIP, and DNS lookups.
TCP promises that every byte arrives in order, and it spends time to keep that promise. UDP promises only speed, and the application decides what to do about loss. The next lesson, HTTP/1.0 vs HTTP/1.1 vs HTTP/2.0 vs HTTP/3.0, shows how HTTP changed as it used these protocols in new ways.
Practice Questions
Try each question first, then open the answer.
1. A round trip between a client and a server takes 80 ms. How long does the TCP handshake take before the client can send its first request? How long is the wait if HTTPS with TLS 1.3 runs on top?
<details> <summary>Show answer</summary>About 80 ms for TCP, and about 160 ms with TLS 1.3. The SYN goes out and the SYN-ACK comes back, which is one round trip. The client can send its request together with the final ACK. The TLS 1.3 handshake then adds one more round trip, so the first HTTP request leaves after about 160 ms.
</details>2. A TCP sender sends packets 1, 2, 3, 4, and 5. Packet 3 is lost, and the others arrive. What does the receiving application get, and when?
<details> <summary>Show answer</summary>It gets 1 and 2 immediately, then 3, 4, and 5 together after 3 is sent again. TCP delivers data only in order. Packets 4 and 5 wait in the receiver, even though they arrived. When the sender retransmits packet 3, the receiver passes 3, 4, and 5 to the application. This waiting is head-of-line blocking.
</details>3. A multiplayer game sends each player's position 30 times per second. Should it use TCP or UDP, and why?
<details> <summary>Show answer</summary>UDP. A new position arrives about every 33 ms, and it replaces the old one. If one update is lost, the next update shows the correct position. With TCP, newer updates would wait for the lost one to be resent, so the game would freeze and then jump.
</details>4. Which protocol fits each task: (a) downloading a software update, (b) a video call, and (c) sending an email?
<details> <summary>Show answer</summary>(a) TCP, (b) UDP, (c) TCP. A software update must arrive complete and correct, or it could break the program. A video call needs low delay, and a short glitch is acceptable. Email must arrive complete, so the email protocols run on TCP.
</details>5. Most DNS lookups use UDP. What happens if a DNS query is lost, since UDP does not resend data?
<details> <summary>Show answer</summary>The client asks again. UDP does not retransmit, so the DNS client waits a short time, like one or two seconds. If no answer arrives, it sends the same query again, or asks another DNS server. The application adds this simple retry itself, which is cheaper than setting up a TCP connection for one small question.
</details>Discussion
On This Page