Core dataE-commerce

Flash Sales and Inventory

Sell exactly the stock you have when ten thousand shoppers click Buy in the same second, and keep the bots out.

stock decrement
Atomic
oversold units
0
holds that release themselves
10 min
Step 1 of 6
Load the stock

500 units go on sale.

Example sessionplaying
# Load the stock
hotkv> SET stock:sneaker-x 500
OK

An animated walkthrough with real HotKV commands and example replies. Click any step to jump to it.

The problem

In a flash sale the inventory database is the bottleneck exactly when it matters most. Read-then-write checks oversell under concurrency, abandoned carts lock stock forever, and bots buy the stock before real customers finish loading the page.

How HotKV handles it

Stock lives in a counter that is decremented atomically, so concurrent buyers can never take more than exists. Each reservation is a key with a ten-minute expiry, a rate limit per customer slows the bots, and confirmed orders go into a stream for fulfilment to process at its own pace.

Step by step

What happens, command by command

  1. 01

    Load the stock

    500 units go on sale.

    SET stock:sneaker-x 500
  2. 02

    Keep the bots in check

    Each customer may try twice per 10 seconds. Bots exceed it immediately.

    GCRA.LIMIT buy:customer:881 10000 2
  3. 03

    Take one unit

    DECR is atomic. 10,000 concurrent clicks can never take more than 500 units.

    DECR stock:sneaker-x
  4. 04

    Hold it for ten minutes

    The reservation expires on its own if checkout is abandoned, and a sweeper puts the unit back.

    SET hold:sneaker-x:order-7781 customer:42 NX EX 600
  5. 05

    Sold out is handled

    When DECR goes below zero, the unit is returned and the shopper sees sold out. Stock never goes negative for long.

    INCR stock:sneaker-x
  6. 06

    Hand orders to fulfilment

    Paid orders are appended to a stream. The warehouse system works through them at its own pace.

    XADD orders * order 7781 sku sneaker-x customer 42
Where it fits

Real-world scenarios

Sneaker and merchandise drops

Limited editions sell out in seconds without overselling a single pair.

Ticketing

Seats are held for a few minutes during checkout and released automatically if the buyer leaves.

Food and grocery delivery

Delivery slots are counters, decremented atomically as customers book them.

Built in

What you get

Atomic counters

DECR and INCR change stock in one step with no race.

DECRINCRBY

Expiring holds

Reservations are keys with a TTL.

SETNXEX

Bot protection

Per-customer limits with an exact retry time.

GCRA.LIMIT

Order hand-off

A stream for fulfilment, with consumer groups and acknowledgements.

XADDXREADGROUP

Common questions

How do expired holds return stock?

Expired holds can be noticed through keyspace notifications or a periodic sweep, which increments the stock counter again.

Can I check stock without taking it?

Yes. GET returns the current count. Only DECR reserves.

What about multi-item carts?

Use MULTI and EXEC, or a function, to decrement several counters together.

Related use cases

Try it on your own workload

HotKV speaks the Redis protocol, so you can point an existing client at it and run these same commands.