Webhook icon
Schedule icon
Log icon
Script icon
Docker icon
If icon
SlackIncomingWebhook icon

Resume MongoDB Change Streams Without Losing the Handoff

Recover MongoDB change streams with persisted resume tokens, atomic outbox handoff, and explicit history-loss failures in Kestra.

Categories
DataInfrastructure

A change-stream consumer can crash between delivering an event and saving its position. This flow writes the event to a MongoDB outbox and advances the resume token in the same transaction. Its bounded run can be woken by webhook or a disabled schedule. It never silently resets an unusable token.

How it works

  1. explain_handoff logs the watched source and run bounds.
  2. capture_and_checkpoint checks the replica set, verifies the checkpoint belongs to the same collection, resumes with the saved token, and commits each outbox entry with its new token in one transaction.
  3. report_handoff records the count and status; alert_on_invalidation warns when a drop or rename requires startAfter on the next run.

Error handling

Inputs, source identity and the replica-set ID are checked before opening a stream. An expired or invalid token fails with recovery guidance; it is never reset to the current time. MongoDB errors are retried with backoff. The flow-level alert names the execution and tells the operator to reconcile the missing interval before resetting a consumer. Atomic MongoDB transactions prevent an outbox entry without its checkpoint, but downstream outbox delivery still requires an idempotent consumer.

What this blueprint teaches about Kestra

A bounded Kestra execution can hand off change-stream events transactionally, retain a durable cursor between runs, and use an event trigger with a scheduled backstop. The If branch treats MongoDB invalidation as a distinct operational state.

What you get

  • Outbox documents keyed by consumer and SHA-256 digest of each source resume token.
  • A checkpoint document containing the full token, source identity, replica-set ID and resume mode.
  • The handoff JSON flow output with status, count and last event ID.

Prerequisites

  • MongoDB replica set; this flow does not support standalone servers or sharded clusters.
  • Kestra worker with Docker task runner access and a Python container that can reach MongoDB.
  • A Slack incoming webhook for error and invalidation alerts.

Secrets

  • MONGODB_URI: URI for a user allowed to watch the source, read/write checkpoint and outbox collections, and run replSetGetConfig (the read-only clusterMonitor role includes it).
  • SLACK_WEBHOOK_URL: incoming webhook URL for operational alerts.

Inputs

  • database (STRING, change_demo) and collection (STRING, orders): watched source.
  • consumer_id (STRING, orders-to-outbox): stable identity for one cursor.
  • checkpoint_collection and outbox_collection (STRING): durable state collections.
  • docker_network (STRING, bridge): task network; change to the MongoDB network for local Docker.
  • max_events (INT, 50) and max_wait_seconds (INT, 10): per-run work bounds.

Outputs

  • handoff (JSON): IDLE, HANDED_OFF or INVALIDATED, count, consumer and last event ID. Failed runs do not emit flow outputs.

Quick start

  1. Configure both secrets and make the Python task's Docker network able to reach the replica set host in MONGODB_URI.
  2. Run once while writing a test document into change_demo.orders; inspect the outbox and checkpoint. An initial run starts at the current point in the stream, so first take a source snapshot if earlier documents matter.
  3. Run again after another write. The saved token resumes after the previous event; enabling the disabled schedule provides a periodic backstop.

Plugins used

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.