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

Scss
Primary DB (writes) 
    ├── Replica 1 (reads)
    ├── Replica 2 (reads)
    └── Replica 3 (reads)

Write Scaling: Sharding

Typescript
function getShard(userId: string) {
  const hash = hashFunction(userId);
  return hash % NUM_SHARDS;
}

Common Patterns

Connection Pooling

Typescript
const pool = new Pool({
  max: 20,
  idleTimeoutMillis: 30000
});

Read Replicas

  • Route reads to replicas
  • Route writes to primary
  • Handle replication lag

When to Scale

  1. Response time increasing
  2. CPU/Memory > 70%
  3. Connection limits hit

Scale before you need to - reactive scaling is expensive!

Enjoyed this article? Show some love!

501 views

Comments