System Design: How Do You Handle Database Writes During Flash Sales in India?


The Big Question:
When designing high-throughput systems for Indian scale—like festival sales or flash ticket drops—direct database writes will instantly bottleneck your connection pool and lock rows.


🗳️ POLL:
Which pattern do you use to absorb massive write spikes?
🔴 A) Message Queue Buffering (Kafka/RabbitMQ $\to$ Async Worker $\to$ DB)
🔵 B) Redis Atomic In-Memory Locks (INCRBY / Lua Scripts $\to$ Sync later)
🟡 C) Database Sharding & Row Locking (Pessimistic/Optimistic Locks)
🟢 D) Rate Limiting at API Gateway (Throttle traffic before it hits backend)


💡 The Engineering Breakdown
Why Option A & B are Preferred for Scale:Redis Atomic Operations (Option B):
How it works: Instead of querying PostgreSQL/MySQL to check stock > 0, maintain the counter in Redis. Use atomic Lua scripts to decrement inventory instantly in RAM (<1\text{ ms})


Why it wins: Prevents database lock contention while guaranteeing zero double-selling.Message Queue Buffering (Option A):How it works: Accept the order, push the payload into Kafka or RabbitMQ, and return an immediate 202 Accepted response to the user.Why it wins: Decouples API availability from database write throughput. Background workers process database writes at a controlled, steady pace.


Why Option C Fails Under Heavy Concurrency:
Row Lock Bottlenecks: Holding a pessimistic lock (SELECT FOR UPDATE) across hundreds of concurrent database connections will quickly exhaust DB pool limits and cause widespread timeout errors (HTTP 504).


Key Takeaways
Decouple Writes: Never write directly to relational databases during extreme traffic surges.
Leverage In-Memory Speed: Use Redis for fast, atomic inventory checks before persisting state.
Async User Experience: Return fast "order processing" receipts to users and process actual stock allocation asynchronously via queues.


CTA
🇮🇳 Designing distributed systems for Indian enterprise scale?
Get deep-dive architecture breakdowns, practical trade-off analyses, and real-world system design guides.


👉 Join Techawks India to connect with senior architects and tech leads building resilient software!
System Design: How Do You Handle Database Writes During Flash Sales in India? The Big Question: When designing high-throughput systems for Indian scale—like festival sales or flash ticket drops—direct database writes will instantly bottleneck your connection pool and lock rows. 🗳️ POLL: Which pattern do you use to absorb massive write spikes? 🔴 A) Message Queue Buffering (Kafka/RabbitMQ $\to$ Async Worker $\to$ DB) 🔵 B) Redis Atomic In-Memory Locks (INCRBY / Lua Scripts $\to$ Sync later) 🟡 C) Database Sharding & Row Locking (Pessimistic/Optimistic Locks) 🟢 D) Rate Limiting at API Gateway (Throttle traffic before it hits backend) 💡 The Engineering Breakdown Why Option A & B are Preferred for Scale:Redis Atomic Operations (Option B): How it works: Instead of querying PostgreSQL/MySQL to check stock > 0, maintain the counter in Redis. Use atomic Lua scripts to decrement inventory instantly in RAM (<1\text{ ms}) Why it wins: Prevents database lock contention while guaranteeing zero double-selling.Message Queue Buffering (Option A):How it works: Accept the order, push the payload into Kafka or RabbitMQ, and return an immediate 202 Accepted response to the user.Why it wins: Decouples API availability from database write throughput. Background workers process database writes at a controlled, steady pace. Why Option C Fails Under Heavy Concurrency: Row Lock Bottlenecks: Holding a pessimistic lock (SELECT FOR UPDATE) across hundreds of concurrent database connections will quickly exhaust DB pool limits and cause widespread timeout errors (HTTP 504). Key Takeaways Decouple Writes: Never write directly to relational databases during extreme traffic surges. Leverage In-Memory Speed: Use Redis for fast, atomic inventory checks before persisting state. Async User Experience: Return fast "order processing" receipts to users and process actual stock allocation asynchronously via queues. CTA 🇮🇳 Designing distributed systems for Indian enterprise scale? Get deep-dive architecture breakdowns, practical trade-off analyses, and real-world system design guides. 👉 Join Techawks India to connect with senior architects and tech leads building resilient software!
0 Comentários 0 Compartilhamentos 51 Visualizações 0 Anterior