AETERNAE AI RESEARCH LLC INDEPENDENT RESEARCH
ÆAETERNAERESEARCH
Sign inRequest access
← Malware catalog

Attributed metadata history

REPORTPUBLIC METADATA

Two customers, one item left: preventing overselling with one PostgreSQL UPDATE

Two customers. One kurta left. Both press Place order within the same second. In many stores, both orders go through. Somebody finds out later, usually the shop owner, on the phone, apologising. I'm building ShopKit PK, a platform that runs many online shops for Pakistani sellers from one codebase. Most of those sellers ship cash on delivery, where every wrong order becomes a refused parcel. So "never sell what you don't have" was a hard requirement, not a nice-to-have. Here's how it works, and the mistakes it avoids. The bug everyone writes first The obvious version reads stock, checks it, then writes: const level = await db.inventoryLevel.findUnique({ where: { variantId } }); if (level.onHand - level.reserved < qty) throw new OutOfStock(); await db.inventoryLevel.update({ where: { variantId }, data: { reserved: { increment: qty } }, }); Between the findUnique and the update, another request can do exactly the same check, see the same stock, and also pass. Two orders, one item. You can patch this with SELECT ... FOR UPDATE and a transaction, but there's a simpler way that needs no explicit lock at all. Make the check and the write one statement Stock in ShopKit is two numbers per variant: on_hand and reserved. What a customer can buy is on_hand - reserved. A database check constraint keeps 0 ≤ reserved ≤ on_hand. Reserving stock is a single conditional update: UPDATE inventory_levels SET reserved = reserved + $q WHERE store_id = $s AND variant_id = $v AND track_inventory AND on_hand - reserved >= $q RETURNING on_hand, reserved; If enough stock is left, one row comes back and the reservation is made. If not, zero rows come back and the line is out of stock. PostgreSQL takes a row lock for the duration of the update, so when two requests race, the second one re-checks the condition against the first one's result. Exactly one wins. Note the store_id in the WHERE. Every store-owned table in ShopKit carries it (49 tables), and CI fails if a new table forgets. A bug in one shop can't touch another shop's stock. Multi-item orders: avoid the deadlock An order with several lines reserves several rows. If order A reserves shirt → trousers and order B reserves trousers → shirt at the same moment, each can end up holding one lock and waiting for the other. That's a deadlock. The fix is boring and reliable: always reserve lines in the same order, sorted by variant_id. Then two orders always queue in the same direction and can't wait on each other. When to reserve: not in the cart Reserving at add-to-cart sounds safer but isn't. Carts get abandoned, and stock gets locked by people who will never buy. ShopKit reserves when the order is placed: Cash on delivery: the reservation has no expiry. The order is real. Online payment: the reservation expires at the payment deadline (a store setting). Don't let correctness depend on a cron job Expired reservations have to be released. The tempting design is a cron job that sweeps them every few minutes. But on free hosting tiers cron is unreliable, and if it's late, items look sold out when they aren't. So expiry is handled lazily: when a reservation for a variant fails, the code first releases that variant's expired reservations and tries again. The cron job still runs, but only to tidy up. If it never runs, stock is still correct. If a late payment webhook arrives after a reservation expired, the handler tries to reserve again. If the stock is still there, the order goes through; if not, the order is flagged instead of silently overselling. Double-clicks and flaky mobile networks Overselling's cousin is the duplicate order: a customer on a weak connection taps the button three times. Every checkout request carries an idempotency key. Inside the transaction: await raw.lockKey(tx, `order-idem:${storeId}:${input.idempotencyKey}`); const existing =

Reference details

Source category
research-report
First reported
Nov 26, 2026 17:42 UTC
Last reported
Nov 26, 2026 17:42 UTC
Catalog updated
Nov 26, 2026 17:53 UTC
Record ID
850eabe340b59dc46a53d38b0dea9e0e117cff77bb79f4b6fc39871da08820b3

Published indicators

Network indicators are visibly defanged. This page does not make them clickable or send them to another service.

No validated file hashes or network indicators are stored for this record.

Source observations

intelligence_src_6dc544294b7f21f98a

Source-provided label
Publisher RSS metadata
Evidence class
mentioned in report
Confidence
Not reported
Observed
Nov 26, 2026 17:42 UTC
Retrieved
Nov 26, 2026 17:53 UTC

This record contains metadata and attribution only. It does not host, download, unpack, render, or execute malware samples.

Feed collection status

Sources represented on this page. Publication and observation dates describe the content; successful batch dates describe local collection. A recent batch does not certify complete coverage or current provider availability.

1 sources: 0 stale, 0 needing attention, 0 paused, 0 unverified.

Review source update dates and status
  • intelligence_src_6dc544294b7f21f98aRecent successful batchLast successful batch: Last attempt:

Stale means older than twice the recorded collection interval, with a minimum of one hour; without an interval, the threshold is 24 hours. Cached content remains available while a feed is stale, paused, or unverified.