Queues, Leases and Worker Recovery
In this article
- Explain why a queue may hand the same job to two workers, and why that is normal.
- Choose a lease length and a heartbeat interval, and trace when a silent worker loses its job.
- Reject a late commit from a worker whose lease expired, using a fencing token.
- Move a job that fails on every delivery to a dead-letter queue after a set number of deliveries.
- Terms defined here: at-least-once delivery, lease, heartbeat, fencing token, poison job, dead-letter queue.
Overview
The shop's support agent runs on a pool of workers: each run handles one refund request, reads the order with get_order and pays with create_refund, which calls an outside payments provider. Requests wait on a queue, and any free worker takes the next one.
Workers fail in untidy ways: one crashes, one freezes for a minute and a half, one chokes on a request it can never read. Who finishes the refund then, and how many times does it run?
By the end you can decide how long a worker may hold a job before another worker gets it, trace what happens when a worker freezes right after it pays, stop its late write from overwriting the good one, and set aside a request that can never succeed.
Sign in to keep reading
The rest of this article opens with an account. An account also keeps your progress and quiz results on every device, and lets you follow news for up to five topics.
Still to read in this article
- Why the job comes back
- Leases and heartbeats
- A worker that freezes
- Fencing the stale commit
- Poison jobs and the dead-letter queue
- What the token cannot undo
- Common mistakes
- In production
- Sources
- Check yourself, 13 questions