All Levels75 min readยท Topic 12.3

E-commerce and marketplaces

Amazon, flash sale system, payment system, Airbnb, Uber

๐Ÿ›’Key Takeaways

  • 1
    Flash sales: pre-warm inventory in Redis, queue-based ordering, optimistic locking for stock deduction
  • 2
    Payment system: idempotency keys are critical โ€” every charge operation must be safely retryable
  • 3
    Uber: geospatial indexing (Google S2/H3), real-time matching, surge pricing, ETA calculation
  • 4
    Inventory: 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.

๐Ÿ’กThe Inventory Problem
Showing 'In Stock' on a product page can use eventual consistency (stale by seconds is fine). But the actual purchase must use strong consistency (optimistic locking or distributed lock) to prevent overselling. Design different consistency levels for different operations.

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

Knowledge Check1/1

Why are idempotency keys critical for payment systems?