Queues and background work
Take slow, bursty or unreliable work off the request path.
15 min readDo less while the user waits
Some work is slow (resizing images), bursty (sending a newsletter) or depends on unreliable third parties (SMS providers). Doing it inside the request makes users wait and lets one slow dependency take down your API.
Producers, queues and consumers
Instead, the API writes a job to a durable queue and returns immediately. Workers pull jobs from the queue at their own pace. The queue absorbs bursts, and you scale workers independently of the API.
- The producer only waits for the queue to acknowledge the job
- Workers can be added when the backlog grows
- If workers crash, jobs stay in the queue
Retries, idempotency and dead letters
Jobs fail, so workers retry. Retrying means a job may run twice, so make handlers idempotent: processing the same job again has no extra effect. After a few failed attempts, move the job to a dead-letter queue so one poison message cannot block the rest.
Key takeaways
- A queue turns a slow synchronous call into a fast acknowledgement
- Make every consumer idempotent because retries cause duplicates
- Use a dead-letter queue to park messages that keep failing
Sign in to track lessons and earn badges.