Inventory System 00:00:01 Two mugs on the screen and one real shelf A pottery shop's product page says two mugs remain. Its display can lag the stock ledger. An illustrative inventory service must decide what is sellable when another shopper is already checking out. The product page shows the number in Redis. It may be five seconds old. The shopper wants two mugs. The request carries a key, so a retry is safe. Increase held stock by two only if the same authoritative update sees at least two available units. A hold line with an end time joins the same transaction. So does the movement. Confirmation transfers this valid hold into allocated inventory. The count and the matching movement must agree. The stock decision and its hold record share a transaction. A confirmed allocation and a temporary hold need different states because cancellation, expiry and payment can happen at different times. 00:00:57 A timer reaching zero does not move any mugs The shopper closes the page and the hold deadline passes. Look closely at the instant after expiry. A timestamp makes the hold eligible for cleanup; it does not execute the database change on its own. Two mugs are held and the shopper closed the page. Nothing happens by itself at that second. The line still says HELD. The cleanup worker can claim eligible expired rows without duplicating another worker's in-progress cleanup. HELD becomes EXPIRED. A confirm arriving now finds no HELD line. Held falls to 0 in the same transaction. A cleanup worker transitions only an eligible held record and returns the reserved quantity in the same transaction. A late confirmation must then discover that its former hold is no longer available. 00:01:46 A delivery has a hundred and fifty lines A lorry arrives with a mixed shipment. Replaying its scan must not increase the warehouse count twice. Identify this delivery, validate the batch and separate unfamiliar product lines from accepted movements. The app sends 150 lines and one key for the delivery. COPY puts the lines into a staging table in one go. Apply the recognized lines and their movement history consistently under the chosen import policy. The warehouse learns at once which two lines need a person. A relay reads the new movements and sends them to Kafka. If Kafka is down they wait. The example recognizes a hundred forty-eight lines and reports the other two for review. Downstream notifications can wait behind a publication record, while the authoritative inventory movements remain available for audit. 00:02:35 The bowl lives on a different shard A basket contains two mugs and one bowl. The mug shard can hold its share, but the bowl shard has no stock. Is a successful first reservation already a fulfilled basket? Two mugs and one bowl. Shard 2 holds the mugs under hold id 789. Shard 5 is asked for a bowl, with the same id. No stock row satisfies the requested bowl quantity on the second shard. The service gives the mugs back. Had the service stopped before the release, the mugs' own end time would free them. Release the partial mug hold or allow its bounded expiry to compensate after a failure. Give all attempts the basket's stable identity so repeated compensation cannot add extra mugs to inventory. 00:03:31 A bank approval arrives after the shelf reopens The bank asks the shopper for an additional verification code. By the time approval arrives, the stock hold has expired. Payment authorization and stock ownership now disagree. The bank asks the shopper for a code. Two mugs are held. The worker expires it at minute 15. The bank says yes one minute later. No line is HELD, so nothing changes. Eight are free, so a new hold works and is confirmed. Never resurrect an expired reservation unconditionally. Attempt a new authoritative hold under the current stock state, or follow the documented cancellation and payment policy. The shop can now distinguish a cached count, a live hold and a sale.