#database#scalability#backend
Database Scaling Strategies: From Single Node to Sharding
A comprehensive guide to database scaling strategies including vertical scaling, read replicas, partitioning, and sharding for high-traffic applications.
2 min read
Table of Contents
The Scaling Journey
Every successful application eventually faces the same challenge: the database becomes a bottleneck. Here’s a systematic approach to scaling your database.
Level 1: Vertical Scaling
The simplest approach — get a bigger machine.
Before: 4 CPU, 16GB RAM, 100GB SSD
After: 32 CPU, 128GB RAM, 2TB NVMe
Pros: No code changes, no complexity Cons: Expensive, has hard limits
Level 2: Read Replicas
Separate read and write traffic:
class DatabaseRouter {
private primary: Connection;
private replicas: Connection[];
query(sql: string, isWrite: boolean) {
if (isWrite) {
return this.primary.execute(sql);
}
// Round-robin across replicas
const replica = this.replicas[
Math.floor(Math.random() * this.replicas.length)
];
return replica.execute(sql);
}
}
Level 3: Sharding
Distribute data across multiple databases:
function getShardKey(userId: string): number {
const hash = crypto
.createHash("md5")
.update(userId)
.digest("hex");
return parseInt(hash.slice(0, 8), 16) % NUM_SHARDS;
}
Choosing the Right Strategy
| Traffic | Strategy | Complexity |
|---|---|---|
| < 10K QPS | Vertical scaling | Low |
| 10-100K QPS | Read replicas | Medium |
| 100K+ QPS | Sharding | High |
Conclusion
Start simple. Optimize queries first. Add indexes. Use caching. Only shard when you’ve exhausted simpler options. Premature sharding is one of the most expensive mistakes in system design.