Backend / Background jobs and workers

Background jobs and workers

A sign-up that takes 8 seconds becomes one that answers in a tenth of a second: the slow parts move to a queue, workers run them, retry them and report back.

Beginner7 steps~6 min
Browser — sends the request and waits
Web server — answers requests, adds jobs
Job — a note describing work to do
Worker — takes jobs from the queue and runs them
// THE SHORT VERSION

In 30 seconds

Some work is too slow to do while a user waits for a page: sending email, resizing photos. A web server can instead write that work down as a job, put it in a queue and answer at once.

Separate programs called workers take jobs from the queue and run them. If a job fails, it goes back into the queue and is tried again after a pause.

Because the request is already over, each job keeps a status that the page can ask for later, or the server sends a message when it is done.

Background jobs and workers, step by step

STEPS01 / 07

Timings are invented round numbers. Real queues differ in detail: some do not keep strict order, and most promise each job runs at least once, not exactly once.

STEP 01 / 07 One slow request
THE MATH
save 0.1 s resize 5 s email 3 s wait = 8.1 s
  1. Step 1: One slow request. Ana signs up and uploads a photo. The web server saves her account, resizes the photo and sends a welcome email, all before it answers. Her browser shows a spinner for 8.1 seconds, and while this request is busy the server has less room for everyone else.
  2. Step 2: Hand the slow work over. Only the saving has to happen before Ana sees the next page. So the server saves her account, writes down the two slow tasks as jobs in a queue, and answers straight away. The work has not been done yet; it has been promised.
  3. Step 3: The job queue. A job is a short note that says what to do and with which data; it is not the work itself. The queue keeps the notes in order, oldest first, in a store that survives a restart, such as a database table or Redis. The web server only ever adds to it.
  4. Step 4: Workers do the work. Workers are separate programs with one loop: take the next job from the queue, run it, repeat. Two workers handle the resize and the email side by side, so both are finished after 5 seconds, and no browser was kept waiting. If the queue grows, start more workers.
  5. Step 5: Failures and retries. The mail service is down, so the email job fails. Instead of being lost it goes back into the queue and is tried again after a pause that grows each time. After a fixed number of tries it is set aside as failed for a person to check. Because a job can run more than once, it must be safe to repeat.
  6. Step 6: Reporting status back. The request ended long ago, so the result needs another way back. Each job keeps a status that the worker updates as it goes. The page can ask for it every few seconds, which is called polling, or the server can push a message when the job is done. For the email, the email itself is the report.
  7. Step 7: What makes a good job. The same pattern fits any work that is slow, may fail for outside reasons, and is not needed for the very next page: sending email, resizing images, encoding video, building reports, importing large files, nightly clean-ups. Anything the user must see immediately stays in the request.
VALUES SO FAR
resize5 sslow
email3 sslow
wait8.1 sbefore
wait····after
queue····waiting
workers····parallel
retry····backoff
status····report
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

// AFTER

Learn next

Nothing builds on this one yet. See every backend concept.

Spotted a mistake on this page?