New to Kestra?
Use blueprints to kickstart your first workflows.
Automated database governance workflow to query MongoDB $indexStats, detect unused secondary indexes wasting RAM, and alert Slack.
In high-throughput MongoDB environments, secondary indexes accelerate query filters but introduce significant performance trade-offs. Each index must be synchronously updated during every document insert, update, and delete operation. Furthermore, indexes reside in the WiredTiger storage cache; unused or redundant indexes consume valuable RAM that should otherwise cache active working sets.
Over time, legacy indexes from deprecated features or experimental queries linger unnoticed, degrading write throughput and ballooning database backup sizes.
This blueprint establishes an automated index governance audit for MongoDB. Running weekly or on-demand, it executes the $indexStats aggregation pipeline across your target collection to inspect historical operational hits (accesses.ops). When secondary indexes with zero access hits are identified, it dispatches an actionable Slack notification detailing the index key pattern and the exact dropIndex() statement needed to reclaim memory.
weekly_index_audit trigger (io.kestra.plugin.core.trigger.Schedule) executes every Monday at 05:00 UTC or on-demand via the Kestra UI.audit_unused_indexes task (io.kestra.plugin.mongodb.Aggregate) runs $indexStats, excluding the mandatory primary _id_ index and filtering for indexes where accesses.ops is zero.evaluate_unused_indexes flowable task (io.kestra.plugin.core.flow.If) branches based on whether any unused indexes were discovered.notify_dba_slack (io.kestra.plugin.slack.notifications.SlackIncomingWebhook) broadcasts an alert with index key details, tracking start date, and the suggested dropIndex command.log_optimal_indexes records nominal status in execution logs.export_audit_manifest task records telemetry outputs for DBA compliance tracking.dropIndex commands directly in Slack for safe operator review.Conducting regular index audits manually across hundreds of collections is rarely performed consistently. Kestra provides declarative, serverless orchestration: it connects directly via native MongoDB drivers, schedules regular inspections, conditionally alerts via Slack, securely handles credentials, and provides an auditable execution history.
| Name | Type | Default | Description |
|---|---|---|---|
database |
STRING | production |
Target MongoDB database to audit. |
collection |
STRING | orders |
Target collection to inspect for unused indexes. |
max_allowed_accesses |
INT | 0 |
Maximum operational accesses below which an index is flagged as unused. |
slack_channel |
STRING | #dba-alerts |
Slack channel destination for index governance alerts. |
{{ outputs.audit_unused_indexes.rows }}: Array of unused index records containing name, key, accesses_count, and since.{{ outputs.audit_unused_indexes.size }}: Total count of secondary indexes flagged with zero accesses.{{ outputs.evaluate_unused_indexes }}: Result of conditional branch evaluation.{{ outputs.export_audit_manifest.value }}: Structured JSON telemetry manifest recording execution timestamp and status.$indexStats).MONGODB_URI: MongoDB connection URI (e.g. mongodb+srv://<user>:<password>@cluster.mongodb.net/?retryWrites=true&w=majority).SLACK_WEBHOOK_URL: Slack Incoming Webhook endpoint URL used for sending alert notifications.MONGODB_URI and SLACK_WEBHOOK_URL in your Kestra namespace secrets.$indexStats counters reset whenever mongod restarts. Run this audit on instances with significant uptime (e.g., at least 30 days) to prevent falsely flagging newly initialized indexes.unique: true) might receive few read hits but remain essential for data integrity constraints. Verify uniqueness requirements before executing dropIndex()._id_, as the primary index cannot and should not be removed.io.kestra.plugin.core.flow.Loop to loop through an array of collections and audit an entire database schema in one run.