Script icon
If icon
SlackIncomingWebhook icon
Log icon
Labels icon
Schedule icon

MongoDB Connection Spike and Exhaustion Guard

Audits MongoDB replica set active connections against configured capacity, alerting DBAs on Slack before connection exhaustion rejects clients.

Categories
DataInfrastructure

This blueprint acts as an automated connection capacity guardian for MongoDB database clusters. Scheduled every 10 minutes, it inspects serverStatus().connections telemetry, computes active-to-total connection utilization percentages, and alerts database administrators on Slack before connection spikes breach maxIncomingConnections ceilings and reject incoming microservice client requests.

How it works

  1. Recurring 10-Minute Probe: The periodic_connection_probe trigger (io.kestra.plugin.core.trigger.Schedule) fires every 10 minutes.
  2. MongoDB serverStatus Query: The audit_mongodb_connections task (io.kestra.plugin.scripts.python.Script) connects to MongoDB via PyMongo, runs the administrative serverStatus command, and extracts active and available connection slots.
  3. Threshold Gate: The check_connection_saturation flowable task (io.kestra.plugin.core.flow.If) branches based on whether connection utilization reached the warning percentage.
  4. Slack Alert Dispatch: When connection saturation is detected, notify_slack_dba (io.kestra.plugin.slack.notifications.SlackIncomingWebhook) dispatches an urgent incident card with connection metrics.
  5. Nominal Confirmation: When connection capacity remains healthy, log_connections_nominal records confirmed status.
  6. Manifest Export: The export_connection_manifest task tags the execution with searchable status labels.

Architecture diagram

flowchart TD
    A[Schedule: Every 10 Minutes] --> B[audit_mongodb_connections: MongoDB serverStatus]
    B --> C{Connection Utilization > Threshold?}
    C -- Yes --> D[notify_slack_dba: Slack Webhook]
    C -- No --> E[log_connections_nominal: Log]
    D --> F[export_connection_manifest: Execution Labels]
    E --> F

Use cases

  • Prevent Cascading Client Connection Failures: Catch connection leaks from microservices that create new client instances per request without reusing pools.
  • Capacity Planning for Autoscaling Workloads: Identify when Kubernetes horizontal pod autoscalers deploy excess application replicas that together exceed the database's net.maxIncomingConnections.
  • Detect Dangling Unclosed Cursors: Spot unclosed cursors that hold open server sockets and pin worker threads in the mongod engine.

Inputs

Name Type Default Description
mongodb_connection_uri STRING mongodb://mongodb-primary.internal:27017 MongoDB connection string pointing to the primary replica set node.
max_connection_utilization_percent_warning FLOAT 80.0 Connection utilization percentage threshold above which a saturation warning is triggered.
slack_channel STRING #mongodb-dba-alerts Slack channel destination for MongoDB connection capacity alerts.

Expected outputs

  • {{ outputs.audit_mongodb_connections.vars.current_connections }}: Count of active incoming connections.
  • {{ outputs.audit_mongodb_connections.vars.available_connections }}: Remaining available connection slots before rejection.
  • {{ outputs.audit_mongodb_connections.vars.total_capacity }}: Total configured connection ceiling.
  • {{ outputs.audit_mongodb_connections.vars.utilization_percent }}: Percentage of connection capacity consumed.
  • {{ outputs.audit_mongodb_connections.outputFiles['mongodb_connections_report.json'] }}: Full JSON connection health report.

Prerequisites

  • MongoDB user credentials with clusterMonitor or read privileges on the admin database.
  • Incoming Webhook URL configured for your database administrator Slack channel.

Secrets

  • MONGODB_USER: MongoDB authentication username.
  • MONGODB_PASSWORD: MongoDB authentication password.
  • SLACK_WEBHOOK_URL: Slack Incoming Webhook endpoint URL.

Quick start

  1. Configure MONGODB_USER, MONGODB_PASSWORD, and SLACK_WEBHOOK_URL in your Kestra namespace secrets.
  2. Import this flow YAML into your Kestra workspace.
  3. Specify your MongoDB connection string in mongodb_connection_uri.
  4. Click Execute in the UI to perform an immediate connection capacity probe.

Common pitfalls and troubleshooting

  • Connection Ceiling Configuration: By default, MongoDB sets net.maxIncomingConnections to 65,536, but system file descriptor limits (ulimit -n) often constrain practical connection capacity much lower (e.g. 1,024 or 4,096); verify operating system file descriptor limits.
  • Replica Set Routing: Connection capacity probes should be executed against the active primary where write-heavy client applications maintain pooled connections.

How to extend

  • Query currentOp() to identify which client IP addresses and usernames account for the highest share of active connections.
  • Dynamically notify client service teams via email or PagerDuty when connection pools remain above 90% for consecutive intervals.
  • Export connection count histograms to Prometheus or Datadog.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.