MAISON CODE .
/ Inventory · Redis · Backend

Race Conditions: Solving Real-Time Inventory Sync

Preventing overselling during High-Heat Drops. Using Redis and Optimistic Locking to manage stock across 5 channels.

AB
Alex B.
Race Conditions: Solving Real-Time Inventory Sync

Imagine you have 1 unit of a limited sneaker left. User A is on your website. User B is on your App. They both click “Buy” at the exact same millisecond (12:00:00.001). Shopify receives two requests. If not handled correctly, it sells the sneaker twice. You now have to email one customer and cancel their order. This is a PR disaster.

6. Eventual Consistency (The 2-Second Rule)

Your Shopify Admin says “Stock: 10”. Your ERP (NetSuite) says “Stock: 8”. Your Warehouse (WMS) says “Stock: 9”. Who is right? None of them. In a distributed system, consistency is eventual. We need a Single Source of Truth for the “Allocated” stock. We use Redis as the authoritative “Allocation Table”. Only after the order is shipped do we decrement the ERP.

7. Safety Stock Buffers

“If stock < 5, mark as Out of Stock.” This is a crude but effective strategy for high-throughput items. If you sell 100 items per second, the latency of sync (200ms) means you might oversell by 20 items. Solution: Dynamic Safety Stock. If sales velocity > 10/min, increase safety stock to 10. If sales velocity < 1/min, reduce safety buffer to 0. This maximizes sell-through while shielding you from negative inventory.

9. Dark Stores and Ship-from-Store

Inventory isn’t just in the Warehouse. It’s in the retail stores. We implement Omnichannel Sync. The “Flagship Store” on 5th Ave is also “Warehouse #2”. If the Main Warehouse is OOS, the system checks Store Inventory. It routes the order to the store iPad. The store associate picks, packs, and ships. This unlocks 20% more inventory that was previously “trapped” on shelves.

10. Return Logistics Sync

When a return is initiated (/returns), the item is technically “In Transit”. It is not “Available To Sell”. But we know it’s coming back. For high-demand items, we can enable Backorders. “Available in 5 days” (Expected Arrival of Return). We sync the return logistics carrier events (FedEx Scan) to update the status in Shopify. This recovers revenue from lost sales.

11. Conclusion: Optimistic Locking & Redis

We cannot rely on the database simply stating stock = 1. We need a Distributed Lock.

sequenceDiagram
    participant UserA
    participant UserB
    participant Redis
    participant Shopify

    UserA->>Redis: SETNX lock:sku_123 1
    Redis-->>UserA: OK (Lock Acquired)
    UserB->>Redis: SETNX lock:sku_123 1
    Redis-->>UserB: FAIL (Locked)
    
    UserA->>Shopify: Checkout Create
    Shopify-->>UserA: Order Success
    UserA->>Redis: DEL lock:sku_123
    
    UserB->>UI: Show "Out of Stock" Error

1. The Reservation System

When a user adds a high-heat item to cart, we reserve it in Redis for 10 minutes. SET reservation:sku_123:user_abc EX 600 This decrements the available stock immediately, even before the purchase happens. If they don’t buy in 10 minutes, the key expires, and the stock flows back to the pool.

Webhooks for Sync

When an order does happen, we need to tell Amazon and TikTok immediately. Shopify’s INVENTORY_LEVEL_UPDATE webhook is our trigger. We push this event to an Event Bus (Amazon EventBridge). Fan-out lambda functions update external marketplaces in parallel. (Latency: <200ms).

12. Handling The “Thundering Herd”

When 50,000 users hit the site at 10 AM, your database will die. Connecting to Postgres for every inventory check is suicide. Redis handles 100,000 ops/second. We use Read-Through Caching.

  1. App asks Redis: “Stock for SKU-123?”
  2. Redis says: “Miss”.
  3. App asks DB: “Stock is 100”.
  4. App tells Redis: “SET SKU-123 100 TTL 10sec”. This reduces DB load by 99%. But what if 1,000 requests miss at the exact same time? They all hit the DB. We use Probabilistic Early Expiration or Request Coalescing (SingleFlight) to ensure only ONE request hits the DB to warm the cache.

Why Maison Code Discusses This

At Maison Code, we engineer High-Heat Launch Systems. We have survived Black Friday traffic for top Streetwear brands. We know that “Overselling” is not an “Ops Probem”; it’s an “Architecture Failure”. We build Distributed Locking mechanisms that guarantee Strong Consistency even at massive scale. We value your Reputation. A cancelled order is a lost customer forever.

14. Conclusion

Inventory Sync is the hardest problem in E-commerce. It fights against Physics (Latency) and Probability (Race Conditions). You cannot win with “Simple Queries”. You need Caching, Locking, and Queuing. You need to design for failure.


Do you oversell during drops?

We implement Redis-based High-Performance Inventory Sync to guarantee 0% overselling.

Hire our Architects.