Skip to content

ayuryntii/Flashsale-Engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Typing SVG

Go React Redis RabbitMQ PostgreSQL Docker


A production-grade Flash Sale system built on a Microservices architecture to solve real-world Race Condition problems at scale β€” featuring a live Cyber HUD Admin Dashboard.


πŸ”΄ Live Demo Β· πŸ“– Documentation Β· πŸ› Report Bug


🧠 The Problem This Solves

In a standard Flash Sale, thousands of users click "Buy" simultaneously. A naive implementation leads to:

  • ❌ Overselling β€” Stock goes negative because multiple threads read the same stock value at the same time.
  • ❌ Data Inconsistency β€” Orders are created for items that no longer exist.
  • ❌ System Collapse β€” A single monolithic server crumbles under the load.

This project solves all three using a distributed microservices architecture.


βœ… The Solution

Problem Solution Used
Race Condition on stock Redis DECRBY Atomic Operation (thread-safe, no lock needed)
Request overload RabbitMQ Message Queue (order requests are queued, not processed simultaneously)
Single point of failure Microservices (each service runs independently)
Slow inventory queries Redis Cache (sub-millisecond reads)

πŸ—οΈ System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        CLIENT (Browser)                      β”‚
β”‚              React Dashboard β”‚ Flash Sale Page               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚ HTTP / REST
                      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚          API GATEWAY (Go)        β”‚
β”‚     Rate Limiting Β· Routing      β”‚
β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚           β”‚
      β–Ό           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Product  β”‚  β”‚  Inventory   │───▢│  Redis (Atomic Counter) β”‚
β”‚ Service  β”‚  β”‚  Service     β”‚    β”‚  DECRBY for Stock        β”‚
β”‚  (Go)    β”‚  β”‚  (Go)        β”‚    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚ Publish
                     β–Ό
            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
            β”‚   RabbitMQ      │──▢│   Order Service (Go)  β”‚
            β”‚  Message Queue  β”‚   β”‚   Consumes & Persists β”‚
            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                              β”‚
                                              β–Ό
                                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                    β”‚   PostgreSQL DB   β”‚
                                    β”‚  (Persistent)     β”‚
                                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸŽ›οΈ Admin Dashboard Preview

Cyber HUD Theme with full Dark / Light Mode toggle β€” real-time system monitoring at a glance.

Feature Description
πŸ“Š Live Stats Stock, Orders, Queue length update every 4 seconds
🟒 Service Health Status indicator for Gateway, Order Processor, Inventory Sync
πŸ–₯️ Security Logs Live feed of system events with timestamps
🎨 Dark / Light Mode Smooth theme transition via CSS Variables
⚑ Control Center Trigger sessions, pause sales, flush cache

πŸš€ Tech Stack

Backend

Layer Technology Purpose
Language Go (Golang) High-performance, concurrent microservices
Web Framework Fiber Express-like HTTP framework for Go
Message Broker RabbitMQ Async order queuing
Cache & Atomic Ops Redis Sub-ms stock reads, Race Condition prevention
Database PostgreSQL Persistent order & product storage
Containerization Docker + Compose One-command environment setup

Frontend

Layer Technology Purpose
Framework React 18 + Vite 5 Fast SPA with HMR
Styling Vanilla CSS Custom Cyber HUD design system
Icons Lucide React Consistent icon library
Fonts Orbitron + Rajdhani Futuristic HUD typography

πŸ“¦ Getting Started

Prerequisites

1. Clone the repository

git clone https://github.com/yourusername/nexcore-flashsale-engine.git
cd nexcore-flashsale-engine

2. Start infrastructure (Redis, RabbitMQ, PostgreSQL)

docker-compose up -d

3. Start the Admin Dashboard (Frontend)

cd frontend
npm install
npm run dev

Dashboard will be available at http://localhost:5173

4. Start the Microservices (Backend)

# In separate terminals:
cd backend/gateway && go run main.go
cd backend/product && go run main.go
cd backend/inventory && go run main.go
cd backend/order && go run main.go

πŸ“ Project Structure

nexcore-flashsale-engine/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ gateway/        # API Gateway - routing & rate limiting
β”‚   β”œβ”€β”€ product/        # Product & Flash Sale schedule management
β”‚   β”œβ”€β”€ inventory/      # Stock management with Redis atomic ops
β”‚   β”œβ”€β”€ order/          # Order processing via RabbitMQ consumer
β”‚   └── shared/         # Shared models, config, utilities
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ App.jsx     # Main Dashboard component
β”‚   β”‚   └── index.css   # Cyber HUD Design System
β”‚   └── package.json
β”œβ”€β”€ docker-compose.yml
└── README.md

πŸ”¬ Key Concepts Demonstrated

1. Solving Race Condition with Redis Atomic Operations

// ❌ Unsafe (classic race condition)
stock := getStockFromDB()
if stock > 0 {
    updateStock(stock - 1)  // Another goroutine may do the same!
}

// βœ… Safe (atomic Redis operation)
remaining, err := rdb.Decr(ctx, "product:stock:1").Result()
if remaining < 0 {
    rdb.Incr(ctx, "product:stock:1") // Rollback
    return errors.New("out of stock")
}

2. Async Order Processing via RabbitMQ

// Producer (Inventory Service) β€” publishes order to queue
ch.Publish("", "order_queue", false, false, amqp.Publishing{
    ContentType: "application/json",
    Body:        orderJSON,
})

// Consumer (Order Service) β€” processes orders from queue
msgs, _ := ch.Consume("order_queue", "", true, false, false, false, nil)
for msg := range msgs {
    processAndSaveOrder(msg.Body) // Saved to PostgreSQL
}

πŸ‘¨β€πŸ’» Author

[Your Name]

GitHub LinkedIn

Informatics Student | Backend & Distributed Systems Enthusiast


πŸ“„ License

This project is licensed under the MIT License β€” see the LICENSE file for details.


Built with β˜• and a lot of debugging sessions

⭐ Star this repo if you find it useful!

About

Production-grade Flash Sale microservices built with Go, RabbitMQ & Redis. Solves Race Condition with atomic operations. Features a real-time Cyber HUD admin dashboard in React.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages