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
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
Load Balancer vs. API Gateway
load balancing
api gateway
reverse proxy
authentication
+2
A food delivery app has a mobile client. The client calls /restaurants, /orders, and /payments. Each of these paths is handled by a different backend service, and each service runs on several servers.
The team draws a box in front of the servers. Then someone asks: is that box a load balancer, or an API gateway? Both receive requests and pass them on. Both sit in front of servers. Are they the same thing?
They are not. A load balancer picks a server. An API gateway picks a service and applies rules to the request. This lesson explains both jobs, why they are easy to confuse, and why most real systems use both.
Two Boxes in Front of Your Servers
Both a load balancer and an API gateway are reverse proxies. A reverse proxy is a server that receives requests on behalf of the servers behind it. The Proxy vs. Reverse Proxy lesson explains this category.
The difference is the question each one answers:
- A load balancer answers: which copy of a server should handle the request?
- An API gateway answers: which service should handle the request, and is the request allowed?
The Load Balancer
A load balancer distributes incoming traffic across multiple backend servers, so that no single server becomes overloaded.
The servers behind a load balancer are usually identical copies. They run the same code, so any copy can handle any request. The only decision is which copy gets each request. An algorithm makes that choice. Common algorithms include:
- Round robin: send requests to each server in turn. It is simple, but it does not consider the current load or capacity of each server.
- Least connections: send each request to the server with the fewest active connections. It works well when request durations vary a lot.
- IP hash: use the client's IP address to choose the server, so the same client always reaches the same server. This gives session persistence.
The Load Balancing lesson covers these algorithms in detail.
Health Checks
A load balancer has a second job that is just as important. It sends each server a health check, which is a small request sent at regular intervals to confirm that the server still responds. When a server fails its health checks, the load balancer stops sending traffic to it.
For example, suppose the load balancer checks each server every 5 seconds, and removes a server after 3 failed checks in a row. If a server crashes, it is removed within about 10 to 15 seconds. After that, all requests go to the healthy copies.
This is what allows one server to fail without users noticing.
What a Load Balancer Reads
A Layer 4 load balancer works with network connections. It looks only at IP addresses and ports, and it never reads the request itself.
A Layer 7 load balancer reads HTTP requests. It can route by path or header, for example sending /images to one group of servers. This overlap is one reason people confuse the two boxes. But even a Layer 7 load balancer does not usually check who the user is or how many requests they have sent.
The API Gateway
An API gateway is the single entry point for your APIs. It sits in front of different services, and it reads each request to decide which service should handle it. It gives clients one unified interface, instead of many service addresses.
Because the gateway already reads every request, it is the natural place for cross-cutting concerns. These are jobs that every service needs, like security and rate limiting. Handling them once at the gateway means each service does not repeat the same code.
- Routing. Send
/ordersto the orders service, and/usersto the users service. - Authentication. Check who the caller is once, at the entry point, so the services can trust the request.
- Rate limiting. Reject a client that sends too many requests, before any expensive work runs.
- Caching. Return common responses without calling any service.
- Combining responses. Call several services and return one response to the client. For example, a home screen that needs data from three services can get it in one request.
A request that fails a check is stopped at the gateway. It never reaches a service, so the services do less work.
Why They Are Easy to Confuse
The two boxes overlap in some features:
- A Layer 7 load balancer can route by path, like a gateway.
- An API gateway often spreads traffic across copies of a service, as a load balancer does.
- Some products can do both jobs.
So think about responsibilities, not products. Picking a healthy copy is the load balancer's job. Checking, limiting, and routing API requests to the right service is the gateway's job. Even when one product does both, these are still two separate jobs.
Using Both, in Order
Real systems usually use both, in a standard order. A load balancer sits in front of several API gateway instances.
The reason is capacity and failure. Every request passes through the gateway. One gateway instance would be a single point of failure, which is a part whose failure stops the whole system. So the team runs several gateway instances, and the load balancer spreads traffic across them. If one gateway instance fails its health checks, the load balancer stops sending traffic to it.
Suppose one gateway instance can handle 4,000 requests per second, and peak traffic is 10,000 requests per second. The team needs 10,000 / 4,000 = 2.5, so 3 instances. To survive one failure, the team runs 4.
Behind the gateway, balancing happens again. Each service runs several copies, and requests to that service are spread across its copies. On a container platform, like Kubernetes, the platform usually provides this second layer of balancing.
Load Balancer vs. API Gateway
| Load balancer | API gateway | |
|---|---|---|
| Decides | Which copy of a server | Which service, and whether the request is allowed |
| Reads the request | Layer 4: no. Layer 7: yes, for routing | Yes, always |
| Behind it | Identical copies | Different services |
| Also does | Health checks, failover | Authentication, rate limiting, caching, combining responses |
| Main benefit | No single server is overloaded | One entry point, with shared rules applied once |
| Skip it when | Almost never, if you have copies | You have only one service |
Choosing
- You run more than one copy of anything: add a load balancer.
- Several services sit behind one public API: add an API gateway, so clients do not need to know about each service.
- Several services need the same rules, like authentication and rate limits: add an API gateway, and write those rules once.
- You have exactly one service: skip the gateway for now. It would add cost and an extra network step, and give no new benefit.
Whether a gateway is worth adding at all is its own decision. The API Gateway vs Direct Service Exposure lesson covers it.
Using This in an Interview
When you draw both boxes, describe each one in a short phrase. For example: "A load balancer spreads traffic across the gateway instances. The gateway handles authentication, rate limits, and routing."
Two follow-up questions are common:
- "What happens when a gateway instance fails?" It fails its health checks, and the load balancer stops sending traffic to it. The other instances handle the requests.
- "Why not use one box for both?" One product can do both jobs. But the jobs are still different: picking a healthy copy, and applying API rules.
If your design has only one service, say that you are leaving the gateway out, and say what would make you add one.
Key Takeaways
- A load balancer distributes incoming traffic across multiple backend servers, so no server is overloaded. It also runs health checks and removes unhealthy servers.
- An API gateway is the single entry point for different services. It routes each request, and handles shared concerns like authentication and rate limiting.
- Both are reverse proxies. A load balancer picks a copy. A gateway picks a service and applies rules.
- A Layer 7 load balancer can also route by path, so focus on responsibilities, not products.
- Real systems usually put a load balancer in front of several gateway instances, and balance traffic again behind the gateway.
- Add a load balancer whenever you run copies. Add a gateway when several services share one public API or the same rules.
The load balancer sends requests only to healthy servers, and the gateway decides what each request is allowed to do. The next lesson, API Gateway vs Direct Service Exposure, looks at when a gateway is worth adding at all.
Practice Questions
Try each question first, then open the answer.
1. A team runs three identical copies of one web service. There is no need for authentication at the entry point, and there is only one service. Does the team need a load balancer, an API gateway, or both?
<details> <summary>Show answer</summary>A load balancer. The three copies run the same code, so the only decision is which copy handles each request. A load balancer makes that choice and removes unhealthy copies. With only one service and no shared rules, a gateway would add cost without a clear benefit.
</details>2. A load balancer uses round robin. Most requests take 50 ms, but some reports take 10 seconds. One server keeps getting several long reports at once and becomes overloaded. Which algorithm would help?
<details> <summary>Show answer</summary>Least connections. Round robin sends requests in turn and ignores how busy each server is. Least connections sends each new request to the server with the fewest active connections. A server that is busy with long reports has more connections, so it receives fewer new requests.
</details>3. A load balancer checks each server every 5 seconds, and removes a server after 3 failed checks in a row. A server crashes. About how long can the load balancer keep sending requests to it?
<details> <summary>Show answer</summary>About 10 to 15 seconds. The load balancer needs 3 failed checks, and they are 5 seconds apart. If the server crashes just before a check, the checks fail at about 0, 5, and 10 seconds. If it crashes just after a check, they fail at about 5, 10, and 15 seconds. Requests sent in that time can fail, so clients should retry safely.
</details>4. One API gateway instance can handle 4,000 requests per second. Peak traffic is 10,000 requests per second. How many instances should the team run, if the system must survive one failed instance?
<details> <summary>Show answer</summary>4 instances. Peak traffic needs 10,000 / 4,000 = 2.5 instances, which rounds up to 3. If one of the 3 fails, the other 2 can handle only 8,000 requests per second. So the team adds one more instance, for a total of 4.
</details>5. A mobile app calls three services: users, orders, and payments. Each service currently checks login tokens with its own copy of the code. What should the team add, and what does it improve?
<details> <summary>Show answer</summary>An API gateway, with a load balancer in front of its instances. The gateway gives the app one entry point, and routes each path to the right service. It checks login tokens once, so the three services no longer repeat that code. Rate limits can also be added there. The load balancer keeps the gateway from becoming a single point of failure.
</details>Discussion
On This Page