Building a High‑Performance Java Seckill System with SpringBoot, Redis, and RabbitMQ
This article details the design and implementation of a Java-based Seckill (flash‑sale) platform built with SpringBoot, MySQL, Redis, RabbitMQ, and front‑end technologies, covering password hashing, distributed sessions, caching strategies, asynchronous order processing, oversell prevention, rate limiting, and provides code snippets and deployment screenshots.
Project Overview
This project is a simplified Seckill (flash‑sale) system built with SpringBoot. It implements login, product list, product details, flash‑sale purchase, and order details, using asynchronous ordering, hotspot data caching, and mechanisms to prevent overselling.
Technology Stack
Backend: SpringBoot, MyBatis, MySQL, RabbitMQ, Redis
Frontend: HTML, jQuery, Thymeleaf
Implementation Details
User Password Double MD5 Encryption
First MD5 hash protects the password during network transmission; a second MD5 hash protects the stored password against database theft.
Distributed Session Management
After successful login, a UUID token is stored in Redis as the session key and also placed in a cookie; subsequent requests retrieve the user object from Redis using the token.
Unified Exception Handling
Custom interceptor combined with @ControllerAdvice and @ExceptionHandler captures all exceptions and returns a unified response.
Page and Object Caching
Rendered HTML pages are cached in Redis to reduce server processing. Frequently accessed objects such as user, product, and order are also cached; flash‑sale products are pre‑loaded into Redis at startup.
Page Staticization
Front‑end uses AJAX to fetch data and bind it to the page; after the first load the page and data are cached in the browser.
Memory Flag + Redis Pre‑decrement + RabbitMQ Asynchronous Processing
private Map<Long, Boolean> localOverMap = new HashMap<>();
// check memory flag
boolean over = localOverMap.get(goodsId);
if (over) {
return Result.error(CodeMsg.MIAO_SHA_OVER);
}
// Redis pre‑decrement
long stock = redisService.decr(GoodsKey.getSeckillGoodsStock, "" + goodsId);
if (stock < 0) {
localOverMap.put(goodsId, true);
return Result.error(CodeMsg.MIAO_SHA_OVER);
}
// check duplicate order
SeckillOrder order = orderService.getOrderByUserIdGoodsId(user.getId(), goodsId);
if (order != null) {
return Result.error(CodeMsg.REPEATE_MIAOSHA);
}
// push to queue
SeckillMessage sm = new SeckillMessage();
sm.setUser(user);
sm.setGoodsId(goodsId);
sender.sendSeckillMessage(sm);The memory flag reduces Redis calls; Redis pre‑decrements stock at startup; RabbitMQ processes orders asynchronously, allowing the client to poll for the result.
Oversell Prevention
(1) Update SQL only when stock > 0:
update seckill_goods set stock_count = stock_count - 1
where goods_id = #{goodsId} and stock_count > 0(2) Unique index on user‑id and product‑id prevents a user from buying the same item twice.
(3) Optimistic lock with a version field retries on conflict.
Interface Rate Limiting
Custom @AccessLimit annotation limits the number of requests per user within a time window, e.g., max 5 requests per 5 seconds for logged‑in users.
@AccessLimit(seconds=5, maxCount=5, needLogin=true)
@RequestMapping(value="/path", method=RequestMethod.GET)
@ResponseBody
public Result<String> getSeckillPath(User user, @RequestParam("goodsId") long goodsId) {
if (user == null) {
return Result.error(CodeMsg.USER_NO_LOGIN);
}
String path = seckillService.createPath(user, goodsId);
return Result.success(path);
}Demo Screenshots
Code structure, login page, product list, product details, order details, etc.
Source Code
Download: https://gitee.com/jike11231/sec-kill-product
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
