RedisCLI icon
Docker icon
OutputValues icon
Log icon
If icon
SlackIncomingWebhook icon
Pause icon
Fail icon
Loop icon
Schedule icon

Requeue Redis list jobs stranded by a dead worker

Find jobs stranded in a Redis in-flight list after a worker died, requeue them with an atomic LMOVE behind an approval, then verify both lists.

Categories
DataInfrastructure

Safety

Requeueing a job makes it run a second time. If the dead worker had already charged the card, sent the email or written the row, the replay does it again. The queue also cannot be fixed with two separate commands: an LREM followed by an LPUSH loses the job if the connection drops between them, while an LPUSH followed by an LREM runs it twice. So every write here is a single LMOVE inside one Lua script. The flow also refuses by default:

Guard What it stops
Two independent conditions, a stale heartbeat and a claim older than visibility_timeout_ms A worker that is slow, paused or mid long running job keeps its work. One signal on its own is not enough to take a job away.
Oldest entry first, stop at the first fresh one A worker that died holding both old and recent claims only gives up the claims that are genuinely abandoned.
protected_workers A queue under manual investigation is reported but never written, even with remediation armed and approved.
auto_remediate false plus dry_run true plus a Pause approval Three independent switches stand between a finding and a write. A pause that expires changes nothing.
Eligibility re-read after the approval An approval is a snapshot. If the worker started heartbeating again while you were deciding, it is dropped from the plan.
Stale plan refusal If an approved job has left both lists, something else is moving this queue. The run stops before any write rather than acting on a plan it can no longer trust.
One LMOVE per entry inside a Lua script A job is never in both lists and never in neither, even if the worker, Kestra or the connection dies mid pass.
LPOS duplicate check before each move An identical payload already waiting on the pending list is refused rather than queued twice.
max_requeue_per_worker, max_workers, max_list_scan A detector mistake on a very long list cannot turn into an unbounded flood of replayed work.
Balance check on a final independent read The write returning success is not treated as proof. The run fails if an approved job was not requeued, ended up in both lists, ended up in neither or if a worker dropped from the plan was written anyway.

How it works

  1. detect (io.kestra.plugin.redis.cli.RedisCLI) runs one read-only Lua script through redis-cli --eval. The script SCANs for <inflight_prefix>*, checks each key's TYPE, reads it oldest entry first with LRANGE, reads the worker liveness hash with HGETALL plus the pending list, then returns one JSON row per worker with its heartbeat age, its claim ages, its stranded entry ids and the reason it is or is not a candidate. Running it as a script means the whole queue is read in one atomic server side pass, so the report cannot mix two different moments.
  2. assess (io.kestra.plugin.core.output.OutputValues) turns that into a plan. A worker joins it only when its heartbeat is stale or missing, its key really is a list, its oldest entry carries a readable claimed_at past visibility_timeout_ms and its name is not in protected_workers. Everything else lands in spared with the reason: worker_alive, within_visibility, oldest_unparsable, not_a_list, no_inflight or protected_worker.
  3. summary (io.kestra.plugin.core.log.Log) prints the thresholds used, the per worker findings plus the spared list, so a read-only run is already a useful report.
  4. route (io.kestra.plugin.core.flow.If) fires only when something is stranded or when the SCAN hit max_workers. alert posts the same text to Slack, planned logs exactly what a remediation run would do.
  5. remediate_gate requires auto_remediate true, dry_run false plus at least one finding. hold (io.kestra.plugin.core.flow.Pause) then waits for a human, read as onResume.approved ?? false, so a pause resumed with no answer is a refusal.
  6. recheck runs the same detect script again after the approval. plan_recheck compares the approved job ids against that fresh read: a worker that is heartbeating again is dropped. An approved job that is now in neither list sends stale_plan_gate into stale_plan_failed (io.kestra.plugin.core.execution.Fail) before anything is written.
  7. requeue (io.kestra.plugin.core.flow.Loop, concurrencyLimit: 1) handles one worker at a time. requeue_worker runs a second Lua script that re-reads the heartbeat with HGET, walks the list tail with LINDEX, checks the entry is in the approved plan, checks LPOS does not already find that payload on the pending list, then moves it with one LMOVE <inflight> <pending> RIGHT LEFT and verifies the moved value is the one it inspected.
  8. reread reads the queue a third time. balance accounts for every approved job against that read, then verify_result sends the run into remediation_failed unless each one is now on the pending list, out of its in-flight list, present exactly once, with no write against a worker the recheck had dropped.

The outputs worth reading afterwards: {{ outputs.detect.vars.command_1 }} is the raw per worker read, {{ outputs.assess.values.items }} holds the plan plus the spared table with a reason per worker, {{ outputs.plan_recheck.values.items }} shows what the post-approval read changed, then {{ outputs.balance.values.items }} is the per job accounting the verification is based on.

What you get

  • Recovery of work that no queue depth alert can see. jobs:pending reads zero and looks healthy while the jobs sit in a private in-flight list no consumer ever reads.
  • A named report before anything moves: which worker, how many entries, how old its oldest claim is, how stale its heartbeat is.
  • A requeue that cannot duplicate or lose a job, because each entry moves with one atomic LMOVE rather than a delete plus a push.
  • A long running job left alone. A worker that is still heartbeating keeps its claims however old they are.
  • A run that fails loudly when the move did not land, instead of reporting success on an empty write.

Who it's for

Teams running the Redis reliable queue pattern (LMOVE or the older RPOPLPUSH) for background jobs, where a worker crash, an OOM kill or a rolling deploy leaves claimed work in a per worker in-flight list that nothing will ever drain.

Why orchestrate this with Kestra

The manual fix is a developer reading in-flight lists by hand during an incident, deciding from memory which workers are dead, then pasting LMOVE commands into redis-cli with no record of what moved. Every risky part of that is exactly what a flow is good at: the detection rule is written down instead of recalled, the approval is recorded against the execution, the credentials stay in secrets, the write is bounded plus serialised, then the verification is a separate read that can fail the run. The Pause task is what makes replaying customer facing work safe to automate, because the plan is built, shown, re-checked against live state then applied as one audited unit with a human in the middle.

Tested end to end

Run against Redis 7.4.11 (redis:7-alpine) on Kestra 2.0.5 with plugin-redis 1.7.0, every case on flow revision 5. The fixture builds a list queue with jobs:pending, six jobs:inflight:<worker> lists, a jobs:heartbeat hash plus an out of prefix other:inflight:worker-x queue. Every assertion is read back with redis-cli inside the Redis container, never through Kestra. A second container runs a real consumer of the same queue as an independent probe.

Fixture roster: worker-dead-1 (dead, three claims 28 to 30 minutes old), worker-dead-5 (dead, one 30 minute claim behind one 10 second claim), worker-live-1 (heartbeating every two seconds, claims 34 minutes old), worker-dead-3 (dead, claims 15 to 20 seconds old), worker-protected-1 (dead, 31 minute claims, named in protected_workers), worker-badjson (dead, oldest entry is not JSON), worker-hash (a hash, not a list).

Case Execution Result
Healthy: every worker heartbeating, old in-flight work KQKd43Y8R7V3tvKLkvoeI 0 findings, queue bytes identical before and after
Degraded, dry_run: true 2JtAjAF4oGmM32ReRslWfF plan matched the independent computation exactly, worker-dead-1 3 jobs plus worker-dead-5 1 job; 5 lookalikes spared with a reason each; queue bytes unchanged
Approval declined 2MNodpFfRqbkaLP7x2ek8b no requeue task ran, queue bytes unchanged, findings still open
Approved remediation 3gKEmhWK6SYWRR64r3MDFb 4 jobs moved, jobs:pending 2 to 6, jobs:inflight:worker-dead-1 drained to 0, worker-dead-5 kept job-0052, 15 entry ids before and 15 after with no duplicate
Second run straight after 5OvHXTysZdqglkRhRQTgIK 0 findings, no approval requested, no write, no duplicate created
Every stranded worker protected, armed plus approved 1qjUCyp2SvM6kB39YRtxno 0 findings, no pause, queue bytes unchanged while the stranded work stayed in place
Refused write: an identical payload already on the pending list 7Qip7kHkzcF8es9nBfTURY LPOS refused the move, jobs:pending 3 before and 3 after, in-flight list untouched, run FAILED
Worker revived during the pause 3O8JZpPmtjIUV7xFzpTodn recheck dropped it as worker_alive, no requeue task ran, all 3 entries stayed with the worker
Approved job vanished from both lists during the pause QJ8pS5LdQvBC6fVYBfWPo stopped at stale_plan_gate before any write, surviving entries untouched, run FAILED

Operational payoff, measured with a real consumer of the same queue rather than with flow output. Before the requeue, the consumer claimed job-9001 plus job-9002 then reported queue empty after 2 claims while six jobs sat in in-flight lists: jobs:done held 2. After the approved remediation the same consumer completed 6 jobs, job-0001, job-0002, job-0003 plus job-0051 among them, while jobs:pending returned to 0. The whole queue held 15 entry ids before the requeue and the same 15 afterwards.

Prerequisites

  • Redis 6.2 or later, because LMOVE needs 6.2.
  • Workers that claim with LMOVE <pending> <inflight> RIGHT LEFT, acknowledge with LREM, write their liveness into a hash plus store each job as a JSON object carrying id and claimed_at in epoch milliseconds.
  • Kestra 2.0 with the Redis plugin plus the Docker task runner, which RedisCLI uses to run redis-cli.
  • Minimum permissions. Detection needs EVAL, SCAN, TYPE, LLEN, LRANGE plus HGETALL; remediation adds HGET, LINDEX, LPOS plus LMOVE. Restrict it to the queue keys, for example ACL SETUSER queueguard on >secret ~jobs:* +eval +scan +type +llen +lrange +hgetall +hget +lindex +lpos +lmove. Leave +lmove off for a report only user.
  • A Slack incoming webhook.

Secrets

Secret What it is
SLACK_WEBHOOK_URL Slack incoming webhook for the findings plus the failure alert

Inputs

Input Type Default What it does
pending_key STRING jobs:pending the list workers claim from
inflight_prefix STRING jobs:inflight: per worker in-flight keys, scanned as <prefix>*; keys outside it are never read or written
heartbeat_key STRING jobs:heartbeat hash of worker name to last seen epoch milliseconds
worker_timeout_ms INT 60000 heartbeat age that makes a worker dead; a worker missing from the hash counts as dead too
visibility_timeout_ms INT 900000 how old a claim must be before it can be requeued
protected_workers ARRAY [] workers reported but never written
max_requeue_per_worker INT 500 ceiling on one pass per worker
max_workers INT 200 ceiling on in-flight keys scanned; hitting it raises the alert
max_list_scan INT 200 how deep each list is read, oldest first
scan_count INT 100 SCAN COUNT per step
host STRING redis-jobs Redis host
port INT 6379 Redis port
database INT 0 database index
network STRING "" Docker network for redis-cli, only needed when Redis is on a named network
approval_timeout STRING PT2H how long the pause waits before expiring with nothing changed
auto_remediate BOOL false allow a requeue to be considered
dry_run BOOL true report without writing

Quick start

  1. Add SLACK_WEBHOOK_URL to your Kestra instance.
  2. Set pending_key, inflight_prefix plus heartbeat_key to your own key names, then run it as it ships. dry_run true with auto_remediate false reports which workers hold stranded work plus which ones were spared and why. Nothing is written.
  3. Read the spared list first. If a worker you expected to be dead shows up as worker_alive, your heartbeat is still being written by something and the job is not actually abandoned.
  4. Set visibility_timeout_ms above the longest time a healthy worker legitimately takes to finish one job, then set worker_timeout_ms above your heartbeat interval times the number of beats you are willing to miss.
  5. Confirm the jobs in the report are safe to run again. Add any queue you are still investigating to protected_workers.
  6. Run with auto_remediate: true plus dry_run: false. The execution pauses. Approve it: the flow re-checks eligibility, moves each entry with one LMOVE, then fails the run if the accounting does not balance.
  7. Enable the schedule once you trust the report. It ships disabled plus forces report only mode.

How to extend

  • Give each queue its own schedule with its own pending_key, inflight_prefix plus timeouts, since a video encoding queue and a webhook queue need very different visibility windows.
  • Add a dead letter path: instead of requeueing an entry whose attempts field is already high, move it to a jobs:failed list and alert, so a job that keeps killing its worker stops taking out the next one.
  • Swap Slack for PagerDuty when the stranded count crosses a number that means a worker fleet is cycling rather than a single pod having crashed.
  • Feed {{ outputs.assess.values.items }} into a table to track which worker strands jobs most often, which usually points at one deploy target or one job type rather than at Redis.
  • Pair it with a flow that scales the consumer deployment, so the requeued work is actually picked up rather than queued behind the same shortage.

Limits: an entry must be a JSON object carrying id and claimed_at. An entry whose claim time cannot be parsed stops the scan of that list, which is reported as oldest_unparsable and never written. Liveness is whatever the workers write into the heartbeat hash, so a fleet that does not heartbeat looks entirely dead, which is why the visibility timeout is a second required condition rather than a refinement. The requeue restores a job to the pending list; it cannot undo the part the dead worker already performed, so job bodies must be idempotent, which is already required of any at-least-once queue. Lists longer than max_list_scan are read only that deep and reported as truncated. The flow serialises its own iterations with concurrencyLimit: 1 but takes no Redis lock, so two overlapping executions are not serialised against each other; the post-approval recheck plus the LPOS duplicate check are what stop a second pass from queueing the same payload twice. The duplicate check is LPOS on the exact payload, so if a producer re-created the same job id with a different payload while the guard was mid run, the requeue would add a second entry for that id; plan_recheck reports such an id under in_pending but does not refuse on it. Verification proves the entry moved and is present exactly once by payload; it does not prove a consumer later succeeded at the job.

Links

This blueprint was created by zkasuran.

See How

New to Kestra?

Use blueprints to kickstart your first workflows.