Our sales orders go into a client's Tally as invoices through a small desktop connector on their PC. The connector asks our API every 150 seconds for orders that aren't in Tally yet, and a queue worker writes them in. "Not in Tally yet" means the order has no Tally id stamped on it, and that id is only stamped once Tally answers.

On one client, Tally took 8 to 10 minutes per push: a big company, a slow PC, a Cloudflare tunnel in between. So for those ten minutes the order still had no Tally id, every 150-second poll queued it again, and the worker wrote it again as soon as the first job finished. One order became two invoices in their books. In accounting software a duplicate isn't a display bug. It's revenue counted twice until someone finds it and reverses it by hand.

The obvious fix

The order already carries its own id into the job. So before adding a push job, look at the jobs that are still waiting or running for that store, collect the order ids inside them, and drop those from the new push. If every order is already on its way, return the existing job instead of adding one.

const pending = await queue.getJobs(['active', 'wait', 'delayed']);
const queued = new Set(pending.flatMap((job) => job.data.orders.map((o) => o.syncId)));
const orders = incoming.filter((o) => !queued.has(o.syncId));
if (!orders.length) return pending[0].id;
return queue.add('push-voucher', { ...data, orders });

Unit tests passed. A poll every 150 seconds can't collide with itself, and that was the bug we'd seen.

The test that broke it

The same week we let a chat assistant push orders on request, so now a human tap and the connector's poll can arrive together. I ran the real thing locally against a test Tally company: two pushes of the same order, fired with Promise.all.

Two jobs. Both calls read the queue, both saw nothing pending, both added. A check followed by a write is not a dedupe once two callers can sit between the check and the write, and with several API pods behind a load balancer that window is real.

The second job then ran and Tally refused it: "rejected 1 record(s) without an explicit error". That test company numbers vouchers manually and won't take a second invoice with the same number. The client from the first incident uses automatic numbering, where Tally replaces our number with its own and happily takes the duplicate.

The same race gives a clean test on one company and a double-booked sale on another. If I'd only tested against the first, the fix would have looked finished.

The fix that holds

The check and the add have to be one step per store. A Redis SET key token NX PX 10000 around them does that, and it works across every pod because they share the Redis:

const lockKey = `tally-push-enqueue:${storeId}`;
const token = `${process.pid}:${Date.now()}:${Math.random()}`;
while (!(await redis.set(lockKey, token, 'PX', 10000, 'NX'))) await sleep(50);
try {
  // the same check-then-add as before, now alone
} finally {
  if ((await redis.get(lockKey)) === token) await redis.del(lockKey);
}

The token makes sure a caller only releases its own lock, so one that outlived the 10-second expiry can't release a lock someone else now holds. The rerun: two simultaneous pushes, both calls returned the same job id, one invoice in Tally.

The unit test now stubs Redis with a yield on every call, so both callers actually interleave the way real network round trips do. Without the lock it fails, and with the lock it passes.

What I'd reuse

  • "Look, then add" is only a dedupe if nothing can happen in between. Make it atomic: a lock per key, a unique job id, or a database constraint. Pick whichever your queue can enforce.
  • Test a dedupe with the collision it exists to stop. Two calls at the same instant, not one after the other. The sequential test passed from day one.
  • Your test environment can hide the bug for you. Here a voucher-numbering setting decided whether the race showed up as a refusal or as double-counted revenue.
  • A slow downstream turns a "rare" race into a common one. Ten minutes per push meant four polls per job. The slower the system you write into, the wider the window your dedupe has to cover.