What happens to a stock update when a store is offline?
A delivery to a store can't simply be sent and forgotten: the store may be unreachable, and a message may get lost on the way back.
Guided demonstration with made-up data, simplified from the production logic.
Store C goes offline
Store C's connection drops. Its shelf holds 3 units of Product A, and it keeps selling on its own.
Head office
Nothing to send yet
Store C
3of 12
Explore the technical mechanism
When a top-up is completed at the office, the units still have to reach the store's database, and the store may be unreachable. Writing to the store inside the request would lose the change whenever the store was down.
So completing a top-up writes the stock changes and a delivery job into the office database in a single transaction: a completed list can never exist without its delivery job. A background worker drains each store's jobs in order. If a store is unreachable, the job stays pending, later jobs for that store wait behind it, and retries back off exponentially.
The changes travel as deltas, "+4 to position P-12", not as final quantities. A delta stays correct however long the store was offline and however much it sold in the meantime; a final quantity computed at the office would overwrite those sales.
The hard case is a lost acknowledgement: the store committed the change, but the office never heard back and retries. The store applies each job inside one store transaction that also records a marker keyed by the job's id. A retry that finds the marker settles the office side from the recorded plan and leaves stock alone. Delivery is at-least-once; the stock effect is exactly once, because applying a job is idempotent. There is no distributed transaction across the two databases.
Interactive technical demonstration. Synthetic and simplified; not the production system or its data.
Empty. Complete a top-up to queue a delivery.
- 5
- 2
- 3
None yet
Try this: complete a top-up while the store is offline, run the worker, bring the store back with a lost acknowledgement, then run the worker twice.
0
Most times any one delivery changed store stock. 0 jobs pending.
- Lock the office delta rows for the job. Edits to a pending delivery take the same locks in the same order, so the two can't interleave.
- Open a store transaction and look for the job's marker.
- Marker found: the store already committed. Settle the office from the plan stored in the marker and stop.
- Otherwise lock the live store positions and plan the placement against them.
- Update each position, checking that exactly one row changed.
- Insert the marker in the same store transaction, then commit.
- Settle the office rows and commit. A crash between the two commits resolves correctly on the next retry.
- Units that can't be placed are recorded as issues for follow-up, never dropped silently.
