๐Key Takeaways
- 1Flash sales: pre-warm inventory in Redis, queue-based ordering, optimistic locking for stock deduction
- 2Payment system: idempotency keys are critical โ every charge operation must be safely retryable
- 3Uber: geospatial indexing (Google S2/H3), real-time matching, surge pricing, ETA calculation
- 4Inventory: eventual consistency is acceptable for display, strong consistency required for purchase
Designing E-Commerce at Scale
E-commerce systems combine nearly every system design concept: high-concurrency reads (product catalog), inventory management (distributed transactions), payment processing (idempotency), and real-time pricing. Flash sale scenarios are popular in interviews because they stress-test every layer.
System Breakdowns
Challenge: 100K users trying to buy 1000 items at exactly 12:00 PM.
Pre-warm: load inventory count into Redis before the sale. Use Redis DECR for atomic stock deduction.
Queue-based: accept all orders into a queue, process sequentially. Users get 'order placed, processing...' immediately.
Consistency: optimistic locking on inventory DB. If Redis and DB disagree, DB is source of truth.
Anti-fraud: rate limiting per user, CAPTCHA, device fingerprinting.
Core principle: every payment operation MUST be idempotent. Network failures mean the same request might be sent twice.
Idempotency key: client generates a unique key per payment intent. Server uses it to deduplicate.
State machine: Created โ Authorized โ Captured โ Settled (or Refunded). Each state transition is atomic.
Reconciliation: async job compares your records with payment provider's records daily.
Geospatial indexing: divide the world into cells (Google S2 or Uber H3). Drivers report location โ cell ID. Matching queries nearby cells.
Real-time matching: when rider requests, find available drivers in nearby cells, rank by ETA, send request.
Surge pricing: supply/demand ratio per cell. If demand >> supply, increase price multiplier.
ETA prediction: ML model trained on historical trip data + current traffic conditions.
Advantages
- โขRedis enables extremely fast inventory checks for flash sales
- โขIdempotency keys make payments safe and retryable
- โขGeospatial indexing enables real-time location queries
Disadvantages
- โขFlash sales require extensive pre-warming and capacity planning
- โขPayment reconciliation is complex and ongoing
- โขSurge pricing requires real-time supply/demand computation
๐งช Test Your Understanding
Why are idempotency keys critical for payment systems?