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

Redis Stream Dead Consumer Pending Reclaim Guard

Reclaim stuck pending entries from dead Redis Stream consumers with XAUTOCLAIM, delete the consumer with XGROUP DELCONSUMER behind a Pause, then verify.

Categories
DataInfrastructure

A Redis Stream consumer group tracks every message it delivers until a consumer acknowledges it. When a consumer crashes or is scaled down, the entries it was handed stay in its Pending Entries List forever. They are never reprocessed, the group lag never clears and the uncleared PEL keeps the stream from being trimmed with XADD MAXLEN or XTRIM. Nothing fails loudly. The queue just stops draining and memory climbs.

This blueprint reads XINFO GROUPS and XINFO CONSUMERS, flags every consumer that has been idle past a threshold and still owns pending entries, reclaims those entries onto a live consumer with XAUTOCLAIM so the work is reprocessed instead of lost, then removes the dead consumer with XGROUP DELCONSUMER. It reads the group again and fails the run if a dead consumer is still there or still owns pending.

This blueprint was created by zkasuran.

Safety

  • A consumer that is actively reading is never a target, even when it holds pending entries. Pending on an active consumer is normal in-flight work, so the detector requires both idle past idle_threshold_ms and a non-zero pending count.
  • XAUTOCLAIM only moves entries idle at least reclaim_min_idle_ms. Set that above the longest time a healthy consumer takes to process one message, so a slow but live message is never stolen from under the worker holding it.
  • A dead consumer is deleted only after a fresh read shows its pending list is empty. Deleting a consumer discards whatever pending entries it still owns, so a consumer that still holds pending after the reclaim is left in place and the run fails instead.
  • The reclaim target is never a reap target, so the group always keeps at least one consumer.
  • The reclaim and the delete need auto_remediate true, dry_run false and a human answering the Pause. Three conditions, all of them, before anything changes.

How it works

  1. detect (io.kestra.plugin.redis.cli.RedisCLI) runs one Lua script through redis-cli --eval. The script calls XINFO GROUPS and XINFO CONSUMERS and returns the name, pending count and idle time of every consumer as JSON. Read-only.
  2. assess flags each consumer idle at least idle_threshold_ms that still owns pending, picks the consumer to reclaim onto (the reclaim_consumer input, else the most recently active consumer, else fallback_reclaim_consumer) then builds the reap set, which never contains the reclaim target. summary logs it.
  3. route (io.kestra.plugin.core.flow.If): a clean group logs and stops. A dead consumer, a missing stream or a missing group sends alert to Slack and planned prints what a remediation run would do.
    • remediate_gate opens only with auto_remediate true, dry_run false and a dead consumer that owns pending. hold (io.kestra.plugin.core.flow.Pause) waits for an answer.
    • On approval, reclaim runs XAUTOCLAIM looped on its cursor to move the stuck entries onto the reclaim target. reread1 reads the consumers again. delete_plan keeps only the reap targets whose pending is now zero. delete_dead_consumers (io.kestra.plugin.core.flow.Loop, concurrencyLimit: 1) runs one XGROUP DELCONSUMER per consumer. reread2 reads the group a final time and verify_result fails the run if any reap target is still present or the reclaim loop did not finish.
    • Without approval, declined records the decision and nothing changes.
  4. errors posts to Slack when the flow fails.

What you get

  • A per-consumer view of a stream group: who is active, who is idle and how many entries each one is holding unacknowledged.
  • The stuck work moved back onto a live consumer so it is reprocessed, not abandoned in a crashed consumer's PEL.
  • The dead consumer removed so the group stops reporting phantom pending, with a re-read that fails the run when the change did not land.

Who it's for

  • Teams running Redis Streams with a consumer group behind a worker pool that scales up and down or restarts, which leaves behind consumers nobody cleans up.
  • SREs who have watched a group's lag sit at a fixed non-zero number for days because a consumer that no longer exists still owns the entries.
  • Anyone evaluating the Redis plugin who wants an XAUTOCLAIM and XGROUP example that is safe to point at a real group.

Why orchestrate this with Kestra

Reclaiming a PEL safely is a sequence, not one command. You read the group, decide which consumers are genuinely dead, move their entries without stealing live work, delete only the consumers that are now empty, then read the group again to confirm. Kestra keeps that sequence in one place, puts the destructive half behind auto_remediate, dry_run and a Pause, runs it on a schedule and records every decision on the execution.

Tested end to end

On Kestra 2.0.5 OSS against a Redis 7.4 container, with a local HTTP sink standing in for Slack. The stream orders held a consumer group g1 with a crashed consumer dead1 holding pending entries it never acknowledged, plus an active consumer worker1 reading and holding in-flight work. Every run was checked against redis-cli with XINFO CONSUMERS, XINFO GROUPS and XPENDING, not only against the Kestra log.

Run Execution Inputs Result
Healthy 10v19VY9ChoFNvZ4ENFJsS both consumers just read (idle under 5s), each holds pending 0 findings. dead1 pending 3 and worker1 pending 1, both spared because neither is idle past the threshold. Route logs clean
Degraded, dry run 7duhkjs2LDgrj6RVuv1YDA idle_threshold_ms: 5000, dead1 idle 41625ms Names dead1 only (pending 3), reclaim target worker1. worker1 idle 42ms pending 2, spared. Alert sent, nothing changed
Remediate, approved 7WsnXsewX98QIKtxdRgL6p auto_remediate: true, dry_run: false, threshold 25000, resumed approved: true XAUTOCLAIM moved 4 entries onto worker1 (pending 2 to 5), XGROUP DELCONSUMER removed dead1, re-read confirmed dead1 gone. XPENDING total 5 before and after, so nothing was lost
Declined 6i0JCAf8uwZuSNFDxiULwf the same, resumed approved: false Paused then declined. dead1 still present with pending 3
Failure 2UQVKXnWDTqidTntBfMil5 reclaim_min_idle_ms: 999999999 so XAUTOCLAIM reclaims nothing Reclaim claimed 0, dead1 still owned pending, so it was not deleted. verify_result failed the run and the errors block posted to Slack. dead1 kept its 3 entries
Fallback target 6Otw1Ao9bWz6mL0LogKbBx no active consumer in the group Both idle consumers reclaimed onto the created reclaim-rescue, then deleted. Entries preserved on reclaim-rescue

Prerequisites

  • A Redis 6.2 or later instance with at least one stream and consumer group, reachable from the containers the RedisCLI tasks start. XINFO, XAUTOCLAIM and XGROUP must be allowed for the user.
  • A live consumer to reclaim onto. Leave reclaim_consumer empty to auto pick the most recently active one.
  • For a password-protected instance add password: "{{ secret('REDIS_PASSWORD') }}" to each RedisCLI task.
  • Local testing:
    • docker run -d --name redis-stream --network mynet redis:7-alpine;
    • seed a stuck PEL with XADD, XGROUP CREATE, then XREADGROUP as a consumer without XACK and let it go idle;
    • run the flow with network: mynet.

Secrets

  • SLACK_WEBHOOK_URL: used by alert and the errors block. In Kestra OSS, provide it as the base64-encoded environment variable SECRET_SLACK_WEBHOOK_URL.

Inputs

Input Default Purpose
stream, group orders, g1 The stream and consumer group to check
idle_threshold_ms 900000 Idle time past which a consumer with pending is dead
reclaim_min_idle_ms 900000 XAUTOCLAIM only moves entries idle at least this long
reclaim_consumer empty Consumer to reclaim onto, empty to auto pick
fallback_reclaim_consumer reclaim-rescue Used only when no consumer is active
scan_count 500 XAUTOCLAIM COUNT per cursor step
max_iterations 1000 Ceiling on the reclaim loop
host, port, database redis-stream, 6379, 0 The instance to read
network empty Docker network for the redis-cli containers
approval_timeout PT2H How long the pause waits
auto_remediate false Allow the reclaim and the delete
dry_run true Must be false for a change to apply

Quick start

  1. Add the SLACK_WEBHOOK_URL secret.
  2. Import the flow, set stream and group and run it with the defaults. It reports the consumers and stops without changing anything.
  3. Set idle_threshold_ms to how long a consumer may be idle before you treat it as gone and reclaim_min_idle_ms above your longest message processing time.
  4. To clean up, name a live reclaim_consumer or leave it empty, set auto_remediate: true and dry_run: false, run it, then answer the pause.
  5. Enable the hourly trigger, which always runs in safe mode.

How to extend

  • Check several groups by wrapping the read in a Loop over a list of stream and group pairs.
  • Trim the stream after a clean reclaim with XADD MAXLEN or XTRIM, now that the PEL no longer pins old entries.
  • Alert only when the reclaimed count or the group lag crosses a threshold, so a tiny PEL stays quiet and a real backlog pages someone.
  • Replace the Pause with a Slack approval. Drop it for a group you are sure about.
  • Record the reclaimed count per run in Kestra KV to chart how often consumers are dying.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.