Invental/Review lessons/Background jobs when something fails

Background jobs when something fails

Finishing a leased job must check the lease

A job lease is only safe if finishing the job checks that you still hold it: otherwise the slow worker you already replaced wakes up and overwrites the new worker's result.

01 · Before and afterThe pattern

SQL
-- claim
UPDATE subs SET claimed_until = now() + interval '5 min'
WHERE id = $1 AND (claimed_until IS NULL OR claimed_until < now()) RETURNING claimed_until;
-- complete: fenced to *this* claim
UPDATE subs SET next_sync_at = $2, claimed_until = NULL
WHERE id = $1 AND claimed_until = $claim AND claimed_until > now();   -- before: WHERE id = $1

02 · Run it on your appThe check

When a background worker finishes a job, does its final write verify it still owns the job?

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.