An On-Call Checklist for MongoDB Incidents
A good MongoDB incident checklist goes in this order: confirm the replica set has a healthy primary, look at what is running right now, check connections, check replication lag, check disk space, and only then look for missing indexes. Each step takes one or two commands in mongosh, and each one rules out a whole class of causes. This article walks through them, explains what to read from the output, and ends with what you can realistically verify from a phone while you are away from your desk.
Run the commands with a user that has the privileges they need (the clusterMonitor role covers most diagnostic commands). If you can, avoid diagnosing as a user that can write: an incident is the worst moment for a stray deleteMany.
1. Is the replica set healthy?
Start with topology. Many “MongoDB is down” reports are really “the application cannot find a primary”.
rs.status().members.map(m => ({
name: m.name,
state: m.stateStr,
health: m.health,
uptime: m.uptime,
lastHeartbeatMessage: m.lastHeartbeatMessage
}))
What to look for:
- Exactly one member in
PRIMARY. Zero means an election is in progress or no majority is reachable. - Members in
RECOVERING,STARTUP2or withhealth: 0. - A small
uptimeon the primary, which suggests a recent restart or failover. - A heartbeat message mentioning connection or authentication failures between members.
rs.conf() shows priorities and votes. A member with priority: 0 explains why it never becomes primary. On Atlas the cluster overview shows the same information, but rs.status() gives you exact state names and heartbeat messages. On a sharded cluster, check each shard’s replica set and also sh.status().
2. What is running right now?
If the topology is fine but things are slow, find the operations hogging the server. db.currentOp() accepts a filter document, which is far more useful than dumping everything:
db.currentOp({
active: true,
secs_running: { $gt: 5 },
ns: { $not: /^(admin|local|config)\./ }
})
The fields worth reading are op (query, update, command, getmore), ns, secs_running, planSummary (a COLLSCAN here is a strong hint), numYields, waitingForLock and command, which holds the actual filter or pipeline.
Two patterns matter most:
- One long-running operation, often a collection scan or an unbounded aggregation. If you are sure it is safe to stop, terminate it with
db.killOp(<opid>). Do not kill replication, index builds or oplog operations without understanding them first. - Many identical operations with the same
planSummary: a hot query that lost its index, or a retry storm from an application.
Operations that are waiting rather than working show up with:
db.currentOp({ waitingForLock: true })
Lots of waiting operations point at lock contention or a saturated storage engine rather than at one bad query.
Use the profiler for a short window
currentOp only shows what is running at the moment you ask. To catch slow operations that come and go, use the database profiler on the affected database:
db.setProfilerLevel(1, { slowms: 100 })
db.system.profile.find({ millis: { $gt: 100 } })
.sort({ ts: -1 })
.limit(10)
Level 1 records operations slower than slowms; level 2 records everything and should be avoided on a busy production system. Profiling has a cost, so switch it off with db.setProfilerLevel(0) when you are done. Profiler availability can differ on some managed tiers; if the command is rejected, use the slow query tooling your provider offers or the slow operation entries in the server log.
3. Are you running out of connections?
Connection exhaustion looks like timeouts and pool-wait errors, often right after a deploy that changed pool sizes or started creating clients per request.
db.serverStatus().connections
{ current: 812, available: 188, totalCreated: 10234, active: 41 }
If available is close to zero you are near the limit. The limit depends on operating system settings for self-hosted servers and on the cluster tier for managed ones, so check yours rather than assuming a number. To see who owns the connections, run this against the admin database:
db.getSiblingDB("admin").aggregate([
{ $currentOp: { allUsers: true, idleConnections: true } },
{ $group: { _id: "$client", n: { $sum: 1 } } },
{ $sort: { n: -1 } },
{ $limit: 10 }
])
One client address holding hundreds of connections usually means an application that creates a new MongoClient per request instead of sharing one for the life of the process.
4. Replication lag
A secondary that falls behind serves stale reads and, if it falls far enough, drops off the oplog window and needs a resync.
rs.printSecondaryReplicationInfo()
On older shells the equivalent is rs.printSlaveReplicationInfo(). It prints how far each secondary is behind the primary. To see how much room you have before a lagging member falls off the end of the oplog:
rs.printReplicationInfo()
The output shows the oplog size and the time range it covers. If the window is one hour and a secondary is 50 minutes behind, act now; if the window is a day, you have time.
Common causes of lag are a heavy write burst, a secondary with slower disks than the primary, an index build on the secondary, or a long-running query starving the replication threads. If your application reads from secondaries, lag is also a correctness issue, so check the read preference configured in your driver.
5. Disk space and cache pressure
A full disk stops writes and can crash the server. Check it early.
db.stats(1024 * 1024 * 1024) // sizes in GiB
Compare dataSize, storageSize and indexSize; on self-hosted servers fsUsedSize against fsTotalSize shows the filesystem. Then find the biggest collections:
db.getCollectionNames()
.map(n => ({ n, s: db.getCollection(n).stats().storageSize }))
.sort((a, b) => b.s - a.s)
.slice(0, 5)
Deleting documents does not necessarily return space to the operating system; the space becomes reusable inside the storage engine. Also look at db.serverStatus().wiredTiger.cache, which reports the bytes in the cache and the configured maximum. A cache pinned at its limit together with heavy disk reads means the working set does not fit in memory, and no query tweak will fix that quickly.
6. Missing or unused indexes
When a specific query is slow, run it with execution statistics:
db.orders.find({ customerId: 42, status: "open" })
.sort({ createdAt: -1 })
.explain("executionStats")
Compare totalDocsExamined with nReturned. If the server examined two million documents to return 20, read the winning plan: COLLSCAN means no usable index, and an in-memory SORT stage means the index does not satisfy the sort. A compound index that orders equality fields first, then the sort field, usually fixes both:
db.orders.createIndex({ customerId: 1, status: 1, createdAt: -1 })
Building an index on a large production collection adds load, so do it deliberately and watch currentOp while it runs. To find indexes that cost writes but are never read, use db.orders.aggregate([{ $indexStats: {} }]) and compare accesses.ops, remembering that the counters reset when the server restarts.
What you can verify from your phone
You are not always at a laptop when the page arrives. A phone is not the place to build indexes or kill operations, but it is enough to answer the questions that decide whether you need to get up:
- Can I still connect, and how fast does it respond? A timeout versus an authentication error already narrows the cause. If the failure is a timeout from a new network, check the IP access list first.
- Is data still being written? Sort a collection by
_idor a timestamp field descending and look at the newest document. - Is the damage what the alert says? Count documents in a bad state with a filter, or run a small
$groupby status.
Bussola runs these as ordinary filters and aggregations directly from an iPhone. It has no index management, and it does not replace rs.status() or currentOp for diagnosis. Whatever tool you use, triage with a read-only user; the reasoning is in How to Safely Query a Production MongoDB From Your Phone.
Checklist
| Step | Command | Red flag |
|---|---|---|
| Topology | rs.status() |
No primary, unreachable member, recent restart |
| Running work | db.currentOp({ active: true, secs_running: { $gt: 5 } }) |
COLLSCAN, many identical operations |
| Waiting work | db.currentOp({ waitingForLock: true }) |
Many operations waiting |
| Connections | db.serverStatus().connections |
available near zero |
| Lag | rs.printSecondaryReplicationInfo() |
Lag approaching the oplog window |
| Disk | db.stats() and OS tools |
Filesystem nearly full |
| Slow queries | profiler, explain("executionStats") |
Docs examined far above docs returned |
Write down what you ran and what you saw as you go. The timeline you build during the incident is most of the postmortem.
Bussola is a free MongoDB client for iPhone: query, edit and aggregate over a direct connection, with no backend.
Coming soon to the App Store