STAGE 07 / LESSON 03
Queues and idempotent jobs
A queue lets a request hand off work to a background worker.
THE IDEA
Understand it.
A queue can smooth spikes and keep slow work, such as generating a report, out of the user’s immediate request. Workers need visibility into failures, retries and jobs that cannot be processed.
Some queues can deliver a message more than once. Design a handler so repeating the same logical job is safe, for example by recording a unique operation key. External side effects may require additional coordination.
SEE IT IN CONTEXT
A report job
POST /reports → create report request → enqueue job
Worker → checks operation key → builds report
→ stores result → marks job complete
Retry → recognizes work already completedThis is a conceptual flow. Ensure the operation-key check and the relevant database changes are coordinated safely.
LEARN FROM REAL WEBSITES
See it in the real world.
These simplified structures come from public sources. Company systems change, and the sources may describe only one project or an earlier design.
Shopify
Shopify engineering, 2021Move work into the background
Shopify describes using background jobs and discusses safely staging jobs as part of a database transaction.
- Application change
- Staged job
- Background worker
What to learn: A queue moves slow work out of the immediate request, but the handoff and retries need careful design.
Read the source: Shopify high-availability background jobsStripe
Stripe engineering, 2017Retry safely with an idempotency key
Stripe explains how an API client can use an idempotency key to retry a supported request without creating a second operation.
- Request + key
- Uncertain response
- Retry with same key
What to learn: Idempotency makes a retry safer. This is a public API example, not evidence about Stripe's internal job queue.
Read the source: Stripe idempotencyTRY IT YOURSELF
Practice.
A message is delivered twice. Why can a worker not assume it will be processed only once?
Show one possible answer
At-least-once delivery systems can redeliver messages. The worker must tolerate duplicates and handle partial failures.
GO DEEPER