LESSON 09 · Asynchronous work

Move slow work off the request

A queue lets web requests and background workers move at different speeds.

SYSTEM VIEW

The architecture

Diagram view
Uploads—
Workers busy—
Backlog—

Watch a photo upload stop waiting on its filters, then see whether workers keep up with the queue.

  1. A user uploads a photo. The web server applies the filters inline before it answers, so the request waits the full eight seconds.

    What changed

    • POST /photos
    • Filters run inside the request
    • Answer after 8 s
  2. Now the web server publishes a job to a message queue and responds without doing the processing inline. The request finishes sooner, and the user sees the photo as pending.

    What changed

    • Message queue
    • Job published
    • Filters move out of the request
  3. A worker, a separate process, pulls the job from the queue, applies the filters and saves the result. Then it acknowledges the job.

    What changed

    • Worker
    • Result saved
    • Job acknowledged
  4. Many queues deliver at least once. If a worker crashes before acknowledging a job, the queue hands it out again, so handlers must be idempotent: a repeat does no extra harm.

    What changed

    • Second worker
    • Worker 1 crashes before its ack
    • Job delivered again
  5. If 20 uploads arrive each second and each takes 8 seconds of processing, about 160 workers are busy at once (20 × 8). Together they finish 20 jobs a second, as fast as jobs arrive.

    What changed

    • 20 uploads a second
    • 160 workers busy (20 × 8)
    • Jobs finish as fast as they arrive
  6. A 50/s burst outpaces 160 workers by 30 a second: 18,000 jobs wait after ten minutes. Unhealthy workers grow a backlog too. No job is lost and the web tier stays responsive.

    What changed

    • Backlog and job age grow
    • Web server still responsive
    Cause
  7. Check the cause, then add or repair workers. The backlog drains while the web tier stays unchanged: the queue decouples the producer from the workers, so each scales on its own.

    What changed

    • More workers
    • Backlog drains
    • Web server unchanged

Space plays · ← → step · Home resets

THE IDEA

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.

DECISION POINT

Uploading a photo takes eight seconds because the request waits for filters to finish.

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Queues buffer asynchronous work and decouple producers from consumers.
  • Backlog and job age reveal whether worker capacity is keeping up.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

Generating a PDF invoice makes checkout requests wait six seconds. Which design fits?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.