Horizontally scaling the Rails backend of Shop app with Vitess
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.
Related posts
- Migrating Etsy’s database sharding to Vitess
Etsy Code as Craft mediumAdeptEtsy 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.
- Partitioning GitHub’s relational databases to handle scale
GitHub Engineering mediumAdeptGitHub 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.
- 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.
- 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.
- Shard Balancing: Moving Shops Confidently with Zero-Downtime at Terabyte-scale
Shopify Engineering mediumAdeptShopify 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.