Webhook icon
Schedule icon
Find icon
Script icon
Switch icon
If icon
Bulk icon
Delete icon
Write icon
Log icon
SlackIncomingWebhook icon
Fail icon

MongoDB Dead-Letter Mutation Reconciliation and Bulk Repair Gate

Triage failed MongoDB mutations from dead-letter collections, enforce version monotonicity, and execute atomic bulkWrite repairs with rollback safeguards.

Categories
Data

Automates dead-letter queue triage, conflict resolution, and atomic bulkWrite repair for out-of-order MongoDB CDC and stream mutations. In high-throughput event-driven microservices, CDC streams, or ETL pipelines, concurrent writes and transient network partitions frequently push rejected updates into dead-letter collections (_dlq). Naive line-by-line replays cause duplicate-key collisions, overwrite newer documents with stale timestamps, and generate heavy lock contention. This blueprint evaluates queued mutations, enforces monotonic document versioning, deduplicates conflicting updates, and executes high-performance atomic repairs using MongoDB's native db.collection.bulkWrite() with automated quarantine cleanup.

How it works

  1. An event-driven Webhook trigger captures dead-letter queue depth breach notifications, while an hourly Schedule trigger provides periodic reconciliation sweeps.
  2. fetch_dead_letter_records (io.kestra.plugin.mongodb.Find) queries quarantined mutations from the designated dead-letter collection (dead_letter_collection) with bounded limit batch_size.
  3. evaluate_and_generate_bulk_plan (io.kestra.plugin.scripts.python.Script) analyzes the mutation batch:
    • Groups mutations by target document identifier (targetId / _id).
    • Enforces version monotonicity by sorting chronological updates and discarding superseded stale states.
    • Synthesizes atomic MongoDB bulkWrite operations formatted as NDJSON (updateOne with $max version guards, replaceOne with upsert, and deleteOne).
    • Detects unresolvable poison mutations (missing identifiers, malformed BSON types) and evaluates them against max_poison_threshold.
  4. route_reconciliation_verdict (io.kestra.plugin.core.flow.Switch) routes downstream actions based on the classified plan:
    • PROCEED_RECONCILIATION: When dry_run is false, runs io.kestra.plugin.mongodb.Bulk to stream the NDJSON payload into the target collection, purges reconciled records from the DLQ via io.kestra.plugin.mongodb.Delete, and writes an execution receipt to internal storage.
    • POISON_MUTATION_BREACH: Quarantines malformed mutations into an audit dossier, logs a high-severity alert, and conditionally notifies database administrators via Slack.
    • EMPTY_DLQ: Confirms normal healthy baseline when the dead-letter queue is empty.
  5. enforce_gate_policy (io.kestra.plugin.core.flow.If) triggers io.kestra.plugin.core.execution.Fail if enforce_gate is enabled and poison mutations exceeded the acceptable threshold.

What you get

  • High-throughput atomic document reconciliation leveraging MongoDB's native bulkWrite engine instead of slow query loops.
  • Mathematical version monotonicity preventing stale out-of-order mutations from overwriting fresher production data.
  • Automated dead-letter queue lifecycle management that deletes successfully reconciled records only after verified write completion.
  • Poison mutation isolation preserving malformed records for root-cause analysis without blocking clean mutations.
  • Audit manifests persisted to Kestra internal storage (kestra://...) documenting exact reconciled identifiers.

Who it's for

  • Database Administrators and Data Platform Engineers managing MongoDB clusters backed by streaming CDC or message queues.
  • Backend SREs automating dead-letter queue recovery and transient write conflict resolution.
  • Platform Engineers seeking declarative, auditable alternatives to ad-hoc Python repair scripts.

Why orchestrate this with Kestra

  • Direct integration with native MongoDB bulkWrite streaming without maintaining external worker pools.
  • Containerized Python execution for complex conflict resolution and NDJSON synthesis within a single YAML file.
  • End-to-end secret masking protecting MongoDB credentials and Slack webhooks across execution history.

Prerequisites

  • A MongoDB 4.4+ database cluster (replica set or standalone) accessible to the Kestra worker.
  • Read/write privileges on the target operational collection and dead-letter queue collection.

Secrets

  • MONGODB_URI: MongoDB connection string containing authentication credentials (e.g. mongodb+srv://user:pass@cluster.mongodb.net/).
  • MONGODB_RECONCILE_WEBHOOK_KEY: Secret key for authenticating incoming dead-letter webhook triggers.
  • SLACK_WEBHOOK_URL: Optional Slack webhook URL for dispatching poison surge alerts (required only if notify_slack is enabled).

Quick start

  1. Import this blueprint into your Kestra instance.
  2. Configure the MONGODB_URI secret in your Kestra namespace.
  3. Click Execute using default inputs (dry_run: true) to test the reconciliation planner against the bundled offline dead-letter sample.
  4. Review execution logs and inspect the synthesized bulk_operations.ndjson artifact in internal storage.
  5. Set dry_run: false to enable live bulkWrite execution and activate the scheduled trigger.

How to extend

  • Add automated schema validation against a JSON Schema contract before synthesizing the bulkWrite payload.
  • Chain downstream cache invalidation tasks (e.g. io.kestra.plugin.redis.string.Delete) to clear stale cache keys for reconciled documents.
  • Wire PagerDuty alerting to notify on-call SREs if poison mutation rates breach sustained thresholds.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.