System Design Primer
Comprehensive reference from https://github.com/donnemartin/system-design-primer (344k stars)
Key Principles
"Everything is a trade-off" — Key principle throughout system design.
Fundamental Trade-offs
| Trade-off |
Description |
| Performance vs Scalability |
Performance = fast for single user; Scalability = fast under heavy load |
| Latency vs Throughput |
Latency = time to complete action; Throughput = actions per unit time |
| Availability vs Consistency |
CAP Theorem — choose 2 of 3 |
Consistency Patterns
| Pattern |
Description |
Use Cases |
| Weak |
Reads may not see writes |
VoIP, gaming |
| Eventual |
Writes propagate asynchronously |
DNS, email |
| Strong |
Synchronous replication |
RDBMS, file systems |
Availability Metrics
| Availability |
Downtime/Year |
Downtime/Month |
| 99.9% (3 nines) |
8h 46m |
43m 50s |
| 99.99% (4 nines) |
52m 36s |
4m 23s |
| 99.999% (5 nines) |
5m 15s |
26s |
Core Topics
Networking & Delivery
- DNS: Hierarchical name resolution, A/CNAME/MX/NS records
- CDN: Push (upload to CDN) vs Pull (lazy load on first request)
- Load Balancer: Layer 4 (transport) vs Layer 7 (application)
- Reverse Proxy: Hide backend servers, SSL termination, caching
Databases
- RDBMS: ACID transactions, complex joins
- NoSQL: Key-value, document, wide-column, graph stores
- Scaling: Replication, Federation, Sharding, Denormalization
Caching Strategies
# Cache-aside pattern (lazy loading)
def get_user(user_id):
user = cache.get(f"user.{user_id}")
if user is None:
user = db.query("SELECT * FROM users WHERE user_id = ?", user_id)
cache.set(f"user.{user_id}", json.dumps(user))
return user
| Strategy |
Description |
| Cache-aside |
Lazy loading, application manages |
| Write-through |
Synchronous write to cache + DB |
| Write-behind |
Async write to cache, then DB |
| Refresh-ahead |
Pre-expire popular entries |
4-Step System Design Interview Approach
- Outline use cases, constraints, assumptions — Scope problem, ask clarifying questions
- Create high-level design — Sketch main components
- Design core components — Dive into details
- Scale the design — Address bottlenecks: load balancing, caching, sharding
Reference Tables
Powers of Two
| Power |
Value |
Approx |
| 10 |
1,024 |
1 KB |
| 20 |
1,048,576 |
1 MB |
| 30 |
1,073,741,824 |
1 GB |
| 40 |
1,099,511,627,776 |
1 TB |
Latency Reference
| Operation |
Latency |
| L1 cache reference |
0.5 ns |
| L2 cache reference |
7 ns |
| Main memory reference |
100 ns |
| SSD random read |
150 μs |
| HDD seek |
10 ms |
| Round trip datacenter |
500 μs |
| Round trip internet |
50 ms |
Solutions Reference
Located at /opt/system-design-primer/solutions/system_design/:
mint — Payment system
pastebin — URL shortener
query_cache — Key-value store
twitter — Twitter timeline/search
social_graph — Social network
web_crawler — Web crawler
sales_rank — Amazon sales ranking
scaling_aws — AWS scalable system
template — Template for new designs
Additional Resources
- Real World Architectures: MapReduce, Spark, BigTable, Cassandra, DynamoDB, HDFS, Kafka
- Company Engineering Blogs: Google, Facebook, Netflix, Twitter, Uber, Airbnb
1---2name: system-design-primer3description: Large-scale system design reference. Use when designing distributed systems, scaling applications, or preparing for system design interviews. Reference concepts like CAP theorem, caching strategies, load balancing, database sharding, and more.4---56# System Design Primer78Comprehensive reference from https://github.com/donnemartin/system-design-primer (344k stars)910## Key Principles1112**"Everything is a trade-off"** — Key principle throughout system design.1314## Fundamental Trade-offs1516| Trade-off | Description |17|-----------|-------------|18| **Performance vs Scalability** | Performance = fast for single user; Scalability = fast under heavy load |19| **Latency vs Throughput** | Latency = time to complete action; Throughput = actions per unit time |20| **Availability vs Consistency** | CAP Theorem — choose 2 of 3 |2122## Consistency Patterns2324| Pattern | Description | Use Cases |25|---------|-------------|-----------|26| **Weak** | Reads may not see writes | VoIP, gaming |27| **Eventual** | Writes propagate asynchronously | DNS, email |28| **Strong** | Synchronous replication | RDBMS, file systems |2930## Availability Metrics3132| Availability | Downtime/Year | Downtime/Month |33|--------------|---------------|----------------|34| 99.9% (3 nines) | 8h 46m | 43m 50s |35| 99.99% (4 nines) | 52m 36s | 4m 23s |36| 99.999% (5 nines) | 5m 15s | 26s |3738## Core Topics3940### Networking & Delivery41- **DNS**: Hierarchical name resolution, A/CNAME/MX/NS records42- **CDN**: Push (upload to CDN) vs Pull (lazy load on first request)43- **Load Balancer**: Layer 4 (transport) vs Layer 7 (application)44- **Reverse Proxy**: Hide backend servers, SSL termination, caching4546### Databases47- **RDBMS**: ACID transactions, complex joins48- **NoSQL**: Key-value, document, wide-column, graph stores49- **Scaling**: Replication, Federation, Sharding, Denormalization5051### Caching Strategies5253```python54# Cache-aside pattern (lazy loading)55def get_user(user_id):56 user = cache.get(f"user.{user_id}")57 if user is None:58 user = db.query("SELECT * FROM users WHERE user_id = ?", user_id)59 cache.set(f"user.{user_id}", json.dumps(user))60 return user61```6263| Strategy | Description |64|----------|-------------|65| **Cache-aside** | Lazy loading, application manages |66| **Write-through** | Synchronous write to cache + DB |67| **Write-behind** | Async write to cache, then DB |68| **Refresh-ahead** | Pre-expire popular entries |6970## 4-Step System Design Interview Approach71721. **Outline use cases, constraints, assumptions** — Scope problem, ask clarifying questions732. **Create high-level design** — Sketch main components743. **Design core components** — Dive into details754. **Scale the design** — Address bottlenecks: load balancing, caching, sharding7677## Reference Tables7879### Powers of Two8081| Power | Value | Approx |82|-------|-------|--------|83| 10 | 1,024 | 1 KB |84| 20 | 1,048,576 | 1 MB |85| 30 | 1,073,741,824 | 1 GB |86| 40 | 1,099,511,627,776 | 1 TB |8788### Latency Reference8990| Operation | Latency |91|-----------|---------|92| L1 cache reference | 0.5 ns |93| L2 cache reference | 7 ns |94| Main memory reference | 100 ns |95| SSD random read | 150 μs |96| HDD seek | 10 ms |97| Round trip datacenter | 500 μs |98| Round trip internet | 50 ms |99100## Solutions Reference101102Located at `/opt/system-design-primer/solutions/system_design/`:103104- `mint` — Payment system105- `pastebin` — URL shortener106- `query_cache` — Key-value store107- `twitter` — Twitter timeline/search108- `social_graph` — Social network109- `web_crawler` — Web crawler110- `sales_rank` — Amazon sales ranking111- `scaling_aws` — AWS scalable system112- `template` — Template for new designs113114## Additional Resources115116- **Real World Architectures**: MapReduce, Spark, BigTable, Cassandra, DynamoDB, HDFS, Kafka117- **Company Engineering Blogs**: Google, Facebook, Netflix, Twitter, Uber, Airbnb