Home Β» Backend Dev Β» Designing a High-Concurrency Flash Sale Stock & Inventory Reservation System with Node.js, Redis, Lua, and MongoDB

Designing a High-Concurrency Flash Sale Stock & Inventory Reservation System with Node.js, Redis, Lua, and MongoDB

Managing product inventory in e-commerce or high-concurrency systems can be tricky. What happens when 10,000 users try to buy the same product at the same time? Without careful design, you risk overselling, inconsistent stock counts, or database bottlenecks.

Without careful design, you risk overselling ❌, inconsistent stock counts 🀯, or database bottlenecks 🐌.

Designing a High-Concurrency Flash Sale Stock & Inventory Reservation System with Node.js, Redis, Lua, and MongoDB

1. 🧐 Problem Statement

πŸ‘‰ Imagine a product with only 5 items in stock πŸ“¦.
If 20 people click β€œBuy” at the same time πŸƒπŸƒπŸƒ, how do we make sure only the first 5 succeed βœ… and the other 15 get out of stock ❌?

  • MongoDB alone = too slow 🐒.
  • Redis + Lua = lightning-fast ⚑ and safe πŸ›‘οΈ.

2. πŸ› οΈ Core Concepts

  • Redis 🧠 β†’ Super fast in-memory DB (like a sticky note always ready).
  • Lua Script 🧩 β†’ Ensures atomic operations, no interruptions.
  • Tokens 🎟️ β†’ Each unit of stock is one token in Redis.
  • MongoDB πŸ“š β†’ Keeps long-term order + reservation data.
  • Cron Jobs ⏰ β†’ Small background workers that clean up expired reservations.

πŸ‘‰ Together = a safe stock system πŸ—οΈ.


crete simple box and line arrow diagram image proper of the archetrurea
Simple Architecture

3. πŸ”„ System Flow Diagram

πŸ‘€ Client (User clicks Buy)
  β”‚
  β–Ό
🟒 Node.js (Express API)
  β”‚
  β”‚ 1️⃣ Ask Redis for tokens
  β–Ό
πŸ”΄ Redis (Tokens per product)
  β”‚
  β”‚ 2️⃣ Lua script reserves tokens
  β–Ό
πŸ“¦ MongoDB
  β”‚
  β”‚ 3️⃣ Save Order + Reservation
  β–Ό
⏰ Cron Job
  β”‚
  β”‚ 4️⃣ Expired order β†’ tokens back
  β–Ό
πŸ”΄ Redis (stock restored)

πŸ‘‰ Think of Redis as a ticket counter 🎫, Mongo as the ledger πŸ“’, and Cron as the cleaning robot πŸ€–.


4. 🎟️ Token-Based Stock Reservation

When seeding products, you push tokens into Redis:

await client.lpush("product:101:tokens", ["token:1","token:2","token:3"]);
  • Each token = 1 unit of stock πŸ“¦.
  • Buy 2 units β†’ 2 tokens are popped out πŸ–οΈπŸ–οΈ.
  • No tokens left β†’ product is sold out 🚫.

πŸ‘‰ Super simple & visual = no overselling! πŸŽ‰


5. 🧩 Lua Script for Atomic Reservation

Why Lua? πŸ€”

Because if 1,000 users click buy at once πŸ‘₯πŸ‘₯πŸ‘₯, normal loops fail.

Lua = all or nothing 🎯

-- Reserve tokens in one atomic step
if enough tokens β†’ success βœ…
else β†’ rollback πŸ”„

πŸ‘‰ Think of Lua as a bouncer at the door πŸšͺ:
Nobody sneaks in, everything is fair βœ‹.


6. πŸ“š MongoDB Orders & Reservations

Redis is fast but temporary ⚑🧠.
So we also save results in MongoDB for safety.

  • Order 🧾 β†’ Who bought what.
  • Reservation πŸ—‚οΈ β†’ Which tokens are locked + expiry time ⏳.

πŸ‘‰ Mongo is like the history book πŸ“– so nothing gets lost if Redis resets.


7. ⏰ Handling Expiry with Cron Jobs

What if a user clicks buy but never pays? πŸ’³βŒ

  • Stock can’t stay locked forever β›”.
  • Solution: Cron Job runs every few seconds πŸ•.
  • Expired reservations β†’ tokens go back to Redis ♻️.

πŸ‘‰ Cron = the janitor 🧹 cleaning unused tokens.


8. βš–οΈ Pros & Cons

βœ… Pros

  • Super fast ⚑ (Redis in-memory).
  • Atomic πŸ›‘οΈ (Lua prevents race conditions).
  • Scalable πŸ“ˆ (works in flash sales).

❌ Cons

  • More moving parts βš™οΈ (Redis + Mongo + Cron).
  • Must seed tokens correctly 🎟️.
  • Sync issues if stock changes outside Redis πŸ”„.

πŸ‘‰ But overall β†’ Worth it for high concurrency πŸ’ͺ.


9. πŸ—οΈ Microservice Use

This design works beautifully in a microservice world 🌍:

  • Product Service πŸͺ β†’ Seeds tokens in Redis.
  • Order Service πŸ“¦ β†’ Handles token reservation + Mongo save.
  • Payment Service πŸ’³ β†’ Confirms payment, finalizes stock.

πŸ‘‰ Redis = the shared brain 🧠 for all services.


10. 🎯 Key Takeaways

  • Redis + Lua = safe, atomic stock ops βš‘πŸ›‘οΈ.
  • Tokens = 🎟️ simple way to represent stock units.
  • Mongo = πŸ“š history keeper.
  • Cron = ⏰ auto cleanup robot πŸ€–.
  • Perfect for πŸ›’ flash sales, ticketing systems 🎟️, and high-load apps πŸ’₯.

πŸ“– Example Story (Easy to Imagine)

  1. Product A has 3 items β†’ Redis = [t1, t2, t3].
  2. Alice buys 2 πŸ›’ β†’ gets [t1, t2].
  3. Bob tries 2 β†’ only [t3] left β†’ Bob gets ❌ out of stock.
  4. Alice doesn’t pay 😬 β†’ Cron returns [t1, t2] to Redis.

πŸ‘‰ Always fair βœ…, never oversells 🚫, and self-heals ♻️.


πŸ”₯ With Redis + Lua, your system can handle 10k+ concurrent buyers without overselling, while MongoDB + Cron ensures long-term consistency.


1 thought on β€œDesigning a High-Concurrency Flash Sale Stock & Inventory Reservation System with Node.js, Redis, Lua, and MongoDB”

  1. How do you handle partial inventory reservations if a user tries to buy more than is available in stock? Does the Lua script manage that or is it handled upstream

Leave a Comment

Your email address will not be published. Required fields are marked *