How it works
For a slow task such as image processing, the web server publishes a job to a message queue and responds without doing the processing inline. Workers, separate processes that pull jobs, consume them and save results. A suitable durable queue, one that stores jobs on disk or across machines, can retain pending work while a worker is unavailable. Amazon SQS and RabbitMQ are common choices.
Size workers with simple arithmetic. If 20 uploads arrive per second and each takes 8 seconds of processing, you need about 160 workers busy at once (20 × 8). Now suppose a burst brings 50 uploads per second for ten minutes. Those 160 workers still finish 20 per second, so 30 per second pile up: a backlog of 18,000 jobs. People wait longer, but the work is not lost and the web tier stays responsive.
A growing backlog means jobs arrive faster than workers finish them, or workers are unhealthy. Add or repair workers after checking the cause. When the queue stays mostly empty, fewer workers may be enough. The producer and workers can scale independently.
Queues have costs. Many, including standard SQS, deliver at least once, so a worker that crashes before acknowledging a job means it runs again; handlers must be idempotent, meaning a repeat does no extra harm. A job that keeps failing needs a dead-letter queue, a place to set it aside. Do not use a queue when the user needs the answer to continue, as with a login or a card authorization.
Uploading a photo takes eight seconds because the request waits for filters to finish.
Accept the upload, publish a processing job, and let workers apply filters asynchronously. The request can finish sooner, but the UI must show that the processed photo is pending and the queue needs retries and monitoring.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Queues buffer asynchronous work and decouple producers from consumers.
- Backlog and job age reveal whether worker capacity is keeping up.