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

A worker can crash after the work and before the ack

Here is the failure that shapes everything in this part. Worker w1 reserves a task that charges a customer $30. It charges the card. Then, before it can send the ack, its machine restarts.

The queue sees a reserved task and a worker that went quiet. It can't tell which of these happened:

  • w1 crashed before it charged the card.
  • w1 charged the card, then crashed before the ack.

The safe choice is to run the task again. A lost task is a bug nobody sees. So a queue that retries delivers each task at least once, and sometimes more than once.

That moves part of the job to the worker. Amazon's SQS guide says it directly: design your applications to be idempotent, so that handling the same message twice does no harm. An operation is idempotent when doing it twice leaves the same result as doing it once.

The usual way is an idempotency key. The task id travels with the work, and whatever applies the result remembers the ids it has applied. A second copy of the same id is skipped.

Predict

Deliveries: (pay-1, 30), (pay-2, 50), (pay-1, 30). pay-1 was delivered twice. What total should the worker apply?

Pick an answer above to see the rest