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
Strong vs Eventual Consistency
consistency
replication
availability
strong consistency
+1
You update your profile picture. The page reloads, and it still shows the old picture.
Nothing is broken. Your write went to one machine, and your read came back from another machine. That second machine had not received the change yet.
Almost every large system keeps copies of its data on several machines. Keeping copies is called replication, and each copy is called a replica. The Redundancy and Replication lesson explains why systems keep copies. This lesson is about the question that copies create: after a write, what does the next read see?
There are two main answers, and they have different costs:
- Strong consistency: every read returns the most recent write. To promise this, the system makes each write wait.
- Eventual consistency: a read is answered immediately, but it may briefly return the old value. The copies catch up shortly after.
Strong Consistency
A system is strongly consistent when every completed write is visible to every later read. It does not matter which client reads, or which replica answers.
Here is a common way to provide it. The write goes to the primary, which is the node that accepts writes. The primary sends the change to its replicas. It does not confirm the write to the client until enough replicas report that they have saved it. This is called synchronous replication, because the write waits for the copying.
Quorums
"Enough replicas" usually means a quorum. A quorum is the minimum number of servers that must successfully complete an operation. A common choice is a majority quorum. With 5 replicas, a majority is 3. So a write succeeds only after 3 of the 5 replicas confirm it.
A write quorum alone does not make every read correct, because 2 replicas may still be behind. So the system also reads from a quorum. Here is the rule. Add the number of replicas that confirm a write to the number that answer a read. If the total is more than the number of replicas, every read reaches at least one replica with the latest write. With 5 replicas, 3 for writes plus 3 for reads is 6, which is more than 5. The Quorum lesson explains this in more detail.
What Strong Consistency Gives You
- Reads are always correct. No user ever sees an old value.
- Simpler application code. The application never needs to handle a stale read, which is a read that returns an old value.
What Strong Consistency Costs You
- Slower writes. Each write waits for the replicas it needs, plus the network time to reach them. Replicas in the same data center add about a millisecond. But a replica on another continent adds a full round trip. For example, a round trip between Virginia and Frankfurt takes about 90 ms. So a write that must wait for Frankfurt takes about 90 ms longer. This is why strong consistency across distant data centers increases write latency. No engineering can remove this delay, because it comes from distance.
- Refused writes. If too many replicas are unreachable, the system has a choice: accept the write and break its promise, or refuse the write. A strongly consistent system refuses. With a majority quorum of 3 out of 5, the system keeps working with 2 replicas down, but not with 3 down.
The classic use for strong consistency is money. You withdraw 100 dollars from an account that holds 500 dollars. Every later read, anywhere, must show 400 dollars. If some machine still shows 500 dollars, the same money could be spent twice.
Eventual Consistency
A system is eventually consistent when its copies converge, which means they all reach the same value. If no new updates are made, all copies of the data eventually become identical. The promise is that the copies will agree, not when they will agree.
The mechanism is the same replication, without the waiting. The write goes to one node, and that node confirms it immediately. The change travels to the other replicas in the background. This is called asynchronous replication, because the copying happens after the confirmation. A read that reaches a replica before the change arrives returns the old value.
What Eventual Consistency Gives You
- Fast writes. A write only needs one machine to confirm it.
- Better availability. The system keeps accepting reads and writes while some replicas are unreachable, because no node waits for another.
What Eventual Consistency Costs You
- A stale window. For a short time, updates are still spreading, so two users may briefly see different data.
- More careful application code. The application must behave correctly when a value is briefly old.
The Domain Name System (DNS), which maps domain names to server addresses, works this way. When a domain's address changes, some DNS servers keep returning the old address for minutes or hours. Nobody calls DNS broken, because the delay is known and planned for.
How Long Is "Eventually"?
Replication lag is the delay between a write on one node and its arrival at the other copies. This number decides whether eventual consistency is acceptable.
- Inside one data center, lag is usually a few milliseconds.
- Across regions, lag is usually tens to a few hundred milliseconds.
For most data, the stale window ends before a user can even reload the page. But lag is not always small. It grows under heavy write load, when a restarted replica is catching up, or when a network link is slow. A replica can fall minutes behind. If lag affects correctness anywhere in your design, monitor it.
Read Your Own Writes
Users rarely notice when other people see slightly old data. They notice when they see it themselves. For example, you post a comment, the page refreshes, and your comment is missing. The system works exactly as designed, but it looks like a bug.
The fix is small. For a short time after a user writes, send that user's reads to the primary. Everyone else keeps reading from replicas. This is called read-your-own-writes consistency.
Almost all traffic still uses the cheaper replica reads. And the one kind of staleness that users actually notice is removed. The cost is a small amount of routing logic, and slightly more load on the primary.
There are also levels between the two extremes:
- Causal consistency keeps related operations in order. For example, a reply never appears before the message it answers.
- Bounded staleness sets a limit on how old a read may be, for example 5 seconds.
Some databases, like Azure Cosmos DB and MongoDB, let you choose a consistency level for each operation.
One Page, Several Answers
A common mistake is choosing one consistency model for a whole system. Real designs choose for each piece of data.
Look at a single shopping page:
- The account balance should be strongly consistent, because a wrong number is a real problem.
- The stock count should be strongly consistent at checkout, because selling the last item twice costs money.
- The view counter can be eventually consistent, because nobody notices a few seconds of delay.
- The recommendations can be minutes old.
That is several answers on one page. So name the data, not the system. For example: "Balance reads go to the primary and are strongly consistent. Everything else is served from replicas."
Strong vs. Eventual Consistency
| Strong consistency | Eventual consistency | |
|---|---|---|
| After a write | Every read returns the new value | A read may briefly return the old value |
| Write speed | Waits for replicas to confirm | One node confirms immediately |
| Replicas unreachable | May refuse writes | Keeps accepting reads and writes |
| Copying style | Synchronous | Asynchronous, in the background |
| Example systems | Google Spanner, etcd, a single primary database | Cassandra and DynamoDB by default, DNS, most caches |
| Good fit | Money, stock, bookings | Feeds, counters, recommendations |
Think of the two models as the two ends of a range. Most real systems sit somewhere between them, on purpose.
Choosing Between Them
- The data counts something that must be exact, like money, stock, seats, or bookings: choose strong consistency.
- The data is content or a casual count, like feeds, likes, views, or recommendations: choose eventual consistency.
- Users read back their own changes: choose eventual consistency, plus read-your-own-writes.
- Replicas are in several regions, and writes must stay fast: choose eventual consistency, because strong consistency waits for the round trip on every write.
- You cannot decide yet: start with strong consistency, and relax specific reads when measurements show the need.
Three other lessons explain the theory behind this choice. The CAP Theorem lesson explains the choice during a network partition, which is a failure that cuts machines off from each other. The PACELC Theorem lesson extends it to normal operation, where requests trade latency against consistency. The Quorum lesson explains how many replicas must confirm each read and write.
Using This in an Interview
Do not answer "strong" or "eventual" for the whole system. Split the answer. Name the one or two pieces of data that must never be stale, and send those reads to the primary. Serve everything else from replicas. Then add read-your-own-writes for each user's own data, because that is the staleness users notice.
A common follow-up question is: "How stale can a replica get?" Answer with numbers: usually a few milliseconds inside a data center, but possibly minutes while a replica is catching up. That is why replication lag is monitored.
Key Takeaways
- Strong consistency promises that every read returns the newest write. Writes wait for replicas, and may be refused when too many replicas are unreachable.
- Across distant data centers, strong consistency increases write latency, because synchronous replication must cross the distance.
- A quorum is the minimum number of servers that must complete an operation. With 5 replicas, a majority quorum is 3.
- Eventual consistency confirms writes immediately. Updates spread over time, and readers may briefly see different data.
- If no new updates are made, all copies in an eventually consistent system become identical.
- Read-your-own-writes removes the staleness users notice most.
- Choose a consistency level for each piece of data, not for the whole system.
Consistency decides what readers see after a write. The next lesson, Primary-Replica vs Peer-to-Peer Replication, looks at how the machines that hold the copies are organized.
Practice Questions
Try each question first, then open the answer.
1. A database uses strong consistency, with replicas in Virginia and Frankfurt. A write in Virginia takes 5 ms locally, and each write must wait for Frankfurt to confirm. A round trip between the two takes about 90 ms. About how long does each write take, and why?
<details> <summary>Show answer</summary>About 95 ms. The local work takes 5 ms, and waiting for Frankfurt adds a round trip of about 90 ms. This is synchronous replication: the write is not confirmed until the distant replica has it. Faster servers cannot remove the 90 ms, because it comes from the distance between the data centers.
</details>2. A system has 5 replicas and uses a majority quorum for writes. How many replicas must confirm each write? Can the system still accept writes with 2 replicas down? With 3 replicas down?
<details> <summary>Show answer</summary>3 replicas must confirm each write. A majority of 5 is 3. With 2 replicas down, 3 are still running, so writes can still succeed. With 3 replicas down, only 2 are running, which is less than the quorum of 3. So the system refuses writes, to keep its consistency promise.
</details>3. A system has 5 replicas. Writes need 3 confirmations, and reads ask 3 replicas. Why does every read see the latest write? What changes if reads ask only 2 replicas?
<details> <summary>Show answer</summary>3 + 3 = 6, which is more than 5, so the groups always overlap. At least one of the 3 replicas that answer a read must also be one of the 3 that confirmed the write. So the read finds the latest value. If reads ask only 2 replicas, 3 + 2 = 5, which is not more than 5. The 2 replicas that answer a read could both be the ones that missed the write, so the read may return an old value.
</details>4. A user posts a comment and refreshes the page 50 ms later. The read goes to a replica, and replication lag is about 200 ms. What does the user see, and how can the design fix it?
<details> <summary>Show answer</summary>The comment is missing, because the replica has not received it yet. The system is eventually consistent, and the user read inside the stale window. The fix is read-your-own-writes. For a short time after a user writes, send that user's reads to the primary. Other users can keep reading from replicas, and will see the comment a moment later.
</details>5. A shopping page shows an account balance, a product's stock count, the product's view count, and recommendations. Which of these need strong consistency?
<details> <summary>Show answer</summary>The account balance and the stock count. A wrong balance is a real problem, and a wrong stock count at checkout can sell the last item twice. The view count and the recommendations can be eventually consistent, because a short delay does no harm. So the design sends balance and stock reads to the primary, and serves the rest from replicas.
</details>Discussion
On This Page