id: mongodb-duplicate-document-dedup-guard
namespace: company.team
description: |
Find the MongoDB documents that share a business key no unique index could keep apart. The
bundled MongoDB plugin cannot create an index, so a collection cannot be given a unique
constraint from Kestra. Nothing then stops an at-least-once ingest or a retried upsert from
writing the same logical record twice. Duplicates build up on an orderId, an email or an
external id and every reader that assumes the key is unique double counts or picks an
arbitrary copy. This flow groups the collection by the business key, reports the keys carried
by more than one document, keeps one survivor per key by a rule you choose, deletes the surplus
copies behind a safe mode gate and an approval, then groups the collection again and fails the
run if any key still has more than one document.
inputs:
- id: connection_uri
type: STRING
displayName: MongoDB connection string
description: >-
Leave empty in production so the flow reads the MONGODB_URI secret. Set it
to a plain connection string only for a local test run.
defaults: ""
- id: database
type: STRING
displayName: Database
defaults: appdata
- id: collection
type: STRING
displayName: Collection
defaults: orders
- id: business_key
type: STRING
displayName: Business key
description: >-
The field that should identify one logical record, for example email or
orderId. For a composite key pass the fields comma separated, for example
orderId,source. A document that is missing any key field or holds null in
it, is never grouped and never touched.
defaults: email
- id: survivor_strategy
type: SELECT
displayName: Which copy to keep
description: >-
How the one survivor per key is chosen. oldest_id keeps the smallest _id,
which for a stock ObjectId is the first inserted. newest_id keeps the
largest _id. newest_timestamp keeps the document with the largest
timestamp_field, with the smallest _id as the tie break. The surplus is
every other copy of that key.
values:
- oldest_id
- newest_id
- newest_timestamp
defaults: oldest_id
- id: timestamp_field
type: STRING
displayName: Timestamp field
description: >-
The BSON date field used only when survivor_strategy is newest_timestamp.
Ignored by the other strategies.
defaults: createdAt
- id: scan_limit
type: INT
displayName: Duplicate keys named per report
description: >-
Ceiling on how many duplicate keys the report and the re-check name. The
counts are exact and server-side, so the cap only bounds how many keys are
listed.
defaults: 25
- id: delete_limit
type: INT
displayName: Surplus documents deleted per run
description: >-
Ceiling on how many surplus copies one run deletes, so a huge backlog
cannot build one giant write. The detection count is exact. If the surplus
is larger than this the run clears part of it and the re-check fails until
a later run finishes the job.
defaults: 50000
- id: batch_size
type: INT
displayName: Document ids per delete batch
description: >-
The delete runs in batches of this many ids, one batch at a time, so a
large dedup never becomes one long running write.
defaults: 500
- id: approval_timeout
type: STRING
displayName: Approval timeout
description: ISO-8601 duration to wait for an answer before the pause expires.
defaults: "PT2H"
- id: auto_remediate
type: BOOL
displayName: Auto remediate
description: Allow the delete. Still needs dry_run false and an approval.
defaults: false
- id: dry_run
type: BOOL
displayName: Dry run
description: Must be false, together with auto_remediate, before any document is
deleted.
defaults: true
variables:
detect_pipeline: >-
{{ {'bk': inputs.business_key, 'sl': inputs.scan_limit, 'dl':
inputs.delete_limit, 'strat': inputs.survivor_strategy, 'ts':
inputs.timestamp_field} | jq('.bk as $bk | .ts as $ts | .strat as $strat |
.sl as $sl | .dl as $dl | ($bk | split(",") | map(gsub(" ";"")) |
map(select(. != ""))) as $fields | {"$and": ($fields | map({(.): {"$exists":
true, "$ne": null}}))} as $exist | ($fields | to_entries | reduce .[] as $e
({}; . + {("k" + ($e.key | tostring)): ("$" + $e.value)})) as $gid | (if
$strat == "newest_id" then {"_id": -1} elif $strat == "newest_timestamp"
then {($ts): -1, "_id": 1} else {"_id": 1} end) as $sort | [{"$match":
$exist}, {"$sort": $sort}, {"$group": {"_id": $gid, "count": {"$sum": 1},
"survivor_id": {"$first": "$_id"}, "all_ids": {"$push": "$_id"}}},
{"$match": {"count": {"$gt": 1}}}, {"$set": {"surplus_ids": {"$filter":
{"input": "$all_ids", "as": "d", "cond": {"$ne": ["$$d",
"$survivor_id"]}}}}}, {"$facet": {"sample": [{"$project": {"_id": 0, "key":
"$_id", "copies": "$count", "survivor": {"$toString": "$survivor_id"},
"surplus": {"$size": "$surplus_ids"}}}, {"$sort": {"copies": -1}},
{"$limit": $sl}], "surplus": [{"$unwind": "$surplus_ids"}, {"$limit": $dl},
{"$group": {"_id": "all", "ids": {"$push": {"$toString":
"$surplus_ids"}}}}], "totals": [{"$group": {"_id": "all", "dup_keys":
{"$sum": 1}, "surplus_total": {"$sum": {"$size": "$surplus_ids"}}}}]}},
{"$project": {"dup_keys": {"$ifNull": [{"$arrayElemAt": ["$totals.dup_keys",
0]}, 0]}, "surplus_total": {"$ifNull": [{"$arrayElemAt":
["$totals.surplus_total", 0]}, 0]}, "surplus_ids": {"$ifNull":
[{"$arrayElemAt": ["$surplus.ids", 0]}, []]}, "sample": "$sample"}},
{"$set": {"surplus_targeted": {"$size": "$surplus_ids"}}}]') | first |
toJson }}
reverify_pipeline: >-
{{ {'bk': inputs.business_key} | jq('.bk as $bk | ($bk | split(",") |
map(gsub(" ";"")) | map(select(. != ""))) as $fields | {"$and": ($fields |
map({(.): {"$exists": true, "$ne": null}}))} as $exist | ($fields |
to_entries | reduce .[] as $e ({}; . + {("k" + ($e.key | tostring)): ("$" +
$e.value)})) as $gid | [{"$match": $exist}, {"$group": {"_id": $gid,
"count": {"$sum": 1}}}, {"$match": {"count": {"$gt": 1}}}, {"$facet":
{"dups": [{"$count": "n"}], "worst": [{"$sort": {"count": -1}}, {"$limit":
5}, {"$project": {"_id": 0, "key": "$_id", "copies": "$count"}}]}},
{"$project": {"remaining_dup_keys": {"$ifNull": [{"$arrayElemAt":
["$dups.n", 0]}, 0]}, "worst": "$worst"}}]') | first | toJson }}
delete_batches: >-
{{ {'ids': outputs.detect.rows[0].surplus_ids, 'n': inputs.batch_size} |
jq('.n as $n | [.ids | to_entries | group_by(.key / $n | floor) | .[] |
map({"$oid": .value}) | tojson]') | first | toJson }}
tasks:
- id: check_inputs
type: io.kestra.plugin.core.flow.If
description: >-
Refuse a newest_timestamp strategy with no timestamp_field and refuse a
batch_size or delete_limit below 1, before a single read. Each would make
the delete loop undefined.
condition: "{{ (inputs.survivor_strategy == 'newest_timestamp' and
inputs.timestamp_field == '') or inputs.batch_size < 1 or
inputs.delete_limit < 1 }}"
then:
- id: bad_inputs
type: io.kestra.plugin.core.execution.Fail
errorMessage: >-
Input check failed. survivor_strategy={{ inputs.survivor_strategy }}
timestamp_field={{ inputs.timestamp_field }} batch_size={{
inputs.batch_size }} delete_limit={{ inputs.delete_limit }}.
newest_timestamp needs a timestamp_field. batch_size and delete_limit
must each be at least 1. Nothing was read. Nothing was written.
- id: detect
type: io.kestra.plugin.mongodb.Aggregate
description: >-
Read-only. Groups the collection by the business key, keeps the chosen
survivor per key with a sort then a group first and marks every other copy
as surplus by its _id. One facet then returns the exact number of
duplicate keys, the exact surplus total, the capped list of surplus ids
the delete acts on and the worst scan_limit keys by copy count. The
survivor id is never in the surplus list, so a delete can never remove the
keeper.
connection:
uri: "{{ inputs.connection_uri != '' ? inputs.connection_uri :
secret('MONGODB_URI') }}"
database: "{{ inputs.database }}"
collection: "{{ inputs.collection }}"
pipeline: "{{ render(vars.detect_pipeline) }}"
store: FETCH
- id: report
type: io.kestra.plugin.core.log.Log
description: Log the counts. Task outputs are not in the execution JSON, so the
numbers the gates use are written here.
message: >-
Checked {{ inputs.database }}.{{ inputs.collection }} for documents
sharing business key {{ inputs.business_key }}. Duplicate keys: {{
outputs.detect.rows[0].dup_keys ?? 0 }}. Surplus copies in total: {{
outputs.detect.rows[0].surplus_total ?? 0 }}. Targeted this run: {{
outputs.detect.rows[0].surplus_targeted ?? 0 }} (delete_limit {{
inputs.delete_limit }}). Survivor kept per key by {{
inputs.survivor_strategy }}. Worst keys: {{ outputs.detect.rows[0].sample
?? 'none' }}.
- id: route
type: io.kestra.plugin.core.flow.If
description: A duplicate key means alert and consider the delete. A collection
with one document per key just logs and stops.
condition: "{{ (outputs.detect.rows[0].dup_keys ?? 0) > 0 }}"
then:
- id: alert
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{{ {'text': ('MongoDB dedup guard on ' ~ inputs.database ~ '.' ~ inputs.collection ~ ': ' ~ (outputs.detect.rows[0].dup_keys ?? 0) ~ ' business key(s) carried by more than one document on ' ~ inputs.business_key ~ ', ' ~ (outputs.detect.rows[0].surplus_total ?? 0) ~ ' surplus copy(s). Survivor kept by ' ~ inputs.survivor_strategy ~ '. Execution ' ~ execution.id ~ '.')} | toJson }}
- id: planned
type: io.kestra.plugin.core.log.Log
description: What a remediation run would do, printed on every run including a
dry one.
message: >-
A dedup would keep one document per key by {{ inputs.survivor_strategy
}} and delete {{ outputs.detect.rows[0].surplus_targeted ?? 0 }}
surplus copy(s) in batches of {{ inputs.batch_size }}. dry_run={{
inputs.dry_run }} auto_remediate={{ inputs.auto_remediate }}. A delete
runs only when auto_remediate is true and dry_run is false and a
reviewer approves the pause.
- id: remediate_gate
type: io.kestra.plugin.core.flow.If
description: The delete opens only when auto_remediate is true and dry_run is false.
condition: "{{ inputs.auto_remediate and not inputs.dry_run }}"
then:
- id: hold
type: io.kestra.plugin.core.flow.Pause
description: >-
The delete is destructive, so a human answers first. With no
answer nothing is deleted and the findings stay open.
pauseDuration: "{{ inputs.approval_timeout }}"
onResume:
- id: approved
type: BOOL
displayName: Delete the surplus duplicate documents?
defaults: false
- id: note
type: STRING
displayName: Decision note
defaults: ""
- id: apply
type: io.kestra.plugin.core.flow.If
condition: "{{ outputs.hold.onResume.approved ?? false }}"
then:
- id: delete_branch
type: io.kestra.plugin.core.flow.If
description: Only open the write path when the detection found a surplus to
remove.
condition: "{{ (outputs.detect.rows[0].surplus_targeted ?? 0) > 0 }}"
then:
- id: remediate
type: io.kestra.plugin.core.flow.Loop
description: >-
One batch of surplus ids per iteration. concurrencyLimit 1
keeps the batches sequential, because parallel deletes on
the same collection race for no gain.
concurrencyLimit: 1
values: "{{ render(vars.delete_batches) }}"
tasks:
- id: purge
type: io.kestra.plugin.mongodb.Delete
description: >-
One DELETE_MANY per batch, filtered on the surplus _id
list only. It never filters on the business key, which
would match the survivor too, so the keeper is safe by
construction.
connection:
uri: "{{ inputs.connection_uri != '' ? inputs.connection_uri :
secret('MONGODB_URI') }}"
database: "{{ inputs.database }}"
collection: "{{ inputs.collection }}"
operation: DELETE_MANY
filter: "{{ {'ids': item.value} | jq('{\"_id\": {\"$in\": (.ids | fromjson)}}')
| first | toJson }}"
- id: log_deleted
type: io.kestra.plugin.core.log.Log
message: >-
Delete loop finished for {{
outputs.detect.rows[0].surplus_targeted ?? 0 }} surplus
copy(s) across {{ outputs.detect.rows[0].dup_keys ?? 0 }}
key(s). The re-group below is the proof, not this count.
- id: reverify
type: io.kestra.plugin.mongodb.Aggregate
description: >-
Read-only. Groups the whole collection by the business key
again and counts how many keys still carry more than one
document, collection wide, so a copy a writer inserted
while the run was paused is caught rather than hidden.
connection:
uri: "{{ inputs.connection_uri != '' ? inputs.connection_uri :
secret('MONGODB_URI') }}"
database: "{{ inputs.database }}"
collection: "{{ inputs.collection }}"
pipeline: "{{ render(vars.reverify_pipeline) }}"
store: FETCH
- id: log_reverify
type: io.kestra.plugin.core.log.Log
message: >-
Re-check after the delete found {{
outputs.reverify.rows[0].remaining_dup_keys ?? 0 }}
business key(s) still carried by more than one document.
Worst remaining: {{ outputs.reverify.rows[0].worst ??
'none' }}.
- id: verify_gate
type: io.kestra.plugin.core.flow.If
description: Fail the run if any business key still has more than one document
after the delete.
condition: "{{ (outputs.reverify.rows[0].remaining_dup_keys ?? 0) > 0 }}"
then:
- id: remediation_failed
type: io.kestra.plugin.core.execution.Fail
errorMessage: >-
The delete ran but the re-group still finds {{
outputs.reverify.rows[0].remaining_dup_keys ?? 0 }}
business key(s) in {{ inputs.database }}.{{
inputs.collection }} carried by more than one
document. The surplus was larger than delete_limit {{
inputs.delete_limit }} or a writer inserted another
copy during the run. The key is still not unique.
else:
- id: remediated
type: io.kestra.plugin.core.log.Log
message: >-
Removed the surplus for {{
outputs.detect.rows[0].dup_keys ?? 0 }} key(s) in {{
inputs.database }}.{{ inputs.collection }}. The
re-group finds {{
outputs.reverify.rows[0].remaining_dup_keys ?? 0 }}
duplicate key(s) left, so every grouped key now
resolves to a single document.
else:
- id: nothing_to_delete
type: io.kestra.plugin.core.log.Log
message: >-
{{ outputs.detect.rows[0].dup_keys ?? 0 }} duplicate
key(s) were reported but the targeted surplus is 0, so
nothing was deleted. Raise delete_limit if a cap held the
surplus back.
else:
- id: declined
type: io.kestra.plugin.core.log.Log
message: >-
Approval not granted{{ (outputs.hold.onResume.note ?? '') ==
'' ? '' : (', note: ' ~ outputs.hold.onResume.note) }}. No
document was deleted. The duplicate copies stay in place.
else:
- id: clean
type: io.kestra.plugin.core.log.Log
message: >-
No duplicates. Every business key {{ inputs.business_key }} in {{
inputs.database }}.{{ inputs.collection }} is carried by a single
document.
errors:
- id: alert_flow_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{{ {'text': ('MongoDB dedup guard failed on ' ~ inputs.database ~ '.' ~ inputs.collection ~ ' (execution ' ~ execution.id ~ '). Check the logs.')} | toJson }}
triggers:
- id: daily
type: io.kestra.plugin.core.trigger.Schedule
description: >-
Shipped disabled. Its inputs force safe mode, so even an enabled schedule
only ever reports and never deletes on its own.
cron: "0 2 * * *"
disabled: true
inputs:
dry_run: true
auto_remediate: false