Read replicas, partitioning or sharding: what does each one fix?
Three ways to outgrow one database, often confused. Replicas add read capacity and cost you freshness. Partitioning keeps one huge table manageable but adds no capacity. Sharding adds write capacity and costs you almost everything that was easy. Measured on PostgreSQL 17: replica staleness, and…
The verdict, which the evidence below earns: they fix different problems.
- Read replicas add read capacity and a standby. They don’t help with writes, and their data is slightly behind.
- Partitioning splits one big table into pieces on the same server. It adds no capacity, but it makes old data cheap to remove, keeps indexes small, and lets queries skip irrelevant pieces.
- Sharding splits data across servers. It’s the only one of the three that adds write capacity and storage beyond one machine, and it makes cross-shard queries, transactions and rebalancing your problem.
Use them in that order, and only for the problem each one solves.
Case file
The case The database is struggling and “we need to scale it” Suspects Read replicas, partitioning, sharding Culprit Using one of them for a problem it doesn’t fix The fix Replicas for reads, partitions for old data, shards for writes and storage, in that order Evidence PostgreSQL 17 in database-tradeoffs-lab, demos 08 and 09
The case #
The database is struggling, and someone says “we need to scale it”. Three suspects get named in the same breath: replicas, partitioning, sharding. They get blurred together often, which is a shame, because each has a clear alibi for some crimes and none for others. Once you can say exactly what each one buys, a lot of scaling advice makes sense.
The replica and partitioning numbers below are measured on PostgreSQL 17 in database-tradeoffs-lab (demos 08 and 09). Sharding isn’t: it needs several servers to mean anything, so that section is design guidance rather than evidence.
The suspects at a glance #
| Read replicas | Partitioning | Sharding | |
|---|---|---|---|
| Splits | Copies of all the data | One table, by a key (time, tenant), on one server | The data, by a key, across servers |
| Adds read capacity | Yes | No (only skips data) | Yes |
| Adds write capacity | No | No | Yes |
| Adds storage beyond one machine | No | No | Yes |
| Makes old data cheap to remove | No | Yes: drop a partition | Only if sharded by time |
| Costs | Replication lag: stale reads | Queries without the key touch every partition; more objects to manage | Cross-shard queries and transactions, rebalancing, hot shards, operations |
| Reach for it when | Reads dominate and can be a little behind; you need a standby anyway | A table is huge, time-ordered, or has data you delete by age | Writes or data outgrow the biggest machine you can buy, after everything else |
Diagram source
flowchart TD
P{"What is the limit?"}
P -->|"Reads"| RR["Read replica, staleness-tolerant reads only"]
P -->|"Old data, huge time-ordered table"| PT["Partition by time, drop old partitions"]
P -->|"Writes or storage past one machine"| SH["Shard by tenant"]
P -->|"Slow queries"| FX["Fix queries and indexes, add a cache and pooler"]
Read replicas: more reads, slightly old ones #
The mechanism is quietly elegant: a replica receives the primary’s write-ahead log and replays it, the same log the primary already writes for its own crash safety. Nothing new has to be invented. You can send reads there: reports, search pages, the API’s read-heavy endpoints. The cost is that it’s always a little behind. From demo 08, under background write load:
- reading a row from an async replica right after committing it on the primary returned the old value 99.7-100% of the time, with replication lag of only about 2 ms (a puzzling pair of clues: tiny lag, yet almost always stale);
- one big
INSERT(2 million rows, 685 MB of WAL) put the replica 34-56 MB behind for up to a second; - making the commit wait until the replica has applied it (
synchronous_commit = remote_apply) removed stale reads, for about 1 ms more per commit on one machine, and a network round trip more in real life.
The CAP/PACELC case covers that trade-off in detail.
The scene: reporting load on the primary. Finance’s queries and the admin dashboard compete with customer traffic. Move them to a replica, where a second of staleness doesn’t matter. Give reports their own replica, so a long report doesn’t delay the replica that serves application reads.
What replicas don’t fix: every write still goes to one primary, and every replica stores the whole database. If writes or disk are the limit, replicas have an alibi; look elsewhere.
Partitioning: one huge table, kept manageable #
Partitioning splits a table into child tables by a key (PARTITION BY RANGE (created_at), for example), all on the same server. Queries that filter on the key only touch the partitions that can match (partition pruning). Each partition has its own, smaller indexes.
From demo 09: the same 6 million rows (a year of events, 1,000 tenants) in one table and in 12 monthly partitions, with the same indexes.
| Query (median of 50) | One table | Monthly partitions | Partitions touched |
|---|---|---|---|
| Last 2 days | 2.9 ms | 3.2 ms | 1 |
| One tenant, last 2 days | 0.10 ms | 0.08 ms | 1 |
| One tenant, all year (no partition key) | 0.65 ms | 1.05 ms | 12 |
| Removing the oldest month | One table: DELETE |
Partitions: DETACH + DROP |
|---|---|---|
| Took | 578 ms | 48 ms |
| WAL written (sent to every replica and backup) | 105 MB | 2 MB |
| Space given back | none, even after VACUUM (1,218 MB before and after) |
104 MB, immediately |
Diagram source
xychart-beta
title "Removing the oldest month, ms"
x-axis ["One table: DELETE", "Partitions: DETACH + DROP"]
y-axis "ms"
bar [578 , 48]
What the clues show, and some of it surprised me:
- A false lead: partitioning didn’t make the indexed queries faster. With good indexes, a B-tree finds rows in a big table as quickly. Queries that don’t filter on the partition key got slower (1.6× here), because they probe every partition. More partitions make that worse.
- Removing old data is where partitioning earns its keep.
DELETEof a month took over half a second at this size, and grows with the data. It wrote 105 MB of WAL that every replica had to replay, and left a table no smaller than before. Dropping the partition was close to instant and gave the space back. That’s the real reveal, and a lovely one: deleting a month becomes a metadata change instead of half a million row deletions. - The partition key has to match how you query and how you delete. Time is the usual choice for events, logs and audit trails. Tenant is the usual choice when you delete or move whole tenants.
The scene: a 2 TB events table. It grows forever, almost every query is about the last few days, and retention is 13 months. Partition by month (or week), and drop partitions past retention. Keep the number of partitions in the tens or low hundreds, not thousands. The pg_partman extension automates creating new partitions and dropping old ones. Put an index on the columns you query with, inside each partition, as usual.
Sharding: more of everything, and much more to manage #
Not measured: sharding needs several servers to show anything real.
Sharding is the big one: it gives the most and asks the most. It puts different rows on different servers by a shard key. Each server holds a fraction of the data and takes a fraction of the writes. It’s the only one of the three that lets writes and storage grow past one machine. What it costs:
- Queries that span shards must ask all of them and merge the results. That’s the fan-out tail-latency problem from the search case, inside your database.
- Transactions that span shards need two-phase commit or application-level patterns (sagas, outboxes). Most sharded systems avoid them.
- Rebalancing: adding a shard moves data. Use a scheme that moves only what’s needed, such as fixed slots or consistent hashing (the hashing case measures why).
- Hot shards: if one key gets most of the traffic (your biggest customer), its shard is the bottleneck, however many others you add.
- Operations: backups, migrations, monitoring and failover, times the number of shards.
The scene: a write-heavy multi-tenant SaaS. Tenants rarely need each other’s data, and most queries are inside one tenant. Shard by tenant_id: each tenant lives on one shard, its queries stay on one server, and its transactions stay local. Options:
- Citus (an open-source extension that distributes PostgreSQL tables by a column);
- Vitess for MySQL;
- one database per group of tenants, routed by the application (the cell-based approach);
- a database that shards for you, such as DynamoDB (by partition key) or MongoDB (by shard key), where choosing the key well is still on you.
Give your biggest customers their own shards before they become everyone’s hot shard.
Remember this
- Replicas add reads, not writes, and their reads are slightly old.
- Partitioning doesn’t make indexed queries faster; it makes old data cheap to drop.
- Only sharding adds write capacity, and it makes cross-shard work your problem.
What I’d do on Monday, in order #
- Fix queries and indexes, and add a cache and a connection pooler. The scaling case measures what each buys.
- Add a read replica when reads dominate, and route only staleness-tolerant reads to it (most apps need a standby anyway).
- Partition the big, time-ordered tables when retention, vacuuming or index size becomes painful, not for speed.
- Shard when writes or storage outgrow the largest primary you can reasonably run. Pick the shard key from your access pattern (usually the tenant), and plan rebalancing and the biggest tenants from day one.
There’s a ladder here: each step solves one problem well and asks for a little more care in return. Climb it one rung at a time, and you’ll rarely need the top.
Measured Nov 2026 on PostgreSQL 17.11 in Docker on one laptop (Intel i7-8550U): replica numbers from demo 08 (three runs), partitioning from demo 09 (two runs, similar). Code and full results: database-tradeoffs-lab.
Comments
No comments yet.
Sign in to comment. We'll email you a link; no password needed.