+− THE DAILY DIFFdev & AI news
SHIP IT

Shopify moved inventory reservations into MySQL

Shopify moved its inventory reservation system from Redis into the MySQL database that already held the inventory ledger.

Want to see our stories first on Google? Add us to your preferred sources — it helps us grow.

Shopify moved its inventory reservation system from Redis into the MySQL database that already held the inventory ledger. A bounded pool of individually lockable unit rows lets concurrent checkouts use SKIP LOCKED to select different eligible units. The design also depends on transaction boundaries, primary-key layout, replenishment rules and observing connection hold time across checkout.

Read the written edition (English) ↗

What this video covers

  • The old Redis quantity-counter model handled concurrency, but reservation cleanup and the MySQL ledger update could not share a single local atomic transaction.
  • The replacement uses one row per available unit in a pool capped at 1,000 per item/location combination. An empty pool can trigger inline replenishment with concurrent requests waiting behind a replenishment lock.
  • The composite primary key is (shop_id, inventory_item_id, inventory_group_id, id). Shopify reduced lock overhead in its prototype and used READ COMMITTED to avoid gap locks that blocked replenishment.
  • Reserve deletes pool rows before inserting reservation records. Committing releases database locks while the stored hold persists across payment; successful payment claims the ledger and removes the reservation in a later atomic transaction.
  • MySQL documents SKIP LOCKED as an inconsistent view that omits locked rows. It does not establish a complete stock count or fair turn-taking, applies only to row-level locks and is unsafe for statement-based replication.
  • The source example includes expires_at, but Shopify does not document the MySQL expiration-cleanup algorithm. The video's release/expiry branch is an explanatory lifecycle requirement.
  • Connection hold time in other checkout code was the final throughput bottleneck. Shopify shadow-wrote both systems, compared outcomes and switched gradually with a Redis kill-switch fallback.

Questions and answers

Why did Shopify move inventory reservations from Redis to MySQL?

Shopify already kept its inventory ledger in MySQL, and the old Redis model needed two separate stores to agree. Claiming a paid order meant updating the MySQL ledger and cleaning up Redis, and those separate writes could leave stock sold twice or unavailable when it should be sellable. A reservation held in MySQL can share one transaction boundary with the ledger.

Watch this part 0:29 · Shopify Engineering — Emilie Noel

How does a row-per-unit pool let concurrent checkouts reserve stock without waiting on each other?

Each available unit gets its own row, instead of one hot quantity row that every checkout queues behind. A locking read skips units another transaction holds and selects other eligible units, so different workers acquire different rows. Shopify bounds the pool at 1,000 rows per item and location, and replenishes it from the ledger.

Watch this part 0:52 · Shopify Engineering — Emilie Noel · Shopify Engineering / CourtneySymons on GitHub Gist

What are the limits of MySQL SKIP LOCKED for inventory counting?

SKIP LOCKED excludes locked rows, so the MySQL manual describes its result as an inconsistent view. It provides neither a complete stock count nor fair turn-taking, applies only to row-level locks and is unsafe for statement-based replication. The availability decision and replenishment rules still have to be handled around it.

Watch this part 2:14 · Oracle / MySQL Reference Manual · Shopify Engineering — Emilie Noel

Why can fast database queries still limit checkout throughput?

Time spent outside the query can create the queue. In Shopify's case, other checkout code held database connections too long. Shopify tagged callers, measured connection hold time, cleaned up the checkout path and revisited thread concurrency. It also shadow-wrote both systems and switched gradually, with a Redis kill-switch fallback.

Watch this part 2:26 · Shopify Engineering — Emilie Noel

Is Shopify's MySQL reservation design worth copying?

The Daily Diff's verdict was SHIP IT. The episode would ship the shared transaction boundary and the rollbackable rollout, with the whole checkout path instrumented. Shopify does not document its MySQL expiration-cleanup algorithm, so the release and expiry lifecycle must be designed by whoever adopts the pattern.

Watch this part 2:38 · Shopify Engineering — Emilie Noel · Shopify Engineering / CourtneySymons on GitHub Gist

Transcript

Why move reservations into MySQL?

0:00 A checkout needs Redis to stay fast. Shopify moved inventory reservations into MySQL using one row per unit in a bounded pool. Why choose MySQL? How do you skip locks safely? This is The Daily Diff, under the hood. And why did fast queries still hit a ceiling? Shopify is a commerce platform for selling online and in person. Redis is an in-memory data store.

0:21 MySQL is a relational database, and Shopify already kept its inventory ledger there. A reservation briefly holds stock while a buyer pays.

Why were two stores risky?

0:29 Their Redis model decremented an item counter. Claiming a paid order meant updating the MySQL ledger and cleaning up Redis. Those separate writes could leave stock sold twice or unavailable when it should be sellable. The old model also lacked location awareness. The replacement must choose stock from somewhere that can fulfill the order. A warehouse on the wrong continent makes an excellent database entry and a terrible delivery promise. Earlier MySQL attempts used a quantity row, so competing checkouts queued at the

What becomes the lockable unit?

0:56 same lock. Think of a velvet rope around a spreadsheet cell. Adding more workers just lengthens the queue. Shopify changed the lockable object. Each available unit gets its own row. A locking read skips units another transaction holds and selects other eligible units. Different workers can acquire different rows, while the hot counter stays out of their way.

What happens when the pool empties?

1:15 That pool is bounded at a thousand rows per item and location. Replenishment draws from the ledger. If it empties, the reserve path replenishes inline, with competing requests waiting behind a replenishment lock. Empty pool does not mean empty warehouse. Reserve deletes selected pool rows, then inserts reservation records in a transaction. Commit releases the database locks.

1:35 Rollback undoes the changes. The reservation survives payment processing as stored state. Successful payment claims the ledger and removes the reservation atomically. Database locks never need to babysit the payment form. Their composite primary key starts with shop, item, group, then unit identity. Matching the lookup reduced index locking in their prototype. They also use read committed to avoid the gap locks that blocked pool

1:57 replenishment, and a consistent table order to prevent circular waits. The published example records an expiry time. Abandoned payments need stock released eventually, or the shopping cart becomes a landlord. Shopify's post leaves that cleanup algorithm unspecified, so this diagram shows the lifecycle requirement. Here's the catch.

What does SKIP LOCKED leave out?

2:14 Skip locked excludes locked rows, so the manual calls its result an inconsistent view. It provides neither a complete stock count nor fair turn-taking. Keep the availability decision and replenishment rules around it.

Where was the real ceiling?

2:26 And that ceiling? Other checkout code was holding database connections too long. Shopify tagged callers and measured connection hold time, then cleaned up the checkout path and revisited thread concurrency. Fast queries can still queue outside the query.

Why would I ship this design?

2:38 They shadow-wrote both systems with Redis authoritative, compared outcomes, then switched gradually with a kill switch. My verdict is ship it. I'd ship the shared transaction boundary and that rollbackable rollout, with the whole checkout path instrumented. Got a question about this? Put it in the comments. And that's the diff for today.

2:54 I'm Niko from Axrisi. Merge responsibly.

Sources

  1. We replaced Redis with MySQL for inventory reservations—and it scaledShopify Engineering — Emilie Noel
  2. Simplified reservation SQL embedded in Shopify's articleShopify Engineering / CourtneySymons on GitHub Gist
  3. MySQL 8.0 — Locking ReadsOracle / MySQL Reference Manual
  4. MySQL 8.0 — Transaction Isolation LevelsOracle / MySQL Reference Manual
  5. What Is Shopify and How Does It Work?Shopify
  6. Redis quick startsRedis documentation
  7. What is MySQL?Oracle / MySQL Reference Manual

Niko from Axrisi

Nikoloz Turazashvili · Vexrail · Axrisi

LinkedIn · dev.to · GitHub · X · Substack

Related videos