Overview
Design a system that evolves from serving a single user to millions of users on AWS. This problem demonstrates the iterative approach to scaling, starting with a simple single-server setup and progressively adding components as bottlenecks emerge.Iterative Scaling Approach
Scaling requires a methodical approach:Benchmark/Load Test
Profile for Bottlenecks
Address Bottlenecks
Repeat
Step 1: Use Cases and Constraints
Use Cases
In Scope
- User makes a read or write request
- Service processes request, stores user data, returns results
- Service needs to evolve from small user base to millions of users
- Discuss general scaling patterns for large number of users and requests
- Service has high availability
Constraints and Assumptions
Assumptions:- Traffic is not evenly distributed
- Need for relational data
- Scale from 1 user to tens of millions of users
- Denote increase as: Users+, Users++, Users+++, etc.
- 10 million users
- 1 billion writes per month
- 100 billion reads per month
- 100:1 read to write ratio
- 1 KB content per write
Usage Calculations
Back-of-the-envelope calculations
Back-of-the-envelope calculations
- 1 TB of new content per month
- 1 KB per write × 1 billion writes per month
- 36 TB of new content in 3 years
- 400 writes per second on average
- 40,000 reads per second on average
- 2.5 million seconds per month
- 1 request per second = 2.5 million requests per month
- 40 requests per second = 100 million requests per month
- 400 requests per second = 1 billion requests per month
Step 2: Initial Design - Single Server
Goals (1-2 Users)
- Start simple with single box
- Vertical scaling when needed
- Monitor to determine bottlenecks
Components
Web Server
MySQL Database
Elastic IP
DNS
Vertical Scaling
Advantages
Advantages
- Simple to implement
- Choose bigger instance size as needed
Monitoring
Monitoring
- CPU usage
- Memory usage
- I/O
- Network
Disadvantages
Disadvantages
- Can get very expensive
- No redundancy/failover
- Limited by single machine capacity
Security
- Open Ports
- Restrictions
- 80 for HTTP
- 443 for HTTPS
- 22 for SSH (whitelisted IPs only)
SQL vs NoSQL
Start with MySQL Database given relational data requirement.Step 3: Users+ - Separate Components
Bottleneck Analysis
Problem: Single box becoming overwhelmed- MySQL taking more memory and CPU
- User content filling disk space
- Vertical scaling expensive
- Cannot scale MySQL and Web Server independently
Goals
Lighten load on single box
Store static content separately
Move database to separate box
New Components
Object Store (S3)
Object Store (S3)
- User files
- JavaScript
- CSS
- Images
- Videos
- Highly scalable and reliable
- Server-side encryption
RDS MySQL
RDS MySQL
- Simple to administer and scale
- Multiple availability zones
- Encryption at rest
- Automatic backups
Virtual Private Cloud (VPC)
Virtual Private Cloud (VPC)
- Public subnet for Web Server (internet-facing)
- Private subnet for database and other internal services
- Security groups control access between components
Trade-offs
- Increased complexity - Need to update Web Server to point to S3 and RDS
- Additional security - Must secure new components
- Higher AWS costs - Weigh against managing similar systems yourself
Step 4: Users++ - Horizontal Scaling
Bottleneck Analysis
Problem: Single Web Server bottlenecks during peak hours- Slow responses
- Occasional downtime
- Need higher availability and redundancy
Goals
Load Balancer
- Highly available
- Terminate SSL to reduce backend load
- Simplify certificate administration
Multiple Web Servers
- Horizontal scaling
- Remove single points of failure
MySQL Failover
- Improve redundancy
- Enable read scaling
Application Servers
- Independent scaling
- Web Servers act as reverse proxy
- Separate Read APIs from Write APIs
Content Delivery Network (CDN)
- CloudFront
- Dynamic Content
- Serve static content globally
- Reduce latency
- Reduce load on origin servers
Step 5: Users+++ - Caching Layer
Bottleneck Analysis
Problem: Read-heavy system (100:1 ratio)- Database suffering from high read requests
- Poor performance from cache misses
Goals
Add Memory Cache (Elasticache) to reduce load and latency:Frequently Accessed Content
Frequently Accessed Content
- Try configuring MySQL Database cache first
- If insufficient, implement Memory Cache
Session Data
Session Data
- Makes Web Servers stateless
- Enables Autoscaling
Performance
Performance
- 4x faster than SSD
- 80x faster than disk
MySQL Read Replicas
Add Read Replicas
Separate Read/Write Logic
Add Load Balancers
Additional Scaling
- Add more Web Servers to improve responsiveness
- Add more Application Servers for business logic
Step 6: Users++++ - Autoscaling
Bottleneck Analysis
Problem: Traffic spikes during business hours- Want to automatically scale up/down based on load
- Reduce costs by powering down unused instances
- Automate DevOps as much as possible
AWS Autoscaling
Configuration
Configuration
- Create one group for each Web Server type
- Create one group for each Application Server type
- Place each group in multiple availability zones
- Set min and max number of instances
Scaling Triggers
Scaling Triggers
- Simple time of day for predictable loads
- OR metrics over time period:
- CPU load
- Latency
- Network traffic
- Custom metrics
Disadvantages
Disadvantages
- Introduces complexity
- Takes time to scale up to meet increased demand
- Takes time to scale down when demand drops
DevOps Automation
- Configuration Management
- Monitoring
- Chef
- Puppet
- Ansible
Step 7: Users+++++ - Advanced Scaling
Continued Scaling Challenges
As service grows towards constraints, continue iterative scaling:Database Scaling
Solutions:Data Warehousing
Data Warehousing
SQL Scaling Patterns
SQL Scaling Patterns
- Federation - Split databases by function
- Sharding - Distribute data across databases
- Denormalization - Optimize read performance
- SQL Tuning - Optimize queries and indexes
NoSQL Database
NoSQL Database
- High read/write throughput requirements
- Flexible schema needs
- Key-value or document data models
Asynchronous Processing
Separate Application Servers for batch processes:Client uploads data
Application Server queues job
Worker Service processes
- Performs computation (create thumbnail)
- Updates Database
- Stores result in Object Store
Memory Cache Scaling
- Popular Content
- Traffic Spikes
- Read Replicas
Related Topics
SQL Scaling Patterns
- Read replicas
- Federation
- Sharding
- Denormalization
- SQL Tuning
NoSQL Options
- Key-value store
- Document store
- Wide column store
- Graph database
Caching Strategies
- Client caching
- CDN caching
- Web server caching
- Database caching
- Application caching
Asynchronous Processing
- Message queues
- Task queues
- Back pressure
- Microservices
Key Takeaways
- Iterative approach is essential for scaling
- Benchmark → Profile → Address → Repeat
- Start simple with single server
- Separate concerns for independent scaling
- Horizontal scaling with Load Balancer and multiple servers
- Caching layer critical for read-heavy workloads
- Autoscaling handles traffic variability
- Database scaling requires multiple strategies
- Asynchronous processing separates real-time from batch work
- Monitoring at every stage identifies bottlenecks
- Security must evolve with architecture
