Managing Long-Running Operations in MongoDB®

2 min read
Managing Long-Running Operations in MongoDB®

SHARE THIS ARTICLE

MongoDB® gives you a number of tools to manage long-running operations in the system. It’s extremely important to keep track of operations running on your production server at any point of time, as in some cases, you might have rouge queries or index builds that are killing the performance of your server.

The MongoDB command which gives you this information is “db.currentOp()”. For more information refer to the MongoDB documentation for db.currentOp(). The command takes parameters to restrict the output by either db, operation, execution time etc.

Here is an example output:

{
  opid: 294,
  active: false,
  op: "query",
  ns: "admin",
  query: { "query": {}, orderby: { "$natural": -1 } },
  client: "0.0.0.0:0",
  desc: "rsMgr",
  threadId: "0x7f3e6af3f700",
  waitingForLock: false,
  numYields: 0,
  lockStats: { timeLockedMicros: { R: 1, W: 1 },
  timeAcquiringMicros: { r: 1, w: 1 }
}

The most interesting fields are:

  1. opid – ID of the operation.
  2. op – The operation that is executing.
  3. ns – The database and collection the operation is executing on.
  4. secs_running – The number of seconds the operation has been running.

Once you identify the long-running operations, you might want to terminate them in some cases. The operation you want to use is:

db.killOp(<opid>)

It goes without saying that you want to use this command very carefully. Do not terminate operations you do not know about. I personally only feel comfortable terminating long-running queries.

If you have operations that are consistently taking a long time, MongoDB provides another option – maxTimeMS:

E.g. db.find(...).maxTimeMS(30)

If you know you have long-running operations that you want to stop after a certain amount of execution time has exceeded, use the maxTimeMS option to set a limit on the execution time of this op.

At ScaleGrid, we understand the importance of this workflow – so we have built it into our admin console. The ‘admin’ console on the cluster details page provides access to the operations list on your MongoDB server:

MongoDB - long running ops

You can also select a particular operation and choose to terminate it. As always exercise this option with discretion:

Terminate long running operations in MongoDB

For more information, please visit www.scalegrid.io. Connect with ScaleGrid on LinkedIn, X, Facebook, and YouTube.
Table of Contents

Stay Ahead with ScaleGrid Insights

Dive into the world of database management with our monthly newsletter. Get expert tips, in-depth articles, and the latest news, directly to your inbox.

Related Posts

high available cluster

High Availability Clustering & Why You Need It

High availability clustering keeps your IT systems running without interruptions, even amid failures. This guide details high availability clustering, its...

blog-feature-img_whats-new-at-scalegrid

What’s New at ScaleGrid – July 2024

ScaleGrid is excited to announce our latest platform updates, showcasing our unwavering commitment to security, usability, and performance. Our recent...

database backend

What is RabbitMQ Used For

RabbitMQ is an open-source message broker facilitating the connection between different applications within a distributed setup. It is widely utilized...

NEWS

Add Headline Here