Imagine your app starts blowing up and your database can't keep up. Do you buy a bigger database, or more databases?

Three databases sound like they should be faster, since you're spreading the work across more machines. But those machines also have to coordinate, and depending on how you split your data, one request from a user can turn into work for all three.

So I gave one big Postgres and three small ones the exact same budget and load tested them until they broke.

Setup

The budget was 12 CPU cores and 24 GB of RAM of Postgres 18, about $57 a month at Synteq's per-core price. I tested three ways to spend it:

  • ONE: a single Postgres with all 12 cores and 24 GB.

  • REPLICAS: a primary and two async streaming replicas, 4 cores and 8 GB each. Writes go to the primary, reads to the replicas.

  • SHARDS: three independent Postgres databases, 4 cores and 8 GB each. Each article lives on shard article_id mod 3 along with its comments, favorites and tags. Users are copied to every shard, so sign-ups and profile edits commit on all three at once with two-phase commit.

Each database ran on a 16-core VPS, fenced to its share with Linux cgroups.

The app is Conduit, the same Medium clone from my microservices and cores experiment. I started with 100,000 users, 500,000 articles, 2.5 million comments, 5 million favorites and 2 million follows, about 4.7 GB of data.

This time there was no app server in the middle. The load tool plays the users and runs the app's exact SQL straight against Postgres, including the routing and merging the distributed setups need. That way the database is the only thing being measured, not a web framework.

Virtual users browse like real readers: they open the feed, read articles, check profiles and comments, and sometimes write something. To pass, a setup had to hold its load for 5 straight minutes with p95 under 500 ms, p99 under 1 second, and under 1% errors.

I also checked that the shard code returned identical rows to the single database on 6,000 comparisons, so the shards weren't winning or losing by returning the wrong answer.

Read-heavy: 76,000 vs 41,000 vs 20,000

First up, a workload like Medium, where about 95% of calls are reads.

ONE held 76,000 users. At that load Postgres was using 90% of its 12 cores and serving just under 20,000 requests per second. It put the extra cores to work almost perfectly. For reference, a single 4-core box (a third of the budget) held 19,000, so 12 cores got 4x the users.

REPLICAS held 41,000, a little over half. That's the best replica setup I found, and getting there took a few changes.

SHARDS held 20,000, barely more than a quarter of ONE, and about the same as that one 4-core box on its own.

Why replicas lost

When I first sent every read to the replicas, the primary sat at 9.5% CPU while both replicas were at about 73%. With 95% reads, a third of the money was doing almost nothing.

Letting the primary serve reads too brought it up to 76%, with the replicas at 65% and 56%. Much better balance, but the score stayed at 41,000. I also tried giving the primary a smaller share of reads, since it handles every write too:

Share of reads on the primary

Users held

One third

41,000

20%

40,000

11%

37,000

None of them beat 41,000.

The bigger problem was correctness. Imagine you publish an article. The primary saves it, and the app takes you to the article page. But that next read goes to a replica that hasn't applied your write yet, so the article you just published comes back as a 404.

Replay lag was only a few milliseconds, but your next click happens immediately. With 5% writes, this hit 0.67% of calls, which slipped under the 1% error limit but is still broken for those users. With 50% writes it hit 6 to 8% at every load, even with 10 users. So this wasn't the databases getting overloaded. The routing let people read a copy that didn't have their change yet.

I tested two fixes:

  • Sticky reads: after you write, your reads go to the primary for 2 seconds.

  • Retry: if a replica can't find the record, try the primary.

Both got rid of the 404s in these tests. Neither one guarantees fresh reads in general, but they fixed the failure I was seeing. The cost is that the one 4-core primary now carries every write plus the redirected reads.

There was one more issue. In two holds a replica stopped replaying for about 9 seconds and then got stuck at 98% CPU doing a fifth of its normal work. The suspect is a lock from autovacuum replaying on the standbys, but I couldn't confirm it. It's why the higher loads failed.

Why shards lost

Remember the home page. The latest articles could be on any of the three shards, so you have to ask all three, merge the results, and throw away the rows that don't make the page. Same for tag pages, profiles and feeds.

That means every shard runs the whole list query, and two thirds of the work gets thrown away.

Database CPU per request

The single database spent about 0.55 ms of database CPU per request. The shards spent 1.59 ms, almost three times as much for the same request.

