System Design of a flash sale e-commerce.
Nikhil Kumar
Angel investor , Engineering Director | Ex Microsoft, Amazon, Intel, UIPath, Whatfix
Problem Statement:
The task is to design a Flash Sale Pre-Checkout System for a food delivery app that wants to sell a specific food item from a top-rated seller within a short time frame. The system needs to handle millions of users competing for a limited stock, ensuring scalability, efficiency, and accurate inventory allocation.
Key Functional Requirements
Key Non-Functional Requirements
Back-of-the-Envelope Calculation
A rough estimation of concurrent users is necessary to determine how much load the system needs to handle.
Estimating Concurrent Users
Given:
This means our system needs to handle at least 115 concurrent users every second, assuming a uniform traffic pattern. However, in reality:
Peak Load Assumption
Thus, our system must be designed to handle at least 10,000–15,000 requests per second (RPS) during peak traffic.
Things That Are Out of Scope:
1. Payment Processing & Payment Failures
? Reason for Exclusion: Payment processing is handled by dedicated payment gateways (Stripe, Razorpay, PayPal, etc.) and requires PCI compliance and fraud detection mechanisms.
2. Order Delivery & Logistics
? Reason for Exclusion: Delivery systems involve fleet management, real-time tracking, and delivery slot optimizations, which are completely separate from pre-checkout operations.
3. Order Cancellation & Abandonment Handling
? Reason for Exclusion: Order cancellations require timer-based inventory restoration and business logic to prevent abuse (e.g., users reserving multiple items without buying them). These optimizations can be handled by a cart management service.
4. Personalized Recommendations & Dynamic Pricing
? Reason for Exclusion: Such features require machine learning models and integration with pricing engines, which are not critical to the core inventory reservation problem.
5. Seller Inventory Management & Restocking
? Reason for Exclusion: Flash sales typically work with pre-allocated stock, and dynamic inventory updates introduce complexity and inconsistencies during a high-traffic event.
Final Scope Summary
? Covered in This Article
? Not Covered (Out of Scope)
System Design Architecture
High-Level Overview
Key Components:
Detailed Component Breakdown:
领英推荐
Redis as the Primary Inventory Store
erDiagram REDIS_CACHE { int customer_id string order_status }
? Why Redis?
?? Redis Key Structure
inventory_count = 20,000 # Decremented atomically reservation:{customer_id} = {status} # Tracks reservations
?? How it Works:
Order Processing Pipeline
To prevent bottlenecks and system crashes, we use an asynchronous, event-driven pipeline.
? Advantages of this pipeline
Handling Scalability Challenges
The Thundering Herd Problem
?? Issue: Millions of users rushing in at the same time overloads Redis. ?? Solution: Introduce Rate Limiting + Distributed Locks
Preventing Race Conditions in Order Placement
?? Issue: Two users reserving the last item at the same time. ?? Solution: Atomic Redis Transactions
MULTI DECR inventory_count SET reservation:{customer_id} CONFIRMED EXEC
?? This ensures either both succeed or both fail.
Order Failures & Retries
?? Issue: If an order fails, how do we retry? ?? Solution: Dead Letter Queue (DLQ) + Retry Worker
Scaling Horizontally
?? Issue: Single server cannot handle millions of users. ?? Solution: Horizontally Scale Order Service
Final Thoughts
? Scalable & Resilient – Can handle millions of users.
? No Race Conditions – Atomic Redis transactions prevent double booking.
? High Availability – Asynchronous queue ensures no service downtime.
? Cost Efficient – Optimized architecture prevents wasteful spending.
Additional Improvements
?? Optimistic Locking for High Contention: Use WATCH in Redis to detect conflicts.
?? AI-powered Demand Prediction: Use ML to pre-stock inventory in high-demand locations.
?? User Experience Enhancements: Show real-time stock updates to users.
?? Use Stream Processor like apache Spark : Avoid availability overheads with embedded DB. Can be paired with CDC tools as well.
This architecture ensures a smooth, fair, and reliable flash sale experience for millions of users. ??