The Last Mango Problem: Race Conditions in a Real Marketplace
说明与用途
GleanGrid 通过使用事务和锁定读取,按顺序锁定,合计购物车行,强制执行优惠券限制,并锁定取货窗口计数查询,解决了并发结账竞争问题。
问题
Concurrent checkout requests can both read the same stock or coupon data before either writes, leading to over‑booking or duplicate coupon usage.
结果
Race conditions were eliminated by making all reads that influence decisions locking reads within a single transaction, ordering locks to prevent deadlocks, and enforcing constraints at the database level.
工作流
- Start transaction
- Lock product rows with lockForUpdate
- Sum duplicate lines per product
- Check stock and decrement
- Lock coupon row, assert usability, create redemption, increment used_count
- Lock pickup slot row, count bookings with lockForUpdate, reject if full
- Commit transaction
证据
Run that from two requests at once and both pass the if before either one saves.
Lock the slot, count the bookings, reject if it's full.
Make the count a locking read too:
- 记录日期: 2026-10-01T21:10:54.515Z