Design and Implementation of a High-Concurrency WeChat Red Packet and Seckill System Using Redis and Bloom Filters
This article details the business flow analysis, functional decomposition, database schema, API design, and high‑concurrency implementation of a WeChat red‑packet and seckill system, showcasing Redis atomic decrement, Bloom filter usage, Lua scripts, JMeter load testing, and integration with SpringBoot.
The article begins with a brief introduction explaining the motivation behind building a WeChat red‑packet project for a company anniversary, followed by a business process analysis and functional breakdown.
Functional Decomposition
New red‑packet creation involves inserting records into both MySQL and Redis. The concurrent red‑packet grabbing utilizes Redis string atomic decrement (DECR/DECRBY) to ensure thread‑safe operations, calculates the amount for the last packet, and updates the database.
Database design includes two tables: red_packet_info for red‑packet metadata and red_packet_record for individual grab records, with SQL definitions provided.
API Development
Red‑packet issuance API adds records to MySQL and Redis for count and amount. The grabbing API performs atomic decrement, checks remaining count, and updates the database, with failure handling when packets are exhausted.
A JMeter load test simulates high concurrency, showing that only the first ten requests succeed when 10 packets of 20000 total amount are issued.
Bloom Filter Overview
The article introduces Bloom filters, their advantages (space‑efficiency, constant‑time queries) and disadvantages (false positives, deletion difficulty), and lists typical use cases such as attack mitigation, URL deduplication, and cache‑penetration protection.
It then presents a membership‑lottery use case where a Bloom filter stores member IDs to quickly verify eligibility.
Implementation steps include adding Guava dependency, creating a MySQL user table, initializing the Bloom filter, and integrating Lua scripts for adding and checking elements:
\
\
com.google.guava\
\
guava\
\
29.0-jre\
\ local bloomName = KEYS[1]
local value = KEYS[2]
local result_1 = redis.call('BF.ADD', bloomName, value)
return result_1 local bloomName = KEYS[1]
local value = KEYS[2]
local result_1 = redis.call('BF.EXISTS', bloomName, value)
return result_1These scripts are invoked from a RedisService.java class.
Seckill (Flash Sale) Design
The seckill flow includes Redis‑based stock decrement, order persistence in MySQL, and asynchronous processing via MQ. Lua scripts ensure atomic stock deduction, preventing overselling.
set skuId_start_1 0_1554045087 --seckill flag
set skuId_access_1 12000 --allowed purchase count
set skuId_count_1 0 --purchase counter
set skuId_booked_1 0 --actual sold countGateway layer checks if the seckill has started and uses Redis INCR to throttle traffic. Order validation leverages Bloom filters to efficiently track whether a user has already purchased.
Lua scripts are used for atomic stock deduction, with success or failure responses returned to the client.
Various diagrams (omitted here) illustrate the architecture, and JMeter results demonstrate the system’s ability to handle high concurrency while maintaining atomicity.
Finally, the article provides download links for the full project source code and encourages readers to set up Redis clusters for further testing.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
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.