Queue
Also called: message queue, job queue, work queue, task queue.
A list of jobs that producers add to and workers take from, so slow work happens in the background instead of inside the request. Each message goes to one worker, and a message that isn't confirmed in time goes back on the queue for another try.
The web server answers each upload at once with 202 Accepted. A worker resizes one photo every 2 seconds and deletes the job only when it's done. If the worker crashes first, the job comes back after the visibility timeout (5 seconds here).
Waiting: 0 · In flight: 0 · Done: 0
No jobs yet. Worker 1 is waiting for work.
Say it in a prompt
Move image resizing out of the upload request into an SQS queue. The API saves the file, enqueues {imageId} and returns 202 right away. Run 4 workers with a 60-second visibility timeout, and delete a message only after the resize succeeds. After 5 failed receives, move it to a dead-letter queue. Vague vs precise prompt
Vague prompt
uploading a photo takes forever, make it faster Typical resultSwaps in a faster image library but still resizes inside the request, so a big upload still times out after 30 seconds.
Precise prompt
Put image resizing on an SQS queue: the API stores the file, enqueues {imageId} and returns 202. Run 4 workers with a 60s visibility timeout; delete the message only after success; move it to a DLQ after 5 receives. Typical resultThe upload answers in well under a second, 4 workers resize in the background, and a crashed worker's job reappears for another worker after 60 seconds.
Seen on
- Amazon SQS: A received message stays hidden from other consumers for the visibility timeout (30 seconds by default); if it isn't deleted in time, it becomes visible again for another try.
- RabbitMQ: Its work queues tutorial hands each task to the next worker in turn, and redelivers a task to another worker if the first one dies before acknowledging it.
You might describe it as
- do the slow part in the background
- a to-do list that the workers pick jobs from
- don't make the user wait for the email to send
Not to be confused with
- Pub/sub
In a queue each message goes to one worker; in pub/sub every subscriber gets its own copy.
- Dead-letter queue
The queue holds work waiting to be done; the dead-letter queue holds the messages that kept failing.