skip to content

MongoDB API calls turn slow right now: how do you use `serverStatus`, `$currentOp` and `killOp` to find and stop the cause, and what can go wrong?

level: seniorimportance: should knowfreq 44%

answer

  1. shape of the load, then the culprit
  2. counters only mean something as differences
  3. filter by seconds running
  4. marked for death, not dead
  5. client operations only

basics

~20 s

Sample serverStatus twice to see the load, list live work with $currentOp and allUsers, then kill a confirmed client operation with killOp. The kill is only a mark that takes effect at a safe point, so verify it.

solid answer

~40 s

I start wide. Two `serverStatus` samples a minute apart show whether connections are near the limit, whether `opcounters` jumped, whether `opLatencies` moved, and whether operations are persistently queued in `queues.execution`. Then I narrow with `$currentOp` on the `admin` database, `allUsers: true`, filtered on `secs_running`, and read `ns`, `planSummary`, `waitingForLock` and `appName`. If one client operation is clearly the cause, `db.killOp(opid)` stops it, but the reply only means it was marked, so I confirm through `killPending` and that it is gone. The risks: killing an internal operation, killing an index build on a replica set instead of using `dropIndexes`, assuming a killed multi-document write rolls back when it does not, and on a `mongos` assuming a kill reaches shard writes when it does not.

code

javascript · 10 lines
javascript
// Every user's operations running for more than 5 seconds
db.getSiblingDB("admin").aggregate([
  { $currentOp: { allUsers: true } },
  { $match: { secs_running: { $gt: 5 } } },
  { $project: { opid: 1, op: 1, ns: 1, secs_running: 1,
                planSummary: 1, waitingForLock: 1, appName: 1 } }
])

// After confirming it is a client operation. Check killPending next.
db.killOp(3478)

go deeper

for a junior

Recall that $currentOp lists what is running now, that serverStatus reports instance-wide numbers, and that killOp stops an operation by its opid.

for a middle

Explain why serverStatus counters must be compared over time, which $currentOp fields identify a culprit, and which privilege actions allUsers and killing another user's operation need.

for a senior

Show the order: load shape, live operations, then a targeted kill. Separate victims from blockers, confirm the kill took effect, and handle mongos writes through killSessions or shard-qualified opids.

for a principal

Pre-decide who may kill what during an incident and what gets written down afterwards, since a kill treats a symptom and has no undo for a multi-document write.

## Work from wide to narrow When calls are slow *now*, the order matters, because each tool answers a different question. | Question | Tool | Why this one | |---|---|---| | Is the server overloaded, and how? | `serverStatus`, sampled twice | Instance-wide counters and gauges | | Which collection is busy? | `mongotop` | Time per namespace, on one `mongod` | | What exactly is running at this moment? | `$currentOp` | One document per live operation | | Can I stop it? | `killOp` | Marks a chosen operation | | What was slow over the last hour? | Slow-query log or `system.profile` | History, which the others lack | | Why was that query slow? | `explain()` | One plan, a separate skill | ## `serverStatus`: which sections first Most `serverStatus` values are **cumulative since the process started** and reset on restart, so one sample tells you little. Take two and compare them. A sensible reading order: 1. **`connections`**: `current` against `available`. A rise here often points at the client tier, not at a query. 2. **`opcounters`**: did volume change? Bulk and multi-document writes count as one operation, and the counters increment even for failures. 3. **`opLatencies`**: are `reads`, `writes` or `commands` slower for the instance as a whole? 4. **`queues.execution`**: is work waiting for storage-layer execution? Since 7.0, ticket counts adjust dynamically. The manual is explicit that zero available tickets for a long time **does not** indicate overload, while a large number of queued operations held for a long time likely does. 5. **`metrics.queryExecutor`**: `scanned` and `scannedObjects`, and `collectionScans`. A jump suggests a query started doing far more work. 6. **`metrics.operation`**: `scanAndOrder` for sorts that could not use an index, and `writeConflicts` for write contention. The output is large, so project it: `db.runCommand({ serverStatus: 1, repl: 0, locks: 0 })`, or read one field off the result. From 8.3 you can also pass `none: 1` to start from nothing and add sections. The WiredTiger cache figures live here too; interpreting them is its own topic. ## `$currentOp`: find the culprit `$currentOp` is an aggregation stage. It must be the first stage and runs on the `admin` database. The old `currentOp` command is deprecated since 6.2. In `mongosh`, `db.currentOp()` uses the stage underneath. Options that matter: - **`allUsers: true`** shows everyone's operations; this requires the `inprog` privilege action. Without it, you only see your own. - **`idleConnections: true`** adds idle connections; **`idleSessions`**, on by default, adds inactive sessions holding locks inside transactions. - **`localOps: true`** on a `mongos` reports what runs on that `mongos` rather than on the shards. Useful output fields: `opid`, `op`, `ns`, `command`, `secs_running`, `planSummary` (a `COLLSCAN` here is a strong hint), `waitingForLock`, `numYields`, `client`, `appName`, `effectiveUsers` and `killPending`. An operation with a long `secs_running` and `waitingForLock: true` is usually a victim. Look for the operation it is waiting on, and kill that one if any. ## `killOp`: what it does and does not do `db.killOp(opid)` wraps the `killOp` command on `admin`. Rules to hold on to: - **It marks the operation.** `{ info: "attempting to kill op", ok: 1 }` is returned once the authorization check passes, whether or not the operation has stopped. The operation ends at its next safe point, so confirm with `$currentOp`. - **Clients only.** The manual says to kill only client-initiated operations and to leave internal database operations alone. - **Not for index builds** on replica sets or sharded clusters: use `dropIndexes` on the primary. - **There is no undo.** An `updateMany` outside a transaction is not atomic as a whole, so documents it already changed stay changed. - **Authorization.** Killing someone else's operation requires the `killop` action on the cluster. On a `mongod`, users can kill their own without it. ## Sharded clusters This is where a confident kill goes wrong: - **Reads:** from the **same** `mongos` the client used, find the op with `$currentOp` and `localOps: true`, then `db.killOp` there. It can stop a query spanning shards. - **Writes:** `killOp` issued on a `mongos` does not propagate to shards for a write operation. If the write belongs to a session, find the `lsid` and run `killSessions` from the `mongos`. Otherwise run `$currentOp` from the `mongos`, which gives shard-qualified ids such as `shardB:79014`, and pass each id for every shard involved to `db.killOp` on the `mongos`. A kill stops the symptom. Afterwards, find what let it run so long, read its plan, and consider a `maxTimeMS` on that query.

  • `killOp` returned `ok: 1` but the operation is still in `$currentOp` a minute later. What now?
    That reply only means the operation was marked; the server reports success once the authorization check passes. Look for `killPending: true`, which means it is flagged and will stop at its next safe termination point. Check what it is doing there, such as a long wait or `numYields` not moving, and keep watching it rather than repeating the kill or restarting the server by reflex.
  • `serverStatus` shows no available execution tickets for several minutes. Is the server overloaded?
    Not by that alone on 7.0 or later. Ticket counts are adjusted dynamically, and the manual says prolonged zero availability does not indicate overload. The signal is a large number of queued operations that persists. Check the queue figures in `queues.execution` and compare with `opLatencies` before calling it overload.

  • killOp returning ok: 1 shows that the operation has stopped
  • Zero available execution tickets on 7.0 or later proves the server is overloaded
  • A single serverStatus sample tells you the current operation rate
  • A killed updateMany outside a transaction rolls back the documents it changed
  • killOp on a mongos always reaches writes running on the shards
  • The way to stop an index build on a replica set is killOp

Assumes MongoDB 9.0; queues.execution is the 8.0 name for what 7.0 called wiredTiger.concurrentTransactions, and the none: 1 projection arrived in 8.3, so an 8.0 server lacks it.