Do it later
After this: Say why a sign-up answers in 55 ms with a queue and 2,050 ms without one.
On this page · 7 sections · 3 checks
You sign up for a new website. You type your name and a password and press the button. A moment later you see "Welcome", and a minute after that an email arrives: "Thanks for joining". The website did two pieces of work for you. Only one of them had to happen before you saw the page.
This tutorial is about the other kind of work: the work that can wait. You will put it to one side, let the user go on, and do it a little later.
Two pieces of work
When a new user signs up, the app server does two things:
- It saves the new account in the database. This takes 50 ms (milliseconds, thousandths of a second).
- It sends a welcome email through an email service, a separate system that delivers mail. This takes 2,000 ms, because the email service is far away and slow to answer.
The user needs the first one: until the account is saved, there is no account to welcome them to. The user does not need the second one before the next page. They will read the email later, if at all.
These numbers are round teaching numbers. Real ones vary a lot from system to system. What matters is their size: the email takes forty times as long as the save.
Doing it all now
If the server does both before it answers, the user waits for both:
- Save the account: 50 ms. The user has waited 50 ms.
- Send the email and wait for the email service to reply: 2,000 ms. The user has waited 2,050 ms.
- Answer: the user sees "Welcome" after 2,050 ms, two whole seconds.
Almost all of that wait is for an email the user is not looking at. Doing the work before the answer like this is called doing it inline.
Doing it later
The other way is to write the email down as a note to do later, answer the user, and let something else send the email afterwards.
The note is called a job. A is a small piece of data that describes the work: which email, to whom. The app server puts the job on a , a waiting line of jobs that a separate system keeps. Then a , a program whose only task is to take jobs from the queue and do them, sends the email.
Now the sign-up goes like this:
- Save the account: 50 ms. The user has waited 50 ms.
- Put a "send welcome email" job on the queue: 5 ms. The user has waited 55 ms.
- Answer: the user sees "Welcome" after 55 ms.
- Meanwhile, a worker takes the job off the queue and sends the email. It still takes 2,000 ms, but nobody is waiting for it.
The email still goes out. The user waits 55 ms instead of 2,050 ms. Work done after the answer, by a worker, is called .
Watch it happen
The card below shows one sign-up. It starts on inline. Run it and step through: watch the time grow to 2,050 ms before the answer. Then switch to queued and run it again. The answer now leaves after 55 ms, and the worker sends the email afterwards.
Press Run it, then step through and watch the requests move.
The rule
Ask one question about each piece of work: does the user need it before they see the next page?
- If yes, do it now, inline. Saving the account, checking a password and loading the page the user asked for are all in this group.
- If no, put a job on a queue and let a worker do it. Emails, resizing an uploaded photo, building a report and sending a notification often are.
The user then waits only for the work they need, plus the few milliseconds it takes to put a job on the queue.
Check yourself
Saving the account takes 50 ms, putting a job on the queue takes 5 ms, and sending the email takes 2,000 ms. With the queue, how long does the new user wait for the welcome page?
An online shop handles an order. Which of these jobs is the best one to put on a queue and do later?
One day the email service is slow: each email takes 10,000 ms instead of 2,000 ms. Saving still takes 50 ms and the enqueue 5 ms. With the queue, how long does the user wait for the welcome page?
Where this goes next
- Split a request's work into what the user needs before the next page and what can wait.
- Say how long the user waits with the slow work done inline (2,050 ms for the sign-up) and with it on a queue (55 ms).
- Say what a job, a queue and a worker are: a note of work to do, a waiting line of such notes, and a program that takes them and does them.
- Say that queued work still happens, after the answer, in the background.