Invental/Review lessons/Background jobs when something fails

Background jobs when something fails

Release a claim when the work fails

A “claim” flag that guards a side effect must be released when the side effect fails; otherwise the retry that is meant to recover the work is the thing that skips it.

01 · Before and afterThe pattern

Claim, act, and roll the claim back on failure.

TypeScript
const claimed = await redis.hsetnx(key, "dispatched", 1);
if (!claimed) return "already queued";
try {
  await queue.publish(job, { dedupId: `order-${token}` });
} catch (err) {
  await redis.hdel(key, "dispatched"); // let the next retry claim it again
  throw err;                            // surface 500 so the sender retries
}

02 · Run it on your appThe check

If the call right after you set a “processing” or “done” marker fails, does a retry still do the work?

If the honest answer is no or not sure, this is worth a look before it reaches production.

Want this checked on your code?

Tell us what you are building and what worries you. We match a senior reviewer to your code and confirm scope before anything starts.

— Get in touch
hi@invental.co ↗
— Or
— What to include

A link or short description of the code or system, what you want checked, your stack, and when you need the answer. No repository access is needed until scope is agreed.

— Or leave a note
We reply within one business day.