Course map

Topic 121 / Core

Background job processing: delayed and scheduled jobs

Chapter 7.5 · Jobs, Workflows, Batch and Streams

What and why

Some work is too slow, or simply doesn't need to happen, inside the request path that triggered it — a request waiting on it pays latency for no user-visible benefit. Background job processing moves that work onto a queue (→ 106, 110) consumed by a pool of workers. A delayed job runs once, at or after a specific future time; a scheduled job runs repeatedly, on a fixed interval or cron-style schedule. Builds on: → 110 (queue-based load leveling — a job queue is this pattern applied to deferred work).

Diagram

queued ──► running ──► succeeded
              │
              └──► failed ──► retry ──► queued   (→ 152)
                           └──► dead-letter (→ 117)

delayed job:   visible to workers only after "run_at" has passed
scheduled job: re-enqueued on a fixed cadence by a scheduler

How it works

  1. The request handler enqueues a job description — what to do, with what input — instead of doing the work inline, and returns immediately.
  2. A pool of workers pulls jobs from the queue (competing consumers, → 107) and executes them, sized independently of the web tier.
  3. A delayed job carries a "not before" time; the queue, or a scheduler component, only makes it visible to workers once that time has passed — used for "send this reminder in 24 hours."
  4. A scheduled job is re-enqueued on a fixed cadence by a scheduler process — used for "run this report every night" — and must handle a missed run (scheduler was down) by catching up or explicitly skipping, never by silently losing the run.
  5. Failures follow the same retry-then-DLQ path as any consumer (→ 117); preventing a scheduled job from running twice when multiple scheduler replicas are live needs a lease or lock (→ 137), not just "whoever gets there first."

Practical example

(illustrative). A nightly billing-report job scheduled for 2 a.m. takes 40 minutes to run against 500,000 accounts. Running it inline in a web request isn't even possible — requests time out in seconds — but in the background it runs whenever its slot comes up, and its result lands in a notification or report table when done, without ever blocking the user-facing system.

When to use it

  • Use for anything that doesn't need to complete before the response is returned — sending email, generating a report, resizing an image, reconciling data.
  • Avoid for work the caller genuinely needs the result of before proceeding — that stays synchronous (→ 105) or becomes async request–reply.
  • Failure scenarios: a scheduled job running twice because two scheduler replicas both fired it with no mutual exclusion (→ 137); a delayed job that silently never fires because the "not before" check stopped running after a deploy.
  • Simpler alternative: for very low volume, a single cron entry calling a script directly is simpler than a job queue and worker pool — add the queue once volume or reliability demands it.

Practice

(pattern; 20–30 min, lab Kafka or a Postgres job table). Implement a job table with run_at and status; enqueue 5 delayed jobs with different future run_at times and a worker loop that only picks up jobs whose time has passed. Then run two worker instances with no locking and observe a job executed twice; fix it with a claim such as UPDATE ... SET status='running' WHERE status='queued' RETURNING *, which only one worker can win. Done when only jobs past their run_at execute, and the duplicate-run bug is both reproduced and fixed.

Shared lab setup

Key takeaway

Background jobs remove slow or deferrable work from the request path; delayed jobs wait for a time, scheduled jobs repeat on a cadence, and both need the same retry/DLQ and duplicate-run discipline as any consumer. Done when you can: design a job's state machine including retry and duplicate-run prevention. Used next in → 122 (workflow orchestration coordinates many such jobs into one process); Project 10, Project 11.

References

None — this topic makes no external claims.

Source directory

Search the guide