A visibility timeout turns a reservation into a lease
Part 2 had a hole. A worker that crashes never calls fail, so its task stays reserved forever. Nobody else can take it.
A prep guide's page on OpenAI mentions a queue with visibility timeouts (1 source). That's the fix, and it's how Amazon SQS works. A reserved task is hidden, or invisible, for a fixed time. If the worker doesn't ack before the time is up, the task becomes visible again, and another worker can take it.
So a reservation is really a lease: a hold on the task that ends by itself at a deadline, reserved_at + visibility_timeout. When the deadline passes:
- the holder no longer holds the task, so its ack or fail returns
False - the task goes back to waiting, in its original place
- the timeout counts as a failed attempt, so a task that keeps timing out reaches the dead-letter queue
That last point matters. A task that crashes its worker every time, say one that runs out of memory, never calls fail. Without counting timeouts, it would loop forever.
When several leases have ended, handle them in deadline order. The order decides which task dies first, so it decides the order of dead_letters().
The leases are a until 12, b until 10 and c until 30. At time 12, which leases have ended, in order?