Backend Development 10 min read

Design and Implementation of a High‑Concurrency WeChat Red Packet System Using Redis and Bloom Filter

This article details the end‑to‑end design, database schema, Redis atomic operations, Bloom filter integration, JMeter load testing, and SpringBoot implementation for a high‑concurrency WeChat red‑packet (抢红包) service, including flash‑sale (秒杀) techniques and Lua scripts for atomic stock handling.

Java Captain
Java Captain
Java Captain
Design and Implementation of a High‑Concurrency WeChat Red Packet System Using Redis and Bloom Filter

The article presents a comprehensive design and implementation of a WeChat red‑packet (红包) system that handles high‑concurrency grabbing using Redis atomic operations and a Bloom filter for duplicate prevention.

It begins with a business‑process analysis and functional decomposition, describing the creation of a red packet (inserting records into MySQL and Redis) and the concurrent grabbing logic that relies on Redis string DECR/DECRBY commands to atomically decrement the remaining count and amount.

Database schemas for CREATE TABLE `red_packet_info` ( ... ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='红包信息表,新建一个红包插入一条记录'; and CREATE TABLE `red_packet_record` ( ... ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='抢红包记录表,抢一个红包插入一条记录'; are provided, followed by API definitions for issuing and claiming red packets, including steps to update MySQL, Redis counters, and record transactions.

A JMeter load test simulates 50 concurrent requests for 10 red packets with a total amount of 20 000 cents, demonstrating that only the first ten requests succeed.

The article then introduces Bloom filters, explaining their principle, advantages, disadvantages, and typical use cases such as traffic filtering, URL deduplication, and anti‑spam.

It shows how to use a Google Guava Bloom filter to implement a member‑only lottery, with Maven dependency <dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>29.0-jre</version></dependency> , a MySQL member table, and sample Java code.

Next, the RedisBloom module is described, including download, compilation, and loading into Redis via loadmodule /usr/soft/RedisBloom-2.2.4/redisbloom.so , as well as basic commands.

Integration with SpringBoot is demonstrated by adding two Lua scripts for adding to and checking the Bloom filter: local bloomName = KEYS[1] local value = KEYS[2] local result_1 = redis.call('BF.ADD',bloomName,value) return result_1 and local bloomName = KEYS[1] local value = KEYS[2] local result_1 = redis.call('BF.EXISTS',bloomName,value) return result_1 , and corresponding Java service methods.

The final part covers a flash‑sale (秒杀) scenario: business flow, Redis‑based stock decrement, order persistence via MQ, and Lua scripts ensuring atomic stock updates, with detailed Redis key conventions and sample commands such as set skuId_start_1 0_1554045087 , set skuId_access_1 12000 , set skuId_count_1 0 , set skuId_booked_1 0 .

All source code snippets are kept intact within ... blocks, and the article concludes with download links for the full project.

JavaDatabaseRedishigh concurrencySpringBootBloom Filterseckill
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

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.