Distributed DB
Architecting a Flash-Sale System for 100M+ Concurrent Users: Static/Dynamic Split, Atomic Redis Lua Deduction and Queue-Based Load Shedding
A systematic review of the layered defenses behind a flash-sale system that absorbs hundreds of millions of instantaneous requests: CDN static offload, gateway rate limiting, atomic stock pre-deduction with Redis + Lua, asynchronous persistence through RocketMQ, and inventory bucketing to escape hot-row locking in the database.
#High Concurrency
#Rate Limiter
#Redis Lua
stock_deduct.lualua
-- Redis Lua script: atomic stock pre-deduction with duplicate-order protection
local stockKey = KEYS[1]
local userOrderKey = KEYS[2]
local userId = ARGV[1]
local quantity = tonumber(ARGV[2])
-- 1. Has this user already ordered?
if redis.call('SISMEMBER', userOrderKey, userId) == 1 then
return -1 -- Already ordered; block the duplicate
end
-- 2. Check remaining stock
local currentStock = ton