System DesignDatabaseScaling
Database Scaling: From Zero to Millions
Strategies for scaling databases as your application grows.
7 min read
Database Scaling: From Zero to Millions
As your application grows, your database strategy must evolve.
Scaling Strategies
Vertical Scaling (Scale Up)
- Add more CPU/RAM
- Simple but limited
- Good for: < 100K users
Horizontal Scaling (Scale Out)
- Add more machines
- Complex but unlimited
- Good for: 100K+ users
Read Scaling
Primary DB (writes)
├── Replica 1 (reads)
├── Replica 2 (reads)
└── Replica 3 (reads)
Write Scaling: Sharding
function getShard(userId: string) {
const hash = hashFunction(userId);
return hash % NUM_SHARDS;
}
Common Patterns
Connection Pooling
const pool = new Pool({
max: 20,
idleTimeoutMillis: 30000
});
Read Replicas
- Route reads to replicas
- Route writes to primary
- Handle replication lag
When to Scale
- Response time increasing
- CPU/Memory > 70%
- Connection limits hit
Scale before you need to - reactive scaling is expensive!