Blog 15 — Shopify: Surviving Flash Sales
C
Qubits of DPK
March 21, 2026
Core Case Studies
Core Concept: Distributed locks, Optimistic locking, Queue-based purchase flow, Preventing overselling
Why SDE-2 Critical: Directly relevant for Flipkart, Meesho, Amazon India — flash sale design is a real interview question
Status: Draft notes ready
Quick Revision
- Problem: Millions try to buy limited inventory at the same instant.
- Core pattern: Queue requests, reserve inventory atomically, confirm after payment.
- Interview one-liner: Flash-sale systems are throughput problems wrapped around correctness constraints.
️ The Core Problem
javascript
QUBITS OF DPK
Core Concepts
Optimistic Locking (Check-and-Set)
java
QUBITS OF DPK
- No DB locks held between read and write
- High throughput for low-contention scenarios
- Problem: Under 10M concurrent requests, retry storm = thundering herd again
Redis Atomic Decrement (Better for Flash Sales)
javascript
QUBITS OF DPK
Queue-Based Purchase Flow (Shopify's Actual Solution)
javascript
QUBITS OF DPK
Inventory Reservation (2-Phase Commit)
javascript
QUBITS OF DPK
Shopify's Infrastructure Preparation
javascript
QUBITS OF DPK
Scale Achieved
5 Interview Questions This Blog Unlocks
Q1. Design Amazon's Great Indian Festival flash sale
Answer: Queue all purchase requests. Workers process at controlled rate. Redis atomic DECR for inventory (single-threaded = no race conditions). 2-phase reservation: reserve on click, confirm on payment success, release on timeout. Pre-warm caches. Rate limit per user. Disable non-critical features under load.
Q2. What is the difference between optimistic and pessimistic locking?
Answer: Pessimistic: lock the row before reading, hold until transaction completes. Safe but slow — every other request waits. Optimistic: read without lock, include version in update condition. No locks held. If version mismatch → retry. Best for low-contention. For 10M concurrent flash sale requests, optimistic causes retry storm — use queue instead.
Q3. How does Redis atomic operations prevent overselling?
Answer: Redis is single-threaded. DECR command is atomic — read-modify-write happens as one operation, no other command executes between them. Even with 10M concurrent clients, Redis processes DECR one at a time. If result >= 0, sale proceeds. If result < 0, sold out. No race condition possible.
Q4. What is inventory reservation and why is it important?
Answer: Don't permanently deduct inventory when user clicks Buy. Instead, reserve (soft-lock) for N minutes. If payment succeeds → confirm. If payment fails or times out → release reservation. This prevents: sold-out items when payments fail, inventory stuck in limbo, users being charged for items that can't be delivered.
Q5. How would you prevent a single user from buying 1000 units in a flash sale?
Answer: Rate limiting per user + per session. Redis SET user_purchased:userId with TTL. Check before allowing purchase. Limit: 1 unit per userId per flash sale event. Also: add CAPTCHA for bot detection, device fingerprinting, purchase history analysis for suspicious patterns.