Queues

Handing off work to be done later, without waiting for it.

What is it?

Some tasks don't need to happen while the user waits: sending a confirmation email, resizing an uploaded image, generating a report. Doing them during the request makes users wait longer than needed.

With a message queue, the server drops a description of the task into a queue and responds right away. Separate worker processes pick up tasks from the queue and handle them in the background.

Explain like I'm 10

It's like dropping a form in an office in-tray instead of waiting at the counter while it's processed. You get on with your day; a clerk works through the tray in order, at their own pace.

Examples

Conceptual producer/consumer with a queue

// Server (producer) — respond fast, queue the slow work
app.post("/signup", async (req, res) => {
  await createUser(req.body);
  await queue.push({ type: "welcome-email", userId: req.body.id });
  res.send("Signed up!"); // doesn't wait for the email to send
});

// Worker (consumer) — runs separately, processes queued jobs
queue.onMessage(async (job) => {
  if (job.type === "welcome-email") {
    await sendWelcomeEmail(job.userId);
  }
});

How it works

A producer (like a web server) adds messages describing work to the queue. One or more consumers (worker processes) keep reading the queue and processing messages.

A message is often removed only after it's handled successfully, so if a worker crashes mid-task, the message isn't lost and can be retried.

Producer → [ queue: job1, job2, job3 ] → Consumer(s) process jobs

Why does it exist?

Queues separate "accepting a request" from "doing the (possibly slow) work". That keeps responses fast, smooths out traffic spikes, and makes it easy to retry failed work without touching the original request.

When to use it

Use a queue when a request triggers work that doesn't need to finish before you respond (sending an email, processing an upload, generating a report), especially work that's slow or sometimes fails and needs retrying.

When not to use it

Don't queue work whose result the user is waiting to see right now. That just adds a hop and delay. Queues also add operational work (workers to run, failures to monitor), so wait until you have slow or bursty background work to offload.

Common mistakes

  • Queuing time-sensitive work when the user needs to see the result immediately.

  • Not handling failures. If a worker crashes mid-task, the message should be retried, not silently lost.

  • Letting the queue grow without limit and without enough workers to keep up, so delays pile up.

Practice exercises

  1. Easy:

    List three real tasks in a typical web app that are good candidates to run through a queue instead of immediately.

  2. Medium:

    Explain in your own words why queues help a system handle sudden traffic spikes.

  3. Hard:

    Describe how you'd handle a job that fails repeatedly (e.g. a broken email address) so it isn't retried forever.

Interview questions

What problem do message queues solve?

They let a system hand off slow or non-urgent work for later, keeping the original request fast and smoothing out traffic spikes.

What are 'producers' and 'consumers' in a queue system?

Producers add messages (jobs) to the queue. Consumers (workers) read and process them, usually independently and in parallel.

Why are queues useful for reliability, not just speed?

If a worker fails partway through, the message can be safely retried instead of the work being lost.