When a job fails
After this: Say when each retry runs, why the waits grow, and where a job goes when it never succeeds.
On this page · 8 sections · 3 checks
A worker takes "send welcome email to Ben" and asks the email service to send it. The email service answers with an error: it is overloaded for a moment. The worker is still running, so this is not a crash. The job failed. What should the worker do with it?
Try again, later
Most failures like this pass: a moment later the email service is fine again. So the worker does not give up. It puts the job back to be tried again after a short wait. Trying a failed job again is called a .
Two questions remain. How long should it wait? And what if the job never works?
Waiting longer each time
If a hundred jobs fail because the email service is overloaded, and all hundred are retried straight away, the service gets the same hundred requests again while it is still struggling. So each retry waits longer than the last, which gives the service time to recover. Growing waits like this are called .
Here the waits are 1, 2 and 4 ticks, each double the last, and a job gets at most 3 retries. That makes at most 4 attempts: the first one and 3 retries.
A job that never works
Some jobs fail every time. The email address may not exist, or the job may hold data the worker cannot read. Retrying it for ever wastes the workers' time. So after the last retry, the job is moved to a separate queue called the . Nothing retries a job there. A person, often alerted when jobs arrive there, looks at each one later, finds out what went wrong, fixes it and sends the job again if it is still needed. In some systems the worker moves the job; in others the queue does it after a set number of attempts.
Five jobs, by hand
Five jobs, j1 to j5, are on the queue at tick 0. j3 fails on its first two attempts and then works. j5 fails every time. The others work the first time. Follow the two that fail:
- Tick 0: j1, j2 and j4 succeed. j3 and j5 fail. Each waits the first wait, 1 tick.
- Tick 1: j3 and j5 both fail again. Each waits the second wait, 2 ticks: back at tick 3.
- Tick 3: j3 succeeds on its third attempt. j5 fails a third time and waits the third wait, 4 ticks: back at tick 7.
- Tick 7: j5 fails its fourth attempt. It has used all 3 retries, so it goes to the dead-letter queue.
Count the attempts: 5 at tick 0, 2 at tick 1, 2 at tick 3 and 1 at tick 7, which makes 10. Five of them were retries. Four jobs succeeded, and one was dead-lettered.
Watch it happen
The task asks you to follow j3 and j5. Each time one fails, the narration says when it comes back. The Dead letters card fills at tick 7.
- Retries: 5 jobsRun it and step through. Follow j3 and j5. Each time one fails, read when it comes back. The waits are 1, 2 and then 4 ticks.
Press Run it, then step through and watch the requests move.
The rule
- A failed job is retried after a wait, and each wait is longer than the last.
- After the last retry, a job that still fails goes to the dead-letter queue, where a person looks at it.
- To find when an attempt runs, add the waits used so far to the time of the first attempt.
Check yourself
The waits before retries are 1, 2 and 4 ticks. j3 fails its first attempt at tick 0 and its second attempt at tick 1. When does its third attempt run?
What is the dead-letter queue for?
The emails fail because the email service is overloaded. Why is it better to wait longer before each retry than to retry every job at once?
Where this goes next
- Say what a retry is, and why a failed job is tried again after a wait.
- Work out when each attempt runs by adding the waits (1, 2, 4 ticks) to the first attempt: 0, 1, 3, 7.
- Say why each wait is longer than the last: quick retries add load to a service that is already struggling.
- Say what the dead-letter queue is for: jobs that failed on every retry, kept aside for a person to look at.