Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

PlanetScale 7 min read Opinion mediumAdept

Handling hot shards

When 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.

Assumes: database sharding basicshash-based partitioning

Related posts

  • Horizontally scaling the Rails backend of Shop app with Vitess

    Shopify Engineering mediumAdept

    Shop 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.

  • Shard design doesn't start from your tables

    PlanetScale mediumAdept

    Neki'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.

  • 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.

  • You should shard your database

    PgDog mediumAdept

    Sharding Postgres early avoids the operational pain of large single instances, illustrated by an outage where stale planner statistics caused a 300 GB table to be sequentially scanned. Attempts to fix it by raising default_statistics_target added planning time (0.2ms to ~1ms per query, significant at 100k QPS), while making autovacuum analyze more often increased IOPS. Splitting the table into 12 shards cuts writes, autovacuum work and scanned rows by 12x, making migrations like ALTER COLUMN TYPE BIGINT finish in seconds and lowering the cost of mistakes.

  • Why you shouldn't do application-level sharding

    PlanetScale mediumAdept

    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.

The quest for software engineering knowledge.

Look:
Layout: