Kinds of background work
Not all "later" work is alike. Naming the kind tells you the tool.
| Kind | Example | Trigger | Typical tool |
|---|---|---|---|
| Fire-and-forget task | Send a welcome email | An event or API call | Task queue (SQS, RabbitMQ, Redis-based queues) |
| Heavy processing | Transcode a video, generate a PDF | Upload completed | Queue + autoscaled workers |
| Scheduled (cron) | Nightly invoices, hourly clean-up | The clock | Scheduler + queue |
| Delayed | Remind the user in 24 h if the cart is still full | An event plus a delay | Delayed queue or scheduler |
| Long-running workflow | Order → pay → pack → ship → deliver, over days | An event; many steps | Workflow engine (Temporal, Step Functions) |
| Bulk / batch | Re-index 200M products | An operator | Batch framework, chunked jobs |
A restaurant kitchen is a job system. Orders go on the rail (the queue), cooks grab tickets (workers claim jobs), the head chef notices if a ticket has been sitting too long (lease timeout), and a burnt dish is remade rather than served (retry). Some dishes need many stations in order (a workflow).
The anatomy of a job system
Each job record needs, at minimum:
| Field | Why |
|---|---|
job_id |
Identity; also the idempotency key for side effects |
type, payload |
What to do, with what (keep payloads small; reference big data by ID) |
status |
queued → running → succeeded / failed / dead |
attempts, max_attempts |
Retry budget |
run_at |
Not before this time (delays, back-off) |
lease_until, worker_id |
Who holds it and until when |
priority, queue |
Isolation and ordering of urgency |
Claiming: two workers must not run the same job
When ten workers poll for work, two of them may grab the same job. Three common ways to claim safely:
- Broker visibility timeout (SQS, many queues): receiving a message hides it from others for N seconds. If the worker doesn't delete it in time, it reappears.
- Database row lock:
SELECT … FOR UPDATE SKIP LOCKED LIMIT 10lets many workers pull different rows from ajobstable without blocking each other. Simple and surprisingly scalable to thousands of jobs per second. - Partitioned ownership: each worker owns a set of partitions (Kafka consumer groups), so jobs for a key go to exactly one worker.
Leases and heartbeats
A claim is a lease, not ownership forever. The worker must finish, or extend the lease with heartbeats, before it expires. If it crashes, the lease lapses and another worker picks the job up.
A lease that expires while the original worker is merely slow (a long GC pause, a slow network call) means two workers now run the same job. Leases make this rare, not impossible. That's why every job must be idempotent, and why critical side effects should check a fencing token or a status guard ("only ship if status is still PACKED").
Idempotency: making "again" harmless
With retries, timeouts and redelivery, every job will eventually run twice. Design for it:
- Natural idempotency: "set status to SHIPPED" is safe to repeat; "add ₹100" is not.
- Idempotency keys: pass
job_idto the payment or email provider so it drops duplicates. - Dedup table: record
(job_id, step)when a side effect is done; check before doing it again, ideally in the same transaction as the effect. - Conditional writes:
UPDATE orders SET status='SHIPPED' WHERE id=? AND status='PACKED'; the second run changes nothing.