Runbook

MongoDB Efficiency of queries is degrading

Back to Runbooks

Overview

This incident type describes an issue where the efficiency of queries in MongoDB is degrading. The incident may be triggered by an alert monitoring system that detects an anomaly in the performance of queries executed in the database. The incident may result in a decrease in system performance and may require the attention of a software engineer to troubleshoot and resolve the issue.

Parameters

1# Environment Variables
2
3export COLLECTION="PLACEHOLDER"
4
5export DATABASE="PLACEHOLDER"
6
7export MONGODB_HOST="PLACEHOLDER"
8
9export MONGODB_USER="PLACEHOLDER"
10
11export MONGODB_PORT="PLACEHOLDER"
12
13export MONGODB_PASSWORD="PLACEHOLDER"

Debug

First, let's check if the MongoDB instance is running

systemctl status mongodb

If the instance is running, check the logs for any errors

journalctl -u mongodb --since "10 minutes ago"

Check if the MongoDB queries are taking longer than usual (replace <database> and <collection> with the relevant names)

mongo ${DATABASE} --eval "db.${COLLECTION}.find().explain('executionStats')"

Check the disk usage of the MongoDB instance

df -h /var/lib/mongodb

Check the network connectivity between the MongoDB instance and the application server

ping ${MONGODB_HOST}

Check the CPU and memory usage of the application server

top

Repair

Define variables

1MONGODB_HOST=${MONGODB_HOST}
2
3MONGODB_PORT=${MONGODB_PORT}
4
5MONGODB_USER=${MONGODB_USER}
6
7MONGODB_PASSWORD=${MONGODB_PASSWORD}

Check for issues or errors in the MongoDB database

mongo --host $MONGODB_HOST --port $MONGODB_PORT --username $MONGODB_USER --password $MONGODB_PASSWORD --eval "db.runCommand({checkDatabase: 1})"

Optimize indexes

mongo --host $MONGODB_HOST --port $MONGODB_PORT --username $MONGODB_USER --password $MONGODB_PASSWORD --eval "db.adminCommand({reIndex: ${COLLECTION}})"

Run database repairs

mongo --host $MONGODB_HOST --port $MONGODB_PORT --username $MONGODB_USER --password $MONGODB_PASSWORD --eval "db.adminCommand({repairDatabase: 1})"

Learn more

Related Runbooks

Check out these related runbooks to help you debug and resolve similar issues.