Architecture & Concurrency Control:
- Goal: Sell 10,000 items in 60 seconds with 1,000,000 concurrent buyers, strictly avoiding overselling, database lock contention, or checkout crashes.
1. Ingress & Traffic Smoothing:
- Edge CDN aggressively caches static product pages; API gateway blocks non-authenticated requests and enforces aggressive IP/user rate limiting.
- Virtual Waiting Room: Route excess traffic into an asynchronous queue (RabbitMQ / Redis queue), letting users into the checkout flow in controlled batches.
2. In-Memory Inventory Reservation:
- Never decrement inventory with relational database row locks during a flash sale (causes deadlocks and pool exhaustion).
- Pre-load inventory count into an in-memory Redis cluster. Execute inventory decrement using an atomic Redis Lua script:
if redis.call('get', KEYS[1]) > 0 then return redis.call('decr', KEYS[1]) else return 0 end.
- Successful decrement creates a temporary reservation token with a 10-minute TTL.
3. Asynchronous Order Processing:
- Reserved orders publish to an order creation Kafka topic.
- Background workers process payment collection and write permanent records to PostgreSQL.
- If user abandons or payment fails, token expires and inventory is incremented back into Redis.