Master scalability patterns with load balancing, caching, database scaling, microservices, and horizontal scaling strategies.
Implement scalability patterns to handle growth, improve performance, and maintain reliability under increasing load.
**Vertical Scaling (Scale Up):**
- Add more CPU, RAM to existing server
- Limits: Hardware ceiling, single point of failure
- Use case: Databases, legacy apps
**Horizontal Scaling (Scale Out):**
- Add more servers
- Benefits: No limit, fault tolerance
- Requires: Stateless design, load balancing
- Use case: Web servers, app servers
**Recommendation:** Design for horizontal scaling
## Cache Architecture
**Cache-Aside (Lazy Loading):**
**Write-Through:**
**Write-Behind:**
**Use Cases:**
- CDN: Static assets (images, CSS, JS)
- Redis: Session data, API responses
- Browser cache: User-specific data
- Application cache: Configuration, reference data
**Example - Redis Caching:**
```javascript
async function getUser(userId) {
// Try cache first
let user = await redis.get(`user:${userId}`);
if (!user) {
// Cache miss - get from database
user = await database.query('SELECT * FROM users WHERE id = ?', [userId]);
// Store in cache (TTL: 1 hour)
await redis.setex(`user:${userId}`, 3600, JSON.stringify(user));
}
return JSON.parse(user);
}
### 3. Database Scaling
```markdown
## Database Scaling Strategies
**Read Replicas:**
- Master: Write operations
- Replicas: Read operations
- Reduces load on master
- Eventual consistency
**Sharding (Horizontal Partitioning):**
- Split data across multiple databases
- Shard key (e.g., user_id % num_shards)
- Challenges: Joins, resharding
**Vertical Partitioning:**
- Split tables by columns
- Separate hot/cold data
- Example: User profile vs user activity logs
**CQRS (Command Query Responsibility Segregation):**
- Separate read and write models
- Optimized for different use cases
- Event sourcing integration
## Load Balancer Strategies
**Round Robin:**
Server 1 → Server 2 → Server 3 → Server 1...
**Least Connections:**
Route to server with fewest active connections
**IP Hash:**
Route based on client IP (sticky sessions)
**Weighted:**
More requests to more powerful servers
**Health Checks:**
- Monitor server health
- Remove unhealthy servers
- Automatic failover
**Example Architecture:**
Internet → CloudFlare CDN → AWS ALB (Application Load Balancer) → Auto Scaling Group → EC2 Instance 1 → EC2 Instance 2 → EC2 Instance 3