Schedule icon
Consume icon
Log icon
Switch icon
SlackIncomingWebhook icon
If icon
Produce icon

NATS JetStream Consumer Lag and Backlog Sentinel

Monitor NATS JetStream consumer lag with Kestra, classify backlog severity, trigger Slack alerts, and publish remediation signals to prevent message stalls.

Categories
Infrastructure

Automated Day 2 Operations sentinel that probes NATS JetStream durable consumers on a regular schedule to sample unacknowledged backlog accumulation, classify severity, alert engineering channels on threshold breaches, and publish quarantine control signals.

How it works

  1. A schedule trigger activates every six hours to initiate the consumer lag audit across configured subjects.
  2. The io.kestra.plugin.nats.core.Consume task binds to the designated durable consumer and samples available unacknowledged messages within the bounded poll duration.
  3. A Pebble classification expression evaluates the sampled message count against warning and critical thresholds to assign an operational status (HEALTHY, WARNING, or CRITICAL).
  4. A Switch flow control task branches based on the evaluated status:
    • HEALTHY: Emits an informational log confirming consumer group latency is within acceptable tolerance.
    • WARNING: Records an advisory warning log for engineering review before backlogs cascade.
    • CRITICAL: Dispatches a formatted Slack incident alert via webhook, and optionally publishes a dead-letter drain control payload via io.kestra.plugin.nats.core.Produce when auto-remediation is enabled.
  5. An unconditional final audit task logs the comprehensive execution verdict and sampled metrics to Kestra's execution history.

What you get

  • Automated recurring detection of stalled or backlogged NATS JetStream consumers before downstream SLA breaches occur.
  • Zero-footprint sampling using native JetStream durable consumer subscriptions without needing external metric exporters.
  • Tiered severity classification separating normal bursts from critical processing stalls.
  • Direct Slack notifications routing engineers straight to the afflicted stream subject and durable consumer ID.
  • Safe optional dead-letter signal dispatch to trigger auto-scaling or backlog drain pipelines.
  • An immutable execution audit trail capturing historical consumer lag trends over time.

Who it's for

  • Platform Engineers maintaining distributed messaging backbones and microservices clusters.
  • Site Reliability Engineers responsible for event streaming health and processing latency SLAs.
  • Data Engineers running real-time streaming pipelines that consume from NATS JetStream streams.

Why orchestrate this with Kestra

  • Declarative YAML eliminates fragile custom Python cron scripts and unmaintained custom monitoring daemons.
  • Out-of-the-box secret masking ensures credentials and webhook tokens remain secure in compliance audits.
  • Native branching and condition evaluation allow combining health checks, incident alerting, and remediation in one cohesive workflow.
  • Unified execution timeline and logging provide instant root-cause diagnostics when investigating message processing delays.

Prerequisites

  • A running NATS server (version 2.2 or higher) with JetStream enabled (-js flag).
  • An existing JetStream stream bound to the target subject pattern.
  • Network reachability between Kestra workers and the NATS server port (default 4222).

Secrets

  • SLACK_WEBHOOK_URL: Slack Incoming Webhook URL used to transmit critical consumer lag alerts (required when critical alerts fire).

Quick start

  1. Configure the SLACK_WEBHOOK_URL secret within your Kestra namespace or environment variables.
  2. Import this blueprint into your Kestra instance under company.team.
  3. Run a manual execution with default inputs against your local or development NATS broker (nats://localhost:4222).
  4. Verify the probe output in the execution logs and activate the consumer_lag_schedule trigger for continuous monitoring.

How to extend

  • Add PagerDuty or Opsgenie alert tasks within the CRITICAL branch for high-priority on-call paging during off-hours.
  • Introduce Kubernetes pod restart or HPA scale-up tasks in the remediation block to dynamically add consumer replicas when lag spikes.
  • Export sampled lag counts to Prometheus or OpenSearch for long-term capacity planning and streaming SLO dashboards.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.