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