Backend Development 12 min read

High-Concurrency Flash Sale Solutions: Pressure Distribution, Redis + MQ, and Inventory Hint Techniques

This article examines common industry approaches for handling massive flash‑sale traffic, detailing pressure‑distribution sharding, Redis + MQ integration, the Inventory Hint optimization, and provides concrete Lua script examples and transactional‑message workflows for reliable stock deduction.

Zhuanzhuan Tech
Zhuanzhuan Tech
Zhuanzhuan Tech
High-Concurrency Flash Sale Solutions: Pressure Distribution, Redis + MQ, and Inventory Hint Techniques

Flash‑sale (秒杀) events generate extreme request spikes that can overwhelm a single MySQL table, so a combination of techniques is required to ensure stability and prevent overselling.

1.1 Pressure Distribution (Sharding) – By splitting a product’s inventory into multiple buckets, load is spread across groups, but designers must handle even bucket allocation, fragment reconciliation, dynamic scaling, and coordination rules.

1.2 Redis + MQ – Redis offers fast, atomic reads/writes to guard against oversell, while a message queue (MQ) buffers bursts and smooths downstream database pressure; together they form a “golden pair” for high‑throughput flash sales.

1.3 Inventory Hint – Some companies rely on Alibaba Cloud RDS’s built‑in Inventory Hint feature, which optimizes hot‑row updates in MySQL, allowing direct database writes even under heavy concurrency.

1.4 Combined Approach – For millions of QPS, sharding, Redis caching, and MQ buffering are orchestrated to share load, reduce lock contention, and maintain fast response times.

2 Redis + MQ Solution – Stock is pre‑deducted in Redis using a Lua script that guarantees atomicity. The script checks for duplicate submissions via a token, validates stock, updates the inventory, records a timestamp, and stores a transaction log in a hash. /* * KEYS[1] -- product id * KEYS[2] -- user id (uid) * ARGV[1] -- deduction amount * ARGV[2] -- user 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], cjson.encode({action='扣减库存', product=product_id, from=stock, to=remaining_stock, change=ARGV[1], token=ARGV[2], timestamp=currentTimeMillis}))\n" + "return remaining_stock";

The script performs three main tasks: (1) prevents duplicate submissions using a token, (2) atomically deducts stock, and (3) records a transaction log for later consistency checks.

2.2 MySQL Stock Deduction – The final database update is performed via RocketMQ transactional messages: a half‑message is sent, the local Redis Lua result determines COMMIT or ROLLBACK, and RocketMQ’s message‑check and retry mechanisms ensure the MySQL update and log are reliably persisted.

2.3 Reason for Transaction Logs – Recording logs enables reconciliation between order tables and inventory, and alternative consistency solutions such as Seata or TCC can be employed.

3 Inventory Hint Database Deduction – By adding a special hint clause to SQL statements, MySQL groups hot‑row updates, reduces row‑level lock wait time, minimizes B‑tree index traversals, and cuts transaction commit frequency, dramatically improving performance under extreme concurrency.

4 Summary – Choose the appropriate combination of techniques based on traffic volume: simple locking for low load, Redis + MQ for moderate load, and add pressure‑distribution sharding plus Inventory Hint for the highest scale.

backend architectureRedishigh concurrencyMessage QueueLua Scriptflash saleInventory Hint
Zhuanzhuan Tech
Written by

Zhuanzhuan Tech

A platform for Zhuanzhuan R&D and industry peers to learn and exchange technology, regularly sharing frontline experience and cutting‑edge topics. We welcome practical discussions and sharing; contact waterystone with any questions.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.