7/28
Part 2 of 6 · Failures and retriesLearn and do

Count the failures, and stop at max_attempts

A task that fails every time would bounce between waiting and reserved forever. A bad input, such as a corrupt file, never gets better on a retry. So the queue counts each task's failed attempts and gives up at a limit, max_attempts.

Say exactly what the limit means before you code it. In the practice problem, max_attempts=3 means a task may be tried three times. The third failure is the last, so it sends the task to the dead-letter queue.

FailureCountWith max_attempts=3
first1back to waiting
second2back to waiting
third3dead

The test is failures >= max_attempts. With >, the task gets a fourth try. That off-by-one is easy to write and easy to miss, because every test where the task succeeds still passes.

Two more details:

  • Each task has its own count. A dict from task id to failures does it.
  • max_attempts below 1 makes no sense, since a task must be tried at least once. Raise ValueError in the constructor.

The dead-letter queue, or DLQ, is just a list of the dead task ids, in the order they died. The task isn't deleted. It's set aside, so a person can look at why it failed.

Predict

max_attempts=2. Task a fails, task b fails, then task a fails again. Which tasks are dead?

Pick an answer above to see the rest