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.
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
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 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Spotted a mistake on this page?