Skip to content

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

Etsy Code as Craft 9 min read Case study mediumAdept

Migrating Etsy’s database sharding to Vitess

Etsy moved shard routing for its ~1,000-table, ~1,000-shard, 425 TB MySQL cluster (1.7M requests/sec) from an ORM that looked up random shard mappings in a single index database to Vitess vindexes, without moving any data. The ORM's shard assignment was switched to match Vitess's hash vindex, and legacy mappings were frozen into a read-only SQLite file copied onto each Vitess server. A custom hybrid vindex picks SQLite lookup or hash based on an id threshold. Tables were ramped incrementally through the experimentation framework with instant rollback, though 27 transaction-coupled models carrying a third of traffic had to move together to preserve per-connection atomicity. The five-year effort removed a single point of failure, cut re-sharding from months to days, and enabled opt-in scatter queries; one query dropped from ~2 s to ~20 ms, and bulk primary-key lookups needed far fewer queries.

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.

  • Partitioning GitHub’s relational databases to handle scale

    GitHub Engineering mediumAdept

    GitHub split its monolithic `mysql1` MySQL cluster by first virtually partitioning tables into schema domains (declared in YAML) and enforcing boundaries with query and transaction linters, then physically moving domains to new clusters via Vitess vertical sharding or a custom ProxySQL/GTID-based write-cutover script. Cross-domain joins were eliminated with techniques like preload, Rails' new `disable_joins` option and application-side joins, validated with Scientist experiments. The write-cutover moved 130 of the busiest tables in tens of milliseconds with only a handful of failed writes. Query volume grew from 950k to 1.2M queries/s while average per-host load halved, reducing database-related incidents.

  • Shard Balancing: Moving Shops Confidently with Zero-Downtime at Terabyte-scale

    Shopify Engineering mediumAdept

    Shopify rebalances its MySQL shards by moving individual shops between pods online, with downtime confined to a brief cutover. Placement is decided by simulating proposed moves against historical utilization data, which in one case narrowed the spread between least- and most-used shards from about 4x to about 2x. The move itself uses Ghostferry, which batch-copies rows filtered by shop_id using SELECT...FOR UPDATE while tailing the row-based binlog to replay concurrent writes. Once replication lag is near real time, a Redis-backed multi-reader-single-writer lock halts writes for the shop so remaining binlog events can drain, after which the routing table is updated, the lock released, the move verified and stale data pruned from the source shard.

  • Handling hot shards

    PlanetScale mediumAdept

    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.

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

The quest for software engineering knowledge.

Look:
Layout: