Scaling
Scale EZ-Console applications horizontally and vertically.
Overview
This guide covers scaling strategies for EZ-Console applications, including horizontal scaling, vertical scaling, database scaling, and load balancing.
Scaling Strategies
Horizontal Scaling
Add more application instances to handle increased load.
Vertical Scaling
Increase resources (CPU, memory) of existing instances.
Application Scaling
Horizontal Scaling
Docker Compose
version: '3.8'
services:
app:
build: .
deploy:
replicas: 3
environment:
- DATABASE_HOST=db
Kubernetes
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 5 # Scale to 5 instances
selector:
matchLabels:
app: myapp
template:
# ...
Auto Scaling
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: myapp-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myapp
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
Vertical Scaling
Increase container resources:
resources:
requests:
memory: "512Mi"
cpu: "500m"
limits:
memory: "1Gi"
cpu: "1000m"
Database Scaling
Read Replicas
Use read replicas for read-heavy workloads:
database:
driver: "mysql"
host: "db-primary.example.com" # Write to primary
read_replicas:
- host: "db-replica-1.example.com"
- host: "db-replica-2.example.com"
Connection Pooling
Optimize connection pool settings:
database:
max_open_connections: 200
max_idle_connections: 20
max_connection_life_time: "5m"
Database Sharding
For very large databases, consider sharding:
- Shard by organization ID
- Shard by date range
- Use consistent hashing
Load Balancing
Application Load Balancer
Nginx
upstream myapp {
least_conn;
server app1:8080;
server app2:8080;
server app3:8080;
}
server {
location /api {
proxy_pass http://myapp;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
HAProxy
backend myapp
balance roundrobin
server app1 app1:8080 check
server app2 app2:8080 check
server app3 app3:8080 check
Session Affinity
For stateful applications:
upstream myapp {
ip_hash; # Sticky sessions
server app1:8080;
server app2:8080;
}
Caching
Application Cache
// Use in-memory cache
cache.Set(key, value, 5*time.Minute)
// Use Redis for distributed cache
redis.Set(ctx, key, value, 5*time.Minute)
Database Query Cache
Enable query caching:
database:
enable_query_cache: true
query_cache_size: 100MB
CDN for Static Assets
Serve static files from CDN:
location /static {
proxy_pass https://cdn.example.com;
}
Performance Optimization
Database Indexes
Add indexes for frequently queried fields:
type Product struct {
Name string `gorm:"index"`
Category string `gorm:"index"`
Status string `gorm:"index"`
}
Query Optimization
// Use Select to limit fields
db.Select("id", "name").Find(&products)
// Use pagination
db.Offset(offset).Limit(limit).Find(&products)
// Use preloading
db.Preload("Category").Find(&products)
Connection Pooling
database:
max_open_connections: 200
max_idle_connections: 20
Monitoring Scaling
Metrics to Monitor
- CPU Usage: Should stay below 70%
- Memory Usage: Monitor for leaks
- Request Rate: Requests per second
- Response Time: P50, P95, P99
- Error Rate: Should be < 1%
Prometheus Queries
# CPU usage
rate(container_cpu_usage_seconds_total[5m])
# Memory usage
container_memory_usage_bytes
# Request rate
rate(http_requests_total[5m])
# Error rate
rate(http_requests_total{status=~"5.."}[5m])
Scaling Checklist
- Monitor application metrics
- Set up auto-scaling
- Configure load balancer
- Optimize database queries
- Add caching layer
- Use CDN for static assets
- Test under load
- Document scaling procedures
Related Topics
- Performance Tuning - Performance optimization
- Deployment Strategies - Deployment methods
Need help? Ask in GitHub Discussions.