Reliable HTTP cron and delayed-job scheduling with automatic retries and full run logs — delay.run

"Send this in 15 minutes" looks like a small feature: stash a payload, sleep, send. In a production web app it has to survive the deploy that happens at minute 8, the retry when the provider 500s at minute 15, the user who clicks Undo at minute 3, and the worker that was mid-job when you scaled it to zero. Cron will not save you: cron is "at this clock time, run this command", not "this particular message, once, at a time that depends on a user action".

Here is the checklist I want filled in before the first delayed job leaves a real user's account.

Before the first delayed job is queued

1. Store the job somewhere that outlives the process. Not setTimeout, not an in-memory list, not a Python BackgroundTasks that dies with the gunicorn worker. A row in Postgres, a Redis sorted set with persistence you actually have on, or a queue service. Reboot the box in staging and confirm the job is still there.

2. Give every job an idempotency key, and a unique constraint that uses it. "Send invoice 9, delayed 15 minutes" must not send twice if the worker retries, if the user clicks twice, or if two workers dequeue the same row. The key belongs to the business event, not to the HTTP request that queued it.

3. Decide, per job type, whether the user can cancel, and what cancel means. Undo-send, "don't remind me after all", "the appointment was moved". Cancellation has to be a state the worker reads at run time, not only a row you delete if you get there first. A job already in flight needs a rule too (best-effort abort, or "it may still arrive").

4. Put a deadline on the work, not only a start time. "Run at t+15 minutes" is not "run whenever a worker becomes free in the following six hours". A payment capture that is three hours late is a different operation, or it is too late. Store run_at and expire_at. Expired jobs should go somewhere visible, not just disappear.

5. Keep the payload small and durable. Store an invoice_id, not a 200 KB HTML body that will be stale by the time you send it. Fetch the fresh state when the job runs, and decide what to do if the invoice has since been paid, deleted or voided. Sign or encrypt anything sensitive; a job table is a database, and it gets copied.

6. Write the failure policy before you write the worker. How many retries, what backoff, which errors are retryable (a 429, a 500, a timeout) and which are not (a 400, a 404, a revoked permission). Where a exhausted job goes (dead-letter, a support task, an email to a human). A delay queue with "retry forever" will surprise you a week later.

When you hook it into the app

7. Enqueue in the same transaction as the state change that requires it. If "mark as pending" commits and "queue the reminder" fails, you will have a pending invoice and no reminder. If it happens the other way around, you will have a reminder for a row that does not exist. A transactional outbox, or a single Postgres COMMIT that includes the job row, is the boring fix.

8. Make the worker a separate process you can deploy, pause and scale. Do not run the delay consumer inside the web request process if you can help it. You want to be able to deploy the web app without interrupting a send, and to stop sending without taking the website down.

A delayed job that only lives in the memory of the process that accepted the request is already gone the next time you deploy. delay.run is built for HTTP jobs that have to fire later, with retries and a run log, so "in 15 minutes" is a stored execution, not a timer in a worker that may not be there in 15 minutes.

9. Log a correlation id from the request, through the row, to the send. When a user says "I got this twice" or "I never got it", you need to find the job, the attempts, and the provider response without grepping for their email and hoping.

10. Test the ugly paths in CI, not only "it sent on my laptop". Restart the worker between enqueue and run_at. Cancel before run_at. Let a retryable error happen and assert a single logical send. Let the document be deleted before run time and assert no send. Deploy mid-flight in staging.

Once it is in production

11. Alert on the age of due jobs, not only on worker liveness. A worker process can be up and not be draining the delay set. If run_at has passed and the job is still queued, that is the incident. Also alert if the dead-letter grows.

12. Have a rehearsed way to pause new starts without dropping what is stored. A flag, a queue pause, a SIGTERM that finishes the current send and does not pick up more. You will need it for a provider outage, a bad template, or a legal request. Confirm that unpausing does not dump six hours of "immediately due" work onto a rate-limited API in ten seconds (a cap, or a human-controlled drain, belongs in the runbook).

Things people skip that then dominate the first incident

  • Clocks. run_at in UTC, user-facing times with an IANA zone stored next to them. "In 15 minutes" is easy. "Tomorrow at 09:00 in the user's timezone, on the day DST ends" is why you do not store only a Unix timestamp and a hope.
  • Poison payloads. A job whose body cannot be parsed will crash the worker in a loop if you reserve, throw, and retry the same row. One bad row should not stall the queue.
  • Multi-tenant fairness. One customer who queues 100,000 "send later" items should not push everyone else's password-reset delays by an hour. A per-tenant cap, or a fair scheduler, is a product decision you want to make early.

If you can only do three of these on the first pass, do 1, 2 and 7: durable storage, an idempotency key, and enqueue with the state change. Most of the painful bugs I have seen started with one of those three missing.

Related reading

When you first added "do this later" to an app, what broke — a deploy in the middle, a double send, or a cancel that did not cancel? What would you insist on now?