我们用 MySQL 替换了 Redis 用于库存预留——并且成功实现了扩展

During checkout, when a buyer clicks "Complete purchase," we need to guarantee the items they're buying are still available. If we get this wrong in one direction, two buyers purchase the same last unit: the merchant has to cancel an order, send an apology email, and eat the support cost. If we get it wrong in the other direction, we tell a buyer something is sold out when it isn't, and the merchant loses a sale they should have made.

在结账时,当买家点击“完成购买”时,我们需要保证他们购买的商品仍然有货。如果我们在这个方向上出错,两个买家购买了同一个最后一件商品:商家必须取消订单、发送道歉邮件,并承担客服成本。如果我们在另一个方向上出错,我们会在商品未售罄时告诉买家已售罄,导致商家损失了一笔本该做成的交易。

At Shopify's scale, either failure compounds fast. On Black Friday 2025, merchants on our platform hit a record $5.1 million in sales per minute at peak. Every one of those transactions touches inventory.

在 Shopify 的规模下,任何故障都会迅速叠加放大。在 2025 年黑色星期五,我们平台上的商家在高峰期创下了每分钟 510 万美元销售额的纪录。每一笔交易都会触及库存。

Our oversell protection system handles this by reserving inventory during payment processing—a short hold that prevents two concurrent checkouts from claiming the same unit. For years, this ran on Redis. When we moved toward a unified database strategy, we had to answer a hard question: could MySQL handle the same scale?

我们的超卖保护系统通过在支付处理期间预留库存来处理这个问题——这是一种短暂的锁定,可防止两个并发结账请求占用同一个单元。多年来,这一直运行在 Redis 上。当我们转向统一的数据库战略时,我们必须回答一个棘手的问题:MySQL 能否处理相同的规模?

Earlier attempts had failed. A single row with a quantity column couldn't handle the contention. MySQL 8's SKIP LOCKED feature introduced a different design: one row per inventory unit instead of one row per item. Inspired by 37signals' approach to database-backed load distribution, we rebuilt reservations on MySQL and hit our high-throughput targets during peak 2025 traffic.

早期的尝试失败了。带有数量列的单行无法处理这种争用。MySQL 8 的 SKIP LOCKED 特性引入了一种不同的设计:每个库存单位一行,而不是每个商品一行。受 37signals 的数据库支持负载分配方法的启发,我们在 MySQL 上重建了预留系统,并在 2025 年流量高峰期达到了高吞吐量目标。

But the hardest lesson wasn't about database design. It was discovering that the real bottleneck wasn’t what we were observing and measuring. This post walks through the solution and what we found a...

开通本站会员,查看完整译文。

Главная - Вики-сайт
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-06-26 18:13
浙ICP备14020137号-1 $Гость$