Change the Size of the Oplog

On this page

The oplog exists internally as a capped collection, so you cannot modify its size in the course of normal operations. In most cases the default oplog size is an acceptable size; however, in some situations you may need a larger or smaller oplog. For example, you might need to change the oplog size if your applications perform large numbers of multi-updates or deletes in short periods of time.

This tutorial describes how to resize the oplog. For a detailed explanation of oplog sizing, see Oplog Size. For details how oplog size affects delayed members and affects replication lag, see Delayed Replica Set Members.


To change the size of the oplog, you must perform maintenance on each member of the replica set in turn. The procedure requires: stopping the mongod instance and starting as a standalone instance, modifying the oplog size, and restarting the member.


Always start rolling replica set maintenance with the secondaries, and finish with the maintenance on primary member.


  • Restart the member in standalone mode.


    Always use rs.stepDown() to force the primary to become a secondary, before stopping the server. This facilitates a more efficient election process.

  • Recreate the oplog with the new size and with an old oplog entry as a seed.

  • Restart the mongod instance as a member of the replica set.

Restart a Secondary in Standalone Mode on a Different Port

Shut down the mongod instance for one of the non-primary members of your replica set. For example, to shut down, use the db.shutdownServer() method:


Restart this mongod as a standalone instance running on a different port and without the --replSet parameter. Use a command similar to the following:

mongod --port 37017 --dbpath /srv/mongodb --bind_ip localhost,<ip address of the mongod host>

Create a Backup of the Oplog (Optional)

Optionally, backup the existing oplog on the standalone instance, as in the following example:

mongodump --db local --collection '' --port 37017

Recreate the Oplog with a New Size and a Seed Entry

Save the last entry from the oplog. For example, connect to the instance using the mongo shell, and enter the following command to switch to the local database:

use local

In mongo shell scripts you can use the following operation to set the db object:

db = db.getSiblingDB('local')

Ensure that the temp temporary collection is empty by dropping the collection:


Use the method and a sort on reverse natural order to find the last entry and save it to a temporary collection: { }, { ts: 1, h: 1 } ).sort( {$natural : -1} ).limit(1).next() )

To see this oplog entry, use the following operation:


Remove the Existing Oplog Collection

Drop the old collection in the local database. Use the following command:

db = db.getSiblingDB('local')

This returns true in the shell.

Create a New Oplog

Use the create command to create a new oplog of a different size. Specify the size argument in bytes. A value of 2 * 1024 * 1024 * 1024 will create a new oplog that’s 2 gigabytes:

db.runCommand( { create: "", capped: true, size: (2 * 1024 * 1024 * 1024) } )

Upon success, this command returns the following status:

{ "ok" : 1 }

Insert the Last Entry of the Old Oplog into the New Oplog

Insert the previously saved last entry from the old oplog into the new oplog. For example: db.temp.findOne() )

To confirm the entry is in the new oplog, use the following operation:

Restart the Member

Stop the server. For example, from the mongo shell, you can issue the following:

use admin

Restart the mongod as a member of the replica set on its usual port. From the system prompt (not the mongo shell):

mongod --replSet rs0 --dbpath /srv/mongodb --bind_ip localhost,<ip address of the mongod host>

The replica set member will recover and “catch up” before it is eligible for election to primary.

Repeat Process for all Members that may become Primary

Repeat this procedure for all members you want to change the size of the oplog. Repeat the procedure for the primary as part of the following step.

Change the Size of the Oplog on the Primary

To finish the rolling maintenance operation, step down the primary with the rs.stepDown() method and repeat the oplog resizing procedure above.