---
title: "Shard Balancing: Moving Shops Confidently with Zero-Downtime at Terabyte-scale"
description: "Shopify rebalances its MySQL shards by moving individual shops between pods online, with downtime confined to a brief cutover."
url: "https://swe.quest/articles/shard-balancing-moving-shops-confidently-with-zero-downtime-at-terabyte-scale-76a6177fa357eea4"
canonical: "https://shopify.engineering/mysql-database-shard-balancing-terabyte-scale"
source: "Shopify Engineering"
published: "2021-09-24"
difficulty: "medium"
kind: "Case study"
reading_minutes: 13
tags: ["mysql","distributed-databases","schema-migrations","scalability"]
---

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

A post on [Shopify Engineering](https://swe.quest/sources/shopify.md), published 2021-09-24, 13 min read, Case study. Original: [https://shopify.engineering/mysql-database-shard-balancing-terabyte-scale](https://shopify.engineering/mysql-database-shard-balancing-terabyte-scale)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [MySQL](https://swe.quest/tags/mysql.md) (Data & databases)
- [Distributed databases](https://swe.quest/tags/distributed-databases.md) (Data & databases)
- [Schema migrations](https://swe.quest/tags/schema-migrations.md) (Data & databases)
- [Scaling](https://swe.quest/tags/scalability.md) (Distributed systems)

## Prerequisites

- MySQL binlog replication ([easy posts on MySQL](https://swe.quest/tags/mysql.md?difficulty=easy))
- database sharding ([easy posts on Databases](https://swe.quest/tags/databases.md?difficulty=easy))
- row-level locking

## Keywords

Ghostferry, MySQL, binlog, Redis, Shopify, NGINX

## Related posts

- [Partitioning GitHub’s relational databases to handle scale](https://swe.quest/articles/partitioning-github-s-relational-databases-to-handle-scale-7b5ea75acfc088c0.md): GitHub Engineering · 2021-09-27 · Medium · 9 min read · MySQL, Distributed databases, Scaling, Schema migrations, Ruby
  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.
- [Capturing Every Change From Shopify’s Sharded Monolith](https://swe.quest/articles/capturing-every-change-from-shopify-s-sharded-monolith-fd5db9b13b97ba9a.md): Shopify Engineering · 2021-03-12 · Medium · 23 min read · Event-driven architecture, Data pipelines, MySQL, Queues & messaging, Analytics & OLAP
  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.
- [We replaced Redis with MySQL for inventory reservations—and it scaled](https://swe.quest/articles/we-replaced-redis-with-mysql-for-inventory-reservations-and-it-scaled-2f8fab4ab3565f02.md): Shopify Engineering · 2026-05-12 · Medium · 10 min read · MySQL, E-commerce, Concurrency, Scaling, Performance engineering
  Shopify moved checkout inventory reservations from Redis to MySQL by modeling one row per sellable unit and using SELECT ...
- [Migrating Etsy’s database sharding to Vitess](https://swe.quest/articles/migrating-etsy-s-database-sharding-to-vitess-c6bef1d09f2cd679.md): Etsy Code as Craft · 2026-03-19 · Medium · 9 min read · MySQL, Distributed databases, Refactoring & migrations, SQLite, Deployment
  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.
- [Horizontally scaling the Rails backend of Shop app with Vitess](https://swe.quest/articles/horizontally-scaling-the-rails-backend-of-shop-app-with-vitess-03c7b2da20f98ec3.md): Shopify Engineering · 2024-01-17 · Medium · 27 min read · MySQL, Distributed databases, Ruby, Scaling, Schema migrations
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