That doesn't mean every sharded app has to work this way. It depends on your shard key and your queries. I didn't redesign the feed for sharding, and I'll test what happens with fewer list pages below.

More writes

At this point you could say this workload is unfair to shards. So I increased the writes: favorites, comments and follows, which only touch one shard. About 70% writes was the most this user model allows, because you have to open an article before you can write to it.

Setup

~5% writes

~50% writes

~70% writes

ONE

76,000

76,000

59,000

SHARDS

20,000

34,000

43,000

REPLICAS (retry)

33,000

35,000

27,000

REPLICAS (sticky)

35,000

33,000

26,000

Shards more than doubled while ONE dropped. They got close, but they never crossed.

The interesting part is why ONE dropped. At 70% writes it was only using 36% of its CPU. Postgres writes every change to the write-ahead log (WAL) and flushes it to disk so it can recover after a crash. Under heavy writes, 11 to 12 connections were waiting in line for that flush. More CPU doesn't help with that.

Three shards have three logs and three disks to flush to. When a comment only touches one shard, the other two keep working. That's the one place splitting the data clearly helped.

It doesn't make every write cheaper though. Sign-ups and profile edits have to commit on all three shards with two-phase commit, and they took 8 to 9 ms at the median compared to about 3 ms on one database.

Replicas got worse as writes went up, because every write still goes through the same 4-core primary. With sticky reads at 70% writes, 54% of reads ended up on the primary, and the replicas sat at about 7% CPU.

Can I make three win?

So far one database won everything. So I started removing the things holding the shards back.

Fewer list pages. I cut list pages down to about 3% of calls, so far fewer requests had to ask all three shards. The shards more than doubled to 45,000. But ONE also got a cheaper workload and went up to 88,000.

More data than fits in memory. I made the dataset 8x bigger, about 18 GB, which doesn't fit on one 8 GB box. ONE held 57,000, replicas 42,000 and shards 20,000. Still no win.

So I gave ONE only 8 GB of RAM with 18 GB of data. It still held 60,000 users, and its cache hit rate was 99.8%.

The users weren't reading all 18 GB evenly. They mostly opened recent articles, the first few pages of lists, and popular authors. The whole dataset didn't fit in RAM, but the part people kept asking for did.

Where three finally won

For the last test, users opened articles at random across all 4 million articles, with list pages still cut down. ONE kept only 8 GB of RAM, and each shard still had 8 GB.

That makes this an unequal fight on purpose. ONE has 8 GB total while the shards have 24 GB between them. The real-world version is when you've hit the biggest box you can buy or afford, and you need more.

With random reads, ONE's cache hit rate dropped to about 95%, so it had to go to disk a lot more. Then I capped each box's disk at different IOPS, like a cloud volume:

Disk cap per box

ONE, 12 cores / 8 GB

SHARDS, 3 × 4 cores / 8 GB

No cap (local NVMe)

68,000

40,000

16,000 IOPS

70,000

8,000 IOPS

29,000

4,000 IOPS

13,000

40,000

On fast local NVMe, ONE still won. But somewhere between 16,000 and 8,000 IOPS it fell off a cliff, and at 4,000 IOPS the shards won by about 3x.

The shards didn't care about the cap because each one held its third of the data in memory. Uncapped, they were only doing about 200 disk operations per second each. A stock cloud volume starts around 3,000 IOPS, which is already past the crossover.

The cap is per box, so the shards also had 3x the total disk allowance. It's a condition where splitting helps, not a reversal of the equal-budget result.

So what does this actually mean?

With the same money, one big database won every normal workload I threw at it. Replicas left capacity idle and broke read-after-write. Shards had to do extra work on any request that touched all three, and added coordination to writes that used to be one transaction.

One real reason to run more than one database is availability, and replicas and shards are very different there. If a replica dies, the primary still has all the data. If one of these shards dies, a third of your data is gone until it comes back, and making shards redundant means replicas for each shard, which costs more.

So for an app like this, scale up while it still works for you. When you do need to scale out, ask three questions first:

  • What work am I actually spreading out?

  • What new work am I creating, like fan-out queries, retries and two-phase commits?

  • Which bottleneck am I trying to remove: CPU, WAL flushes, memory or disk?

And don't assume a second database doubles your capacity. In this test, adding two more made things slower.

Have a good week!

Arjay.

P.S. I'm hosting the Advent of System Design this December: a free system design puzzle every day, inside Devmaxx. If you liked reasoning through this one, you'll like those.