An inventory integration needs a rule for messages that arrive in a different order from the updates they represent. If revision 12 has already been accepted, a late delivery of revision 11 should not replace it merely because it arrived last.
This runnable teaching example from YX Fulfillment models that decision for one authoritative inventory stream. It is not a description of the company's production system or a claim about any particular platform's API.
State the revision contract before writing the comparison
In this example, each warehouse-and-SKU pair has a monotonically increasing revision assigned by its authoritative source. The quantity is an already defined available-to-sell value from that source. The example does not calculate it from physical stock or reservations.
Those are input contracts. If the source does not provide an ordered revision with this scope, adding a local arrival counter will not create one. Two independent systems with unrelated counters also cannot be combined by simply comparing their integers.
Test older, repeated and conflicting snapshots
Save the following as inventory-revision-demo.cjs and run node inventory-revision-demo.cjs. It uses the Node.js strict assertion API to check the outcomes.
const assert = require('node:assert/strict'); const latest = new Map(); function acceptSnapshot(input) { const { warehouseId, sku, revision, available } = input; for (const value of [warehouseId, sku]) { assert.ok(typeof value === 'string' && value.trim().length > 0); } assert.ok(Number.isSafeInteger(revision) && revision >= 0); assert.ok(Number.isSafeInteger(available) && available >= 0); const key = JSON.stringify([warehouseId, sku]); const current = latest.get(key); if (current && revision < current.revision) return 'stale'; if (current && revision === current.revision) { assert.equal(available, current.available, 'Conflicting same revision'); return 'duplicate'; } latest.set(key, Object.freeze({ warehouseId, sku, revision, available })); return 'accepted'; } const newer = { warehouseId: 'warehouse-example', sku: 'MUG-WHITE', revision: 12, available: 7 }; assert.equal(acceptSnapshot(newer), 'accepted'); assert.equal(acceptSnapshot({ ...newer, revision: 11, available: 9 }), 'stale'); assert.equal(acceptSnapshot(newer), 'duplicate'); assert.throws(() => acceptSnapshot({ ...newer, available: 8 })); assert.equal(latest.get(JSON.stringify([newer.warehouseId, newer.sku])).available, 7); assert.equal(acceptSnapshot({ ...newer, revision: 13, available: 0 }), 'accepted'); assert.equal(acceptSnapshot({ ...newer, warehouseId: 'other-warehouse' }), 'accepted'); assert.throws(() => acceptSnapshot({ ...newer, revision: 14, available: -1 })); assert.equal(latest.get(JSON.stringify([newer.warehouseId, newer.sku])).available, 0); console.log('Stale, duplicate, conflicting and independently scoped updates checked.'); The same-revision conflict deserves its own outcome. Silently treating different quantities as interchangeable would conceal an inconsistent source record. The test also accepts zero as a valid quantity and keeps another warehouse's stream separate.
Persist the comparison at the write boundary
The map makes the decision visible in a small process. It does not persist state, coordinate workers, authenticate a source or update a store.
In a durable implementation, make the conditional revision comparison and state write atomic. Reading the revision and later performing an unconditional write permits two workers to undo the intended ordering rule. A restart must also recover the last accepted revision, not just the quantity.
If a separate worker sends accepted quantities to a storefront, test that boundary too. A correctly ordered local record does not establish that outbound updates will be applied in the same order. Check the destination's actual update contract and retain a record of what it accepted.
For a warehouse connection, agree which system owns the quantity, what its revision means, what resets or migrations do to that sequence and how conflicts reach an operator. Then test a newer snapshot arriving first, a delayed older one, a repeated one and a same-revision conflict. The acceptance result should explain why the stored quantity is still the right one.
Comparing inventory records at a workstation. Illustrative scene.


