High‑Concurrency Seckill Solutions: Redis + MQ, Pressure Distribution, and Inventory Hint Techniques
This article examines common industry practices for handling massive e‑commerce flash‑sale traffic, detailing pressure‑distribution, Redis + MySQL, Redis + MQ, and Alibaba's Inventory Hint approaches, and explains how Lua scripts, transactional MQ messages, and database hints together ensure atomic stock deduction and consistency under extreme load.
1. Common Industry Practices
To cope with the surge of requests during a flash‑sale (seckill), many teams adopt a combination of techniques such as pressure distribution (bucket strategy), Redis + MySQL, the proprietary Inventory Hint feature, and a hybrid of pressure distribution with Redis and MQ.
1.1 Pressure Distribution
Bucket‑based sharding splits a single product’s inventory into multiple groups, reducing contention at peak moments. The design must address even bucket allocation, fragment handling, dynamic scaling, and coordination rules.
1.2 Redis + MySQL
Redis provides ultra‑fast reads/writes and atomic operations to prevent overselling, while MQ buffers the burst of requests, smoothing the load on the downstream database.
1.3 Inventory Hint
Some companies rely on Alibaba Cloud RDS with the built‑in Inventory Hint technology, which optimises database handling of high‑concurrency stock updates without adding extra caching layers.
1.4 Pressure Distribution + Redis + MQ
For millions of QPS, a layered solution—SQL batch execution, distributed/local caches, and bucket sharding—shares the load and keeps the promotion stable.
2. Redis + MQ Solution for Seckill
2.1 Redis Stock Pre‑Deduction
The stock is decremented atomically using a Redis Lua script. The script checks for duplicate submissions via a token, validates stock, updates the remaining quantity, records a transaction log, and returns the new stock value.
/*
* KEYS[1] -- product id
* KEYS[2] -- user id (uid)
* ARGV[1] -- deduction amount
* ARGV[2] -- user‑submitted token
*/
String luaScript = "redis.replicate_commands()\n" +
"if redis.call('hexists', KEYS[2], ARGV[2]) == 1 then\n" +
"return redis.error_reply('repeat submit')\n" +
"end \n" +
"local product_id = KEYS[1] \n" +
"local stock = redis.call('get', KEYS[1]) \n" +
"if tonumber(stock) < tonumber(ARGV[1]) then \n" +
"return redis.error_reply('stock is not enough') \n" +
"end \n" +
"local remaining_stock = tonumber(stock) - tonumber(ARGV[1]) \n" +
"redis.call('set', KEYS[1], tostring(remaining_stock)) \n" +
"local time = redis.call('time') \n" +
"local currentTimeMillis = (time[1] * 1000) + math.floor(time[2] / 1000) \n" +
"redis.call('hset', KEYS[2], ARGV[2], \n" +
"cjson.encode({action='扣减库存', product=product_id, from=stock, to=remaining_stock, change=ARGV[1], token=ARGV[2], timestamp=currentTimeMillis})\n" +
") \n" +
"return remaining_stock";Lua Script Workflow
The script uses a hash to store per‑user tokens and a string key for product stock, ensuring idempotency, atomic stock reduction, and transaction logging.
2.2 MySQL Stock Deduction via MQ
After the Redis step, a RocketMQ transactional message is sent. The local transaction checks the Lua script result; on success it commits, otherwise rolls back. The consumer then applies the stock update to MySQL and records the same log, relying on MQ’s retry mechanism for consistency.
2.3 Reason for Recording Operation Logs
Transaction logs enable reconciliation between the order table and stock changes, and allow fallback mechanisms such as Seata or TCC for distributed transaction guarantees.
3. Inventory Hint Database Deduction
3.1 Usage
Developers add a special hint clause to SQL statements; the database interprets it as a hot‑update marker.
3.2 Principle
The hint tells MySQL to group updates on the same primary key within a short window, reducing row‑level lock wait time, minimizing B‑tree index traversals, and cutting the number of transaction commits by batch‑submitting grouped updates.
4. Summary
Seckill architectures must be chosen according to traffic volume. Light traffic can rely on a single locked table; medium traffic benefits from Redis + MQ; extreme traffic demands pressure distribution, Redis + MQ, and optionally Inventory Hint to unlock database potential. The right combination of these techniques ensures stable, high‑throughput flash‑sale systems.
Cognitive Technology Team
Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.