FlashEngine is a lightweight, high-throughput flash sale processing engine built using Core Java. It simulates real-world e-commerce flash sale events where multiple concurrent users compete for limited inventory. The system leverages Java concurrency primitives to ensure thread safety, prevent overselling, eliminate duplicate purchases, and provide real-time analytics through an embedded HTTP server.
- Lock-free inventory management using
AtomicInteger - Supports concurrent purchase processing
- Duplicate purchase prevention
- Token bucket rate limiting
- Real-time transaction analytics
- Embedded HTTP server with interactive dashboard
- Zero external dependencies
Concurrent Client Requests
│
▼
Token Bucket Rate Limiter
│
▼
Lock-Free Inventory Allocation
(AtomicInteger Compare-And-Swap)
│
▼
Duplicate Purchase Prevention
(ConcurrentHashMap Key Set)
│
▼
Analytics & Metrics Engine
(Java Streams + System.nanoTime)
│
▼
Embedded HTTP Server Dashboard
| Component | Technology |
|---|---|
| Language | Java 17+ |
| Concurrency | AtomicInteger, ConcurrentHashMap, ExecutorService, CountDownLatch |
| Analytics | Java Stream API |
| Networking | com.sun.net.httpserver.HttpServer |
| Frontend | HTML, CSS, JavaScript |
| Dependencies | None |
FlashEngine-Java/
│
├── Main.java
├── WebServer.java
├── FlashSaleService.java
├── Product.java
├── PurchaseRecord.java
├── PurchaseResult.java
├── RateLimiter.java
├── SaleAnalytics.java
├── DuplicatePurchaseException.java
├── OutOfStockException.java
├── RateLimitExceededException.java
├── README.md
├── LICENSE
└── assets/
└── flashengine-demo.gif
Inventory updates are performed using AtomicInteger and Compare-And-Swap (CAS) operations, ensuring thread-safe stock allocation without explicit locks.
A token bucket rate limiter restricts each user to a fixed number of requests per second, protecting the system from excessive traffic.
A thread-safe ConcurrentHashMap.newKeySet() is used to ensure that each user can complete only one successful purchase during a flash sale.
Execution metrics are collected using System.nanoTime() and aggregated using the Java Stream API to calculate:
- Total successful purchases
- Failed transactions
- Average latency
- Minimum latency
- Maximum latency
- Total revenue
The application includes a lightweight web server built using com.sun.net.httpserver.HttpServer that serves an interactive dashboard and REST endpoints.
{
"analytics": {
"totalAttempts": 100,
"successfulOrders": 10,
"outOfStockCount": 85,
"rateLimitedCount": 5,
"totalRevenue": 4999.90,
"avgLatencyMs": 0.412,
"minLatencyMs": 0.085,
"maxLatencyMs": 1.840
}
}| Parameter | Value |
|---|---|
| Concurrent Threads | 100 |
| Initial Stock | 10 Items |
| Product Price | $499.99 |
| Rate Limit | 3 Requests/Second/User |
| Inventory Overselling | Prevented |
- Java 17 or later
- Git
git clone https://github.com/Shreeja-88/FlashEngine-Java.git
cd FlashEngine-Javajavac *.javajava WebServeror run the simulation directly
java Mainhttp://localhost:8080
- Multithreading
- ExecutorService
- Atomic Operations
- Compare-And-Swap (CAS)
- ConcurrentHashMap
- CountDownLatch
- Java Stream API
- Custom Exceptions
- Embedded HTTP Server
- RESTful API Design
- Thread-Safe Programming
