System Design Cheatsheet
A comprehensive guide to system design principles, patterns, and best practices.
System Design Fundamentals
Key Characteristics
Core system design principles and characteristics.
// System Design Key Characteristics
1. Scalability
- Horizontal (adding more machines)
- Vertical (adding more power)
2. Reliability
- Fault tolerance
- Redundancy
- Failover mechanisms
3. Availability
- High availability (99.99%)
- Load balancing
- Replication
4. Performance
- Latency
- Throughput
- Response time
5. Maintainability
- Simplicity
- Modularity
- Testability
Capacity Planning
Estimating system requirements.
// Traffic Estimates
Daily Active Users (DAU) = 1 million
Requests per user = 10/day
Total daily requests = 10 million
// Storage Estimates
Average request size = 100 KB
Daily storage = 10M * 100KB = 1TB/day
5-year storage = 1TB * 365 * 5 = 1.8PB
// Bandwidth Estimates
Peak requests/second = Daily requests / 86400
Write bandwidth = Peak RPS * avg request size
// Memory Estimates
80-20 Rule: 20% data serves 80% traffic
Cache size = Daily active data * 20%
Architecture Patterns
Microservices
Microservices architecture patterns.
// Microservices Patterns
1. Service Decomposition
- By business capability
- By subdomain
- By data autonomy
2. Inter-Service Communication
- Synchronous (REST, gRPC)
- Asynchronous (Message Queues)
- Event-Driven
3. Database Patterns
- Database per Service
- Shared Database
- CQRS Pattern
4. Deployment Patterns
- Single Service per Host
- Multiple Services per Host
- Service per Container
5. Service Discovery
- Client-Side Discovery
- Server-Side Discovery
- Service Registry
Distributed Systems
Key distributed system concepts.
// CAP Theorem
Choose 2 of 3:
- Consistency
- Availability
- Partition Tolerance
// Consistency Patterns
1. Strong Consistency
- All nodes see same data
- Higher latency
2. Eventual Consistency
- Nodes will converge
- Better performance
3. Casual Consistency
- Only related actions
- Middle ground
// Partition Handling
1. AP (Available + Partition Tolerant)
- Continue in split-brain
- Resolve conflicts later
2. CP (Consistent + Partition Tolerant)
- Reject requests
- Maintain consistency
Data Management
Database Selection
Choosing the right database.
// Database Types
1. Relational (RDBMS)
Use when:
- ACID compliance needed
- Complex queries/joins
- Structured data
2. NoSQL
Document (MongoDB):
- Flexible schema
- Nested data
- No complex joins
Key-Value (Redis):
- Fast access
- Simple data
- Caching
Wide-Column (Cassandra):
- High write throughput
- Structured data
- Horizontal scaling
Graph (Neo4j):
- Connected data
- Complex relationships
- Path finding
Data Partitioning
Strategies for data partitioning.
// Partitioning Methods
1. Horizontal (Sharding)
- Range based
- Hash based
- Directory based
2. Vertical Partitioning
- By feature
- By service
- By data type
// Sharding Considerations
- Joins across shards
- Referential integrity
- Rebalancing data
- Hot spots
// Consistent Hashing
- Minimize data movement
- Add/remove nodes easily
- Virtual nodes for balance
Performance & Scaling
Caching Strategies
Different caching approaches.
// Cache Locations
1. Client Side
- Browser cache
- Mobile app cache
- Local storage
2. CDN Caching
- Static assets
- API responses
- Geographic distribution
3. Application Cache
- In-memory cache
- Distributed cache
- Session data
// Cache Patterns
1. Cache-Aside
Load:
if (cache.has(key))
return cache.get(key)
else
data = db.get(key)
cache.set(key, data)
return data
2. Write-Through
Write:
db.write(key, data)
cache.set(key, data)
3. Write-Behind
Write:
cache.set(key, data)
async.write(db, key, data)
4. Refresh-Ahead
Predict and pre-fetch
Load Balancing
Load balancing methods and algorithms.
// Load Balancer Types
1. Transport Layer (L4)
- TCP/UDP level
- Fast processing
- Limited intelligence
2. Application Layer (L7)
- HTTP/HTTPS level
- Content-aware
- More features
// Balancing Algorithms
1. Round Robin
- Simple rotation
- Equal distribution
2. Weighted Round Robin
- Based on capacity
- Unequal servers
3. Least Connections
- Dynamic allocation
- Connection counting
4. Least Response Time
- Performance based
- Latency aware
// Health Checks
- Active monitoring
- Passive monitoring
- Circuit breaking
- Failover handling
Security & Monitoring
Security Patterns
Common security considerations.
// Authentication
1. OAuth 2.0 / OpenID Connect
- Authorization flows
- Token management
- Scope control
2. API Security
- Rate limiting
- API keys
- JWT tokens
3. Data Security
- Encryption at rest
- Encryption in transit
- Key management
// Monitoring
1. Metrics to Track
- Request rate
- Error rate
- Response time
- Resource usage
2. Logging
- Application logs
- System logs
- Audit logs
3. Alerting
- Thresholds
- Anomaly detection
- Incident response