Why you shouldn't do application-level sharding
Application-level sharding of an OLTP database looks simple on day one (a `shard_for(user_id % N)` function) but accumulates serious engineering burdens as a system grows. Walking through a fast-growing marketplace on Postgres, the hazards are resharding with processes deploying at different times and losing writes, routing queries that lack the shard key (scatter-gather, colliding bigserial IDs, shard-encoded IDs à la Instagram, lookup tables that become single points of failure), hot shards from large tenants, cross-shard transactions requiring two-phase commit or sagas with outboxes and idempotency, and schema changes applied across every shard. Examples from Intercom, Etsy and Slack illustrate the costs, and the case is that this routing and coordination logic belongs in a database-level sharding layer rather than application code.
Related posts
- Handling hot shards
PlanetScale mediumAdeptWhen a shard key like tenant_id stops matching reality because a few whale tenants dominate load or cross-tenant queries appear, Neki (PlanetScale's sharded Postgres from the Vitess maintainers) offers three escalating fixes: vertically scale the hot shard with its own configuration profile, isolate the whale by narrowing hashed key ranges in the declarative data topology, or reshard specific tables by a better key. Slack's move from per-workspace to per-channel sharding of messages with Vitess illustrates the long-term fix: choose a per-table shard key that matches access patterns so common queries stay single-shard. Resharding copies rows to new shards and streams ongoing writes before cutting over without downtime. The advice is to reshard the table that hurts now rather than waiting to redesign everything.
- How Figma’s databases team lived to tell the scale
Figma mediumAdept
Figma horizontally sharded its RDS Postgres stack after vertical partitioning ran out of room. Multi-terabyte tables were hurting vacuum reliability and approaching RDS IOPS limits. Instead of moving to CockroachDB, TiDB, Spanner, Vitess or NoSQL, the team built sharding on its existing Postgres fleet using several pieces: colocated table groups ("colos") that share a shard key such as UserID, FileID or OrgID; hash-based routing to avoid hotspots from auto-incrementing and Snowflake IDs; logical sharding via views, kept separate from physical shard splits; and a DBProxy query engine that parses and routes queries and also does load-shedding and request hedging. Deliberate tradeoffs included dropping atomic cross-shard transactions, accepting slower range scans, keeping rollback possible, and using full logical replication. Sharding the first table took nine months.
- Shard design doesn't start from your tables
PlanetScale mediumAdeptNeki's shard groups co-locate related Postgres tables by hashing a shared shard key (e.g. customer_id via xxhash) into a 64-bit keyspace split across shards, so joins and transactions for one customer stay on a single shard with full Postgres semantics. Cross-shard transactions get no atomic commit or shared snapshot. The design toolkit includes reference tables replicated to every shard for lookup data, separate shard groups for differently keyed data like a SKU catalog, SQL expressions in shard indexes such as lower(sku), range and modulo index types for legacy schemes, and global secondary indexes that turn non-shard-key lookups into two single-shard reads. The core advice is to design shard groups from your most important queries and transactions rather than from your tables, and to change them later with the Reshard workflow as needed.
- Horizontally scaling the Rails backend of Shop app with Vitess
Shopify Engineering mediumAdeptShop app outgrew a single MySQL database despite federating tables across databases, so the team moved its Rails backend to Vitess, sharding user-owned tables by user_id into a "users" keyspace and keeping the rest in an unsharded "global" keyspace. The key enabler was a set of application-layer query verifiers that flagged missing sharding keys, cross-database and cross-shard transactions, cross-shard writes and cross-keyspace joins. They were rolled out first in dev/CI, then in log-only mode in production. Getting there also meant backfilling user_id onto billion-row tables, rewriting shard-key updates as insert-then-delete, and patching ActiveRecord associations so the sharding key reaches every query. Two takeaways: fix the data model early, and avoid cross-shard transactions because Vitess does not roll back partial commits by default.
- The architecture of Neki
PlanetScale mediumAdeptNeki shards unmodified PostgreSQL behind a single connection string. A Router speaks the Postgres wire protocol, parses and plans queries against the sharding layout, and either pushes joins down to shards or runs cross-shard nested-loop, hash or merge joins itself. Each instance runs a Sidecar that pools connections by lifetime (statement, transaction, session advisory lock) and a PostgresManager. Admin handles failover, pg_rewind and per-shard sync/async/cross-zone durability, while an authoritative shard keeps custom type OIDs consistent and etcd holds the Data Topology. A Replicator built on logical replication drives MoveTables, Reshard and shadow-table OnlineDDL, and the Router buffers retryable queries during cutovers and failovers.