Invental/Review lessons/Background jobs when something fails

Background jobs when something fails

Never advance a cursor past pending work

One “read up to N” number cannot describe a channel where some records are handled immediately and others wait: the cursor must never pass the oldest record that still owes a delivery.

01 · Before and afterThe pattern

TypeScript
// before: commit the highest sequence any handler touched
cursor = Math.max(cursor, record.seq);
// after: commit only up to the oldest record still waiting for a consumer
pending.add(record.seq);            // when buffered
pending.delete(record.seq);         // when consumed
const safe = pending.size ? Math.min(...pending) - 1 : highestSeen;
saveCursor(safe);

02 · Run it on your appThe check

If your worker crashes right now, can a message that arrived but was not yet processed be skipped on restart?

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.