Skip to content

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

Shopify Engineering 13 min read Case study mediumAdept

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

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.

Related posts

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

  • Capturing Every Change From Shopify’s Sharded Monolith

    Shopify Engineering mediumAdept

    Shopify is replacing Longboat, its query-based change-data capture tool that polls MySQL replicas on an updated_at column, with a log-based CDC platform built on Debezium, Kafka Connect and Kafka that reads the binlog from 100+ MySQL shards. Polling caps data freshness at about an hour and cannot capture hard deletes, intermediate row states, or updates that skip updated_at. It also produces snapshots with mismatched high-water marks. The new design merges per-shard Debezium output into one Kafka topic per logical table, so a single CDC pipeline can serve both the internal data warehouse and the merchant analytics platform with near-real-time change events.

  • We replaced Redis with MySQL for inventory reservations—and it scaled

    Shopify Engineering mediumAdept

    Shopify moved checkout inventory reservations from Redis to MySQL by modeling one row per sellable unit and using SELECT ... FOR UPDATE SKIP LOCKED, so concurrent checkouts grab different rows without contention while reservations and the inventory ledger share ACID transactions. Key fixes included a bounded pool of 1,000 rows per item/location with locked inline replenishment, a composite primary key that halved row locks, READ COMMITTED to avoid gap/supremum locks, consistent lock ordering to prevent deadlocks, and UNION ALL batching. The real throughput ceiling turned out to be database connection exhaustion from other checkout code, found by tagging SQL with caller comments and measuring hold time in ProxySQL; cleanup cut 50% of reads and 33% of transactions, and raising InnoDB thread concurrency removed the limit. Cutover used dual-write shadow mode with a kill switch and gradual pod-by-pod rollout.

  • Migrating Etsy’s database sharding to Vitess

    Etsy Code as Craft mediumAdept

    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.

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

The quest for software engineering knowledge.

Look:
Layout: