System Design Interview Guide: Complete Patterns and Approaches
System design questions are a crucial part of technical interviews for senior engineering roles. This guide covers fundamental patterns, common approaches, and building blocks for designing large-scale systems.
What is System Design?
System design involves making architectural decisions about how software systems should be built. It encompasses:
- Functional requirements: What the system should do
- Non-functional requirements: Performance, scalability, reliability
- Technical constraints: Budget, timeline, existing infrastructure
Common System Design Concepts
CAP Theorem
CAP Theorem states that a distributed system can only provide 2 of 3 guarantees:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ Consistency โ
โ โก โก โ
โ โ โ โ
โ โ โ โ
โ Availability โก โก โ
โ โ โ โ
โ โ โ โ
โ Partition โ
โ Tolerance โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- Consistency (C): All nodes see the same data
- Availability (A): Every request gets a response
- Partition Tolerance (P): System works despite network failures
ACID vs BASE
| ACID | BASE |
|---|---|
| Atomicity | Basically Available |
| Consistency | Soft state |
| Isolation | Eventual consistency |
| Durability |
Scalability Basics
Vertical vs Horizontal Scaling
Vertical Scaling (Scale Up) Horizontal Scaling (Scale Out)
โโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโ โโโโโโโโโ โโโโโโโโโ
โ โ โ โ โ โ โ โ
โ โโโโโโโโโโโ โ โ App โ โ App โ โ App โ
โ โ Server โ โ โ โ โ โ โ โ
โ โโโโโโโโโโโ โ โโโโโโโโโ โโโโโโโโโ โโโโโโโโโ
โ โ โ โ โ โ
โ More CPU/RAM โ Load Balancer
โ โ
โโโโโโโโโโโโโโโโโโโโโโโ
Database Scaling
Read Replicas Sharding
โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ
โ Main โ โ Shard 1 โ โ Shard 2 โ
โ Database โโโโReplicationโโโถ โ (Users A-M)โ โ(Users N-Z)โ
โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ
โ
Reads/Writes
โ
โโโโโโโดโโโโโโโ
โ โ
โผ โผ
โโโโโโโโโโโโ โโโโโโโโโโโโ
โ Read โ โ Read โ
โ Replica โ โ Replica โ
โโโโโโโโโโโโ โโโโโโโโโโโโ
Common Building Blocks
1. Load Balancer
# Load balancing strategies
class LoadBalancer:
def __init__(self, servers):
self.servers = servers
# Round Robin
def round_robin(self):
return self.servers[self.index % len(self.servers)]
# Least Connections
def least_connections(self):
return min(self.servers, key=lambda s: s.connections)
# Weighted Round Robin
def weighted_rr(self):
# Servers with higher weight get more requests
pass
# IP Hash (sticky sessions)
def ip_hash(self, client_ip):
return self.servers[hash(client_ip) % len(self.servers)]
2. Caching
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Cache Hierarchy โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ Browser โโโถ CDN โโโถ Load Balancer โโโถ App Server โ
โ โ โ โ โ
โ โผ โผ โผ โ
โ Cache Edge Cache Redis/Memcached โ
โ โ โ
โ โผ โ
โ Database โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
3. Database Patterns
-- Read Replica
SELECT * FROM users WHERE id = 1; -- Read from replica
-- Sharding by User ID
-- Users 1-1,000,000 -> Shard 1
-- Users 1,000,001-2,000,000 -> Shard 2
-- Vertical Partitioning
-- Users table: id, name, email
-- UserProfiles table: user_id, avatar, bio, settings
Design Approaches
Step 1: Requirements Clarification
Ask questions like:
- What are the key features?
- How many users?
- What are the read/write ratios?
- What are the latency requirements?
- Any geographic considerations?
Step 2: High-Level Design
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ User Interface โ
โโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Load Balancer โ
โโโโโโโโโฌโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Service A โ โ Service B โ โ Service C โ
โโโโโโโโฌโโโโโโโ โโโโโโโโฌโโโโโโโ โโโโโโโโฌโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Cache โ โ Cache โ โ Cache โ
โโโโโโโโฌโโโโโโโ โโโโโโโโฌโโโโโโโ โโโโโโโโฌโโโโโโโ
โ โ โ
โโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโ
โ Database โ
โ (Primary + โ
โ Replicas) โ
โโโโโโโโโโโโโโโโโโโ
Step 3: Component Design
Design each component:
1. API Server
- REST/GraphQL
- Authentication (JWT, OAuth)
2. Data Storage
- SQL vs NoSQL
- Caching strategy
3. Message Queue
- Async processing
- Event-driven
4. Search
- Full-text search (Elasticsearch)
- Suggestions
Step 4: Trade-offs
Common Trade-offs:
1. Consistency vs Performance
- Strong consistency = slower
- Eventual consistency = faster
2. Latency vs Cost
- More caching = lower latency, higher cost
- Less caching = higher latency, lower cost
3. Availability vs Consistency
- Always available = eventual consistency
- Consistent = may be unavailable during partitions
Common System Designs
1. URL Shortener
Requirements:
- Shorten long URLs
- Redirect to original URL
- Track click analytics
Design:
โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ
โ Client โโโโโโถโ API Server โโโโโโถโ Database โ
โ โโโโโโโ (Hash + โโโโโโโ (URL + โ
โ โ โ Redirect) โ โ Short ID) โ
โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโ
โ Cache โ
โ (Redis) โ
โโโโโโโโโโโโโโโโ
2. Twitter/News Feed
Requirements:
- User follows others
- See chronological feed
- High read throughput
Design:
โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ
โ Client โโโโโโถโ API Server โ
โโโโโโโโโโโโโโโโ โโโโโโโโฌโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโ
โผ โผ โผ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Fan-out โ โ Search โ โ Analytics โ
โ Service โ โ Service โ โ Service โ
โโโโโโโโฌโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Message โโโโโโถโ Cache โ
โ Queue โ โ (Redis) โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
3. Real-time Chat
Requirements:
- 1:1 messaging
- Group chats
- Online status
- Message history
Design:
โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ
โ WebSocket โโโโโโถโ Chat API โ
โ Connection โ โ Server โ
โโโโโโโโโโโโโโโโ โโโโโโโโฌโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโ
โผ โผ โผ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Presence โ โ Message โ โ Notificationโ
โ Service โ โ Service โ โ Service โ
โโโโโโโโโโโโโโโ โโโโโโโโฌโโโโโโโ โโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโ
โ Database โ
โ(Cassandra) โ
โโโโโโโโโโโโโโโ
Estimation Techniques
Traffic Estimation
ๅ่ฎพ:
- 1 million DAU
- ๆฏไธช็จๆทๆฏๅคฉ 10 ๆฌก่ฏทๆฑ
- ๅณฐๅผ = ๅนณๅ็ 3ๅ
่ฎก็ฎ:
- QPS = 1M ร 10 / 86400 โ 115 QPS (ๅนณๅ)
- ๅณฐๅผ QPS = 115 ร 3 = 345 QPS
Storage Estimation
ๅ่ฎพ:
- 100 million users
- ๆฏไธช็จๆท 100 ไธชๅธๅญ
- ๆฏไธชๅธๅญ 1KB
่ฎก็ฎ:
- Storage = 100M ร 100 ร 1KB = 10 TB
- ๅ ไธ 3 ๅนดๅพ็ๅญๅจ = 100+ TB
Bandwidth
ๅ่ฎพ:
- 100 QPS
- ๆฏๆฌกๅๅบ 10KB
่ฎก็ฎ:
- Bandwidth = 100 ร 10KB = 1MB/s
- ๅณฐๅผ = 3MB/s
Interview Tips
Do’s
- Clarify requirements - Ask questions before designing
- Think out loud - Show your reasoning
- Make trade-offs - Explain pros/cons
- Start simple - MVP first, then scale
- Know the numbers - Estimate capacity
Don’ts
- Don’t jump straight to code
- Don’t ignore non-functional requirements
- Don’t over-engineer the solution
- Don’t forget about failure scenarios
- Don’t ignore monitoring/operations
External Resources
- Designing Data-Intensive Applications
- System Design Interview
- High Scalability Blog
- AWS Well-Architected Framework
Comments