# CLAUDE.md This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. ## Project Overview FlashSaleSystem is a high-concurrency flash sale (秒杀) system built with Spring Boot and Redis Cluster. The system implements distributed architecture with anti-overselling mechanisms, rate limiting, and distributed locking using Redis and Lua scripts. **Project Completion**: 90% ## Core Architecture ### Technology Stack - **Backend**: Spring Boot 2.7.6, Java 1.8 - **Database**: MySQL with JPA/Hibernate - **Cache**: Redis Cluster with Redisson - **Frontend**: JSP + JSTL + Bootstrap 5 + jQuery - **Build Tool**: Maven - **API Documentation**: Knife4j (Swagger) ### Key Components - **Controllers**: Handle HTTP requests for different modules (flash sales, cart, orders, admin) - **Services**: Business logic layer with Redis-based caching and distributed operations - **Repositories**: JPA data access layer for MySQL operations - **DTOs**: Data transfer objects for API communication - **Entities**: JPA entities mapping to database tables ### Redis Integration - **Cluster Configuration**: Multi-node Redis cluster setup via RedissonConfig - **Data Types**: String (locks, sessions), Hash (user/product info, cart), List (order queues), Set (successful users), ZSet (rankings) - **Lua Scripts**: Atomic operations for flash sales, distributed locks, rate limiting, cart operations - **Message Queues**: Pub/Sub for order status changes and inventory updates ## Common Development Commands ### Build and Run ```bash # Compile project mvn clean compile # Run application mvn spring-boot:run # Package application mvn clean package ``` ### Testing ```bash # Run all tests mvn test # Run specific test mvn test -Dtest=FlashSaleServiceTest mvn test -Dtest=RedisServiceTest ``` ### Database Setup ```bash # Create database mysql -u root -p CREATE DATABASE flash_sale_db; # Import schema and test data mysql -u root -p flash_sale_db < src/main/resources/sql/schema.sql mysql -u root -p flash_sale_db < src/main/resources/sql/test-data.sql ``` ## Application Configuration ### Key Configuration Files - `application.yml`: Main application configuration including Redis cluster, database, and custom flash sale settings - `pom.xml`: Maven dependencies and build configuration - `src/main/resources/lua/`: Lua scripts for Redis atomic operations ### Important Configuration Sections - **Redis Cluster**: Configured for 6-node cluster with authentication - **Flash Sale Settings**: Rate limiting (10 requests/minute), max quantity per user (1), stock preload timing - **Cache TTL**: User info (30min), product info (60min), flash sale data (10min) - **Database**: HikariCP connection pool with optimized settings ## Development Guidelines ### Service Layer Patterns - All Redis operations use Redisson for cluster support - Critical operations (stock deduction, cart updates) use Lua scripts for atomicity - Distributed locks prevent overselling using Redisson's RLock - Rate limiting implemented via sliding window algorithm in Lua ### Key Services - **FlashSaleService**: Core flash sale logic with distributed locking and Lua scripts - **RedisService**: Generic Redis operations wrapper - **DistributedLockService**: Distributed locking abstraction - **RateLimitService**: API rate limiting using Redis - **CartService**: Shopping cart operations with Redis Hash storage ### Testing Approach - Unit tests exist for core services (FlashSaleServiceTest, RedisServiceTest) - Integration tests should verify Redis cluster connectivity - Load testing recommended for flash sale scenarios ## Lua Script Usage The system includes 5 Lua scripts in `src/main/resources/lua/`: - `flashsale.lua`: Atomic stock deduction with overselling prevention - `distributed_lock.lua`: Distributed lock acquisition - `unlock.lua`: Safe distributed lock release - `rate_limit.lua`: Sliding window rate limiting - `cart_operation.lua`: Atomic shopping cart operations ## API Documentation - **Swagger UI**: http://localhost:8080/doc.html (Knife4j) - **API Docs**: http://localhost:8080/v3/api-docs ## Monitoring and Health - **Actuator Endpoints**: /actuator/health, /actuator/metrics, /actuator/prometheus - **Log Files**: logs/flash-sale-system.log with detailed Redis and SQL logging - **Debug Level**: Enabled for package com.org.flashsalesystem and Redis operations