Strategies to Scale Your Database
Database scaling isn't a single decision. It's a sequence of moves, applied in the right order, based on where the bottleneck actually is. Here's the play...
6 Jun 2024

Database scaling isn't a single decision. It's a sequence of moves, applied in the right order, based on where the bottleneck actually is. Here's the playbook, from least to most complex.
1. Indexing
Start here. Always. Indexing is the highest-leverage optimization available. An index turns a full table scan into a direct lookup.
Benefit: Orders-of-magnitude improvement on read queries with zero architecture changes.
Cost: Each index consumes storage and slows down writes. Too many indexes on a write-heavy table will hurt.
2. Materialized Views
A materialized view stores the precomputed result of a query. Instead of running an expensive aggregation on every request, you run it once and read the cached result.
Benefit: Turns expensive analytical queries into simple table lookups.
Cost: Data can go stale. You need a refresh strategy -- time-based, trigger-based, or on-demand. Storage doubles for the precomputed data.
3. Denormalization
Combine related tables to eliminate JOINs. Store redundant data intentionally to speed up reads.
Benefit: Simpler, faster read queries. Fewer JOINs means less CPU and I/O per request.
Cost: Writes become more complex. Update anomalies are possible. You're trading data integrity for read speed.
4. Vertical Scaling (Scale Up)
Throw more hardware at the problem. Bigger CPU, more RAM, faster disks.
Benefit: No code changes. No architecture changes. Your existing setup just runs faster.
Cost: Expensive. There's a hard ceiling -- the biggest machine money can buy is still one machine. Doesn't solve availability.
5. Caching
Store frequently accessed data in memory using Redis, Memcached, or application-level caches. Check the cache before hitting the database.
Benefit: Dramatically reduces database load. Reads from memory are 100x faster than disk.
Cost: Cache invalidation is hard. Stale data causes subtle bugs. Adds a dependency you need to manage and monitor.
6. Replication
Copy your database to multiple servers. The primary handles writes. Replicas handle reads.
Benefit: Read scalability. High availability -- promote a replica if the primary fails.
Cost: Replication lag means replicas might serve stale data. Writes still bottleneck on the primary. Operational complexity increases.
7. Sharding
Split your data across multiple databases. Each shard holds a subset of the data.
Benefit: Scales reads AND writes. No single server holds all the data. Theoretically unlimited horizontal scale.
Cost: The most complex option. Cross-shard queries are painful. Schema changes require coordination across shards. Rebalancing is operationally difficult.
The Right Order
Apply these strategies in order of complexity: indexing first, then caching and denormalization, then replication, then vertical scaling, and finally sharding.
Each step up the ladder adds operational complexity. Don't shard a database that hasn't been properly indexed. Don't add read replicas before you've set up caching. Solve the cheap problems first.
Measure before you optimize. The bottleneck is rarely where you think it is.
Keep reading
- Database Performance and Indexes in Prisma ORM
- Magic of Horizontal and Vertical Scaling
- Optimizing Database Queries: Techniques and Best Practices
- The Capacity Estimation Numbers Every Engineer Should Carry Into a System Design
- Database Sharding: A Comprehensive Guide
- Resilience Patterns: Retry, Circuit Breaker, and Timeouts