How do you scale a relational database? Replication, partitioning and sharding.
Scale up first
A bigger machine, faster disks, more memory for the buffer cache, and connection pooling (HikariCP in the app, PgBouncer for PostgreSQL) go a long way and keep everything simple.
Read replicas
The primary streams changes (WAL in PostgreSQL, binlog in MySQL, redo in Oracle Data Guard) to replicas that serve reads. Usually asynchronous, so replicas lag: route read-after-write traffic (a user viewing their own update) to the primary, or wait for the replica to catch up.
High availability
A synchronous or semi-synchronous standby in another zone with automatic failover (Patroni, Cloud SQL HA, Amazon RDS Multi-AZ, Oracle Data Guard). Clients reconnect through a stable endpoint.
Partitioning (one database)
Split one large table into partitions by range (by month), list (by region) or hash. Queries that filter on the partition key read only relevant partitions (partition pruning), and old data can be dropped instantly by detaching a partition.
Sharding (many databases)
Distribute rows across independent databases by a shard key (tenant_id, user_id). It scales writes, but cross-shard joins and transactions become hard, rebalancing is complex, and the key must spread load evenly (avoid hot shards). Tools: Vitess (MySQL), Citus (PostgreSQL), or distributed SQL databases (Spanner, CockroachDB, YugabyteDB).
Caching and CQRS
Put Redis in front for hot reads; build read models or search indexes (Elasticsearch) from change events (CDC with Debezium).
Example
-- PostgreSQL declarative range partitioning by month
CREATE TABLE events (
id BIGINT GENERATED ALWAYS AS IDENTITY,
tenant_id BIGINT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
payload JSONB,
PRIMARY KEY (id, created_at)
) PARTITION BY RANGE (created_at);
CREATE TABLE events_2026_11 PARTITION OF events FOR VALUES FROM ('2026-11-01') TO ('2026-12-01');
CREATE TABLE events_2026_12 PARTITION OF events FOR VALUES FROM ('2026-12-01') TO ('2027-01-01');
-- Pruned to one partition
SELECT count(*) FROM events WHERE created_at >= '2026-12-01' AND tenant_id = 9;
-- Retention: drop a whole month instantly
ALTER TABLE events DETACH PARTITION events_2026_11;
DROP TABLE events_2026_11;Follow-up questions interviewers ask
Try answering each one before you open it.
How do you pick a shard key?
Choose a column present in almost every query (so most queries hit one shard), with high cardinality and even distribution, that rarely changes. tenant_id suits B2B SaaS; user_id suits consumer apps. Avoid monotonically increasing keys such as timestamps with range sharding, which send all new writes to the last shard.