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 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
Introduction to API Gateway
api gateway
microservices
load balancing
authentication
+3
A shopping app shows one home screen. To build it, the app needs the user's profile, their recent orders, and some product recommendations. Each of those comes from a different backend service, and each service has its own address.
So the phone app makes three separate calls to three addresses. Each service checks the user's login token with its own copy of the same code. And when the team moves the order service to a new address, every app must be updated.
This lesson explains the part that fixes these problems: the API gateway. It covers what a gateway is, how a request flows through it, and how it differs from a load balancer.
What an API Gateway Is
An API gateway is a server-side component that acts as an intermediary between clients and backend services.
- Clients are web browsers, mobile apps, or other services that call your system.
- Backend services are your microservices and APIs. Microservices are small, separate services that each do one job, like managing users or processing orders.
The main purpose of an API gateway is to provide a single entry point for external consumers to access the backend system. It receives each client request, forwards it to the appropriate microservice, and returns that service's response to the client.
Why Systems Use an API Gateway
Without a gateway, every client must know the address of every service it uses. Every service must also repeat the same shared work, like checking login tokens.
A gateway changes this in three ways.
- One address for clients. Clients call the gateway, not the individual services. The gateway knows where each service runs, so the team can move or split services without changing the clients.
- Shared work in one place. The gateway handles jobs that every service would otherwise repeat, like routing, authentication, and rate limiting. Rate limiting means setting a maximum number of requests that a client can make in a period of time. Each microservice can then focus on its own work.
- Better performance and scalability. Shared jobs run once at the gateway instead of in every service. The gateway can also cache responses and spread requests across several copies of a service.
How a Request Flows Through a Gateway
Here is what a typical gateway does with one request.
- Receive. The client sends a request, like
GET /orders?user=42, to the gateway's address. - Authenticate. The gateway checks the user's login token. If the token is missing or invalid, the gateway rejects the request immediately.
- Rate limit. The gateway checks whether this client has made too many requests recently. If so, it rejects the request with an error, like
429 Too Many Requests. - Route. The gateway uses the URL of the request to choose the right service and forwards the request to it.
- Return. The service responds, and the gateway sends the response back to the client.
The routing rules are often a simple table that maps URL paths to services.
GET /users/42 -> user service
GET /orders?user=42 -> order service
GET /products/9001 -> product catalog service
Requests that fail authentication or rate limiting are stopped at the gateway, so they never use any backend service's resources.
Gateways can do much more. For example, a gateway can combine responses from several services into one response. It can convert between formats, like XML and JSON. It can also send old and new clients to different versions of an API. The Usage of API Gateway lesson covers these jobs.
API Gateway vs. Load Balancer
API gateways and load balancers are easy to confuse. Both sit in front of your servers, and both decide where a request goes. But they decide different things.
An API gateway is focused on routing. It sends each request to the appropriate microservice.
A load balancer is focused on distributing. It spreads requests evenly across a group of backend servers.
They also handle different kinds of requests.
- An API gateway handles API requests. Each request has a specific URL that identifies which API the client wants. The gateway routes the request to the right microservice based on that URL.
- A load balancer handles requests sent to a single, well-known IP address. It routes each request to one of many backend servers, based on things like server performance and availability.
| API gateway | Load balancer | |
|---|---|---|
| Focused on | Routing | Distributing |
| Decides using | The URL that identifies the API | Server performance and availability |
| Behind it | Different services doing different jobs | Many servers doing the same job |
| Question it answers | Which service should handle this? | Which server should handle this? |
The "Behind it" row is the easiest way to tell them apart. A load balancer chooses between servers that are the same. A gateway chooses between services that are different.
They Work Together
Real systems usually use both.
- The gateway runs as several copies, for capacity and reliability. A load balancer spreads client traffic across those copies.
- Each microservice also runs as several copies. After the gateway picks the right service, a load balancer, or the gateway itself, picks one healthy copy of that service.
The Introduction to Load Balancing lesson explains how load balancers work.
Common API Gateways
- Managed cloud services, like Amazon API Gateway, Azure API Management, and Google Cloud Apigee. The cloud provider runs and scales them.
- Self-hosted software, like Kong, or gateways built on Nginx or Envoy. Your team runs them.
The Costs of an API Gateway
A gateway also brings costs.
- A possible single point of failure. All traffic passes through the gateway, so an outage there can affect the whole system. Teams run several gateway copies to avoid this.
- An extra hop. Every request passes through one more server, which adds a little latency.
- One more system to run. The gateway's rules must be configured, tested, and kept up to date.
The Advantages and Disadvantages of Using API Gateway lesson covers these trade-offs in detail.
Key Takeaways
- An API gateway is a server-side component that acts as an intermediary between clients and backend services.
- Its main purpose is to provide a single entry point for external consumers to access the backend system.
- It receives a request, forwards it to the appropriate microservice, and returns the response to the client.
- It handles shared jobs, like routing, authentication, and rate limiting, in one place, so each microservice can focus on its own work.
- An API gateway is focused on routing requests to the right service, based on the URL. A load balancer is focused on distributing requests across identical servers.
- Real systems use both, and run several gateway copies, so the gateway is not a single point of failure.
An API gateway gives clients one address and handles shared rules in one place, so services can stay small and simple. The next lesson, Usage of API Gateway, covers the main jobs that gateways do in real systems.
Practice Questions
Try each question first, then open the answer.
1. A mobile app's home screen needs data from the user service, the order service, and the recommendation service. How can an API gateway reduce the work the app does?
<details> <summary>Show answer</summary>The app can make one request to the gateway instead of three. The gateway calls the three services, combines their responses, and returns one response to the app. The app also needs to know only one address. If a service moves, the gateway's routing changes, and the app stays the same.
</details>2. Five services each check login tokens with their own copy of the same code. A security bug is found in that code. How would an API gateway have helped?
<details> <summary>Show answer</summary>The token check would run in one place. With a gateway, authentication happens at the gateway before any request reaches a service. The team would fix the bug once, in the gateway, instead of in five services. The services could then focus on their own business logic.
</details>3. Which one fits each need, an API gateway or a load balancer? (a) Requests to /payments must reach the payment service. (b) Ten identical copies of the payment service must share the traffic evenly.
(a) An API gateway, and (b) a load balancer. Choosing the payment service from the URL is routing, which is the gateway's job. Spreading requests across ten identical copies is distributing, which is the load balancer's job.
</details>4. A system already has an API gateway. Why does it usually still need load balancers?
<details> <summary>Show answer</summary>Because both the gateway and the services run as several copies. A load balancer spreads client traffic across the gateway copies, so no single gateway is overloaded or becomes a single point of failure. After the gateway picks a service, requests must also be spread across that service's copies.
</details>5. All traffic for an app passes through a single API gateway instance. What is the risk, and how can the team reduce it?
<details> <summary>Show answer</summary>The gateway is a single point of failure. If that one instance crashes or slows down, the whole app is affected. The team should run several gateway instances, ideally in more than one data center, behind a load balancer. Keeping the gateway's work light also limits the extra latency it adds.
</details>Discussion
On This Page