Application-Level Sharding: Why Teams Regret It

Diagram of application-level sharding across database shards

Application-level sharding looks like the easy fix when a Postgres or MySQL database outgrows one server, but PlanetScale argues it hands your team a second job. The company traces a fast-growing online marketplace through its first 1,000 days of sharding in application code and lists the tooling that team ends up building.

The conclusion is blunt: use a sharding layer such as Vitess for MySQL or Neki for Postgres, and shard only when you need to.

Why teams shard in the app

Read replicas help, but they don’t touch writes, which still go to the primary. A bigger server buys time until there’s no bigger server to buy. Before that, sheer size causes pain: backups and restores that take hours, and replicas that fall behind during write spikes.

So the example store picks user_id as the shard key and splits users across four databases. The first version is one function that returns a connection based on user_id modulo 4. It takes an afternoon to write. Moving existing users takes six steps: copy rows, keep copying new writes, verify the rows match, deploy the router, copy writes back in case of rollback, then delete the originals. Intercom, which ran its own sharding layer for years, said sharding a single table took months.

Where the pain shows up

Growing from four shards to eight means changing the modulo and moving half the users. The risky moment is the deploy. Web apps, workers and cron jobs each carry their own routing code and don’t deploy together, so for a few minutes they can disagree on where a user lives. In PlanetScale’s example, a worker marks an order shipped on the old shard after the web app has already cancelled it on the new one, and the shipment is lost. Teams respond with a config service, which only shrinks the window.

Lookups come next. A support email with only an order number has no user_id to route on. Querying all eight shards is slow, and it can return two different orders because each shard counts IDs independently. Instagram embedded the shard in the ID, pointing at logical buckets that map to servers. A lookup table works for IDs you don’t create, such as a payment provider’s charge ID, but if it goes down the store goes down. Etsy once said such an index outage could cause a full site outage.

Other problems pile up:

  • A seller’s dashboard needs orders from every shard, which means querying all of them, merging, sorting and paging.
  • A big brand can turn one shard hot, so it gets a dedicated shard and an exceptions list in the router. Slack chose to over-provision most shards instead.
  • Store credit moves money between two users on different shards. Two-phase commit locks rows and stalls if the coordinator crashes, so most teams build a saga with an outbox, idempotent retries, a pending state and an undo step.
  • A schema change must run on every shard. With nine shards there are nine chances to fail, and one long report can block an ALTER and then every query behind it.
  • Failovers happen about nine times as often, and every process must find the new primary.
  • Every app server opens connections to every shard. Notion had to shard its PgBouncer pooler after its 2023 reshard.

The 1,000-day tally

By day 1,000, PlanetScale says, the team has built reshard tooling and a config service, an ID scheme and lookup table, an exceptions list, a saga, a schema rollout tool, a failover orchestrator and a connection pooler for each shard. In effect, a sharding layer, though the team’s real job is running a marketplace.

What PlanetScale recommends

A sharding layer sits between the app and the shards and looks like one database. PlanetScale says Neki, its horizontally sharded Postgres in platform preview and built by the team behind Vitess, covers most of the list. Resharding is an automated workflow, and the router buffers queries during the switch. It hands out unique IDs and offers global secondary indexes that are themselves sharded. Hot key ranges can move to their own shard, schema changes run without downtime, and failover and pooling are built in.

The catch: Neki doesn’t yet make writes across shards atomic. That remains the application’s job, so choose a shard key that keeps most writes on one shard.

Start early, shard late

Neki and Vitess can begin as a single primary with replicas and reshard when the scale arrives. Slack, Etsy and Intercom built their own sharding first and moved to Vitess later. Slack took three years to reach 99% of its traffic on it, and Etsy took five.

Migrating after you’ve shipped your own layer is slow, so the advice is to begin on a sharding layer and add shards when you need them.

Source: planetscale.com

Written by

Saksham Bhargava

Saksham Bhargava runs Alteroid and has been writing about consumer technology for over five years, covering iPhone, Android, Windows, AI tools and gaming. With an MCA and six years of hands-on web development experience, he tests the features he writes about on his own devices and focuses on clear, practical guides that solve real problems. Away from the screen, he enjoys travelling to places most tourists skip.