Flash Sale with Limited Tickets

Problem Statements Limited inventory: Exactly 200 tickets, no overselling. One ticket per user: Strictly enforce (assuming logged-in users only). Handle high concurrency: 20k+ users rushing at sale start, avoid server overload/high peak load. Minimize database stress: Avoid hammering the persistent DB during the peak. Typical Solutions Fitting Your Node.js Setup To meet your requirements: Use Redis as the primary hotspot for inventory and user checks (in-memory, distributed via clustering). Pre-load remaining tickets as a counter (e.g., atomic DECR). Use a Set for sold users (check/add atomically). Best: Redis Lua script (or Redis function in v7+) for atomicity: check stock >0, check user not bought, decrement stock, add user. Enforce one per user via user ID in a Redis Set. Handle concurrency: Virtual queue (e.g., Redis list or separate service like RabbitMQ/Kafka) or waiting room to throttle ingress. Offload DB: Successful attempts go to a message queue (e.g., Kafka/RabbitMQ) for async persistence; failures reject immediately. Node.js scaling: Cluster with PM2, share Redis, use worker threads for non-blocking I/O. This pattern prevents overselling reliably while keeping peak DB hits near zero. ...

December 20, 2025

How to Optimize a System for 1 Million Concurrent Users

To keep your system running smoothly when millions of users access it at the same time, a Software Architect needs to consider many factors. Below is a comprehensive checklist of bottlenecks and optimization solutions to ensure your system is always ready for high traffic. 1. Bottleneck from monolith architecture All logic and resources are bundled together → difficult to scale Solutions: Switch to microservices Make services stateless to allow horizontal scaling Add an API Gateway (rate-limiting, circuit breaker) Use a service mesh (Istio, Linkerd) if observability is needed 2. DB bottleneck due to too many direct queries 1 million users can generate tens of millions of DB queries Solutions: ...

April 16, 2025