Home » MongoDB Oplog

MongoDB Oplog

  • by
MongoDB Oplog

1. Overview

In this article, we will learn more about the Oplog of MongoDB. MongoDB achieves replication and high availability using replica sets.

replica set in MongoDB is a group of mongod processes that maintain the same data set.

2. MongoDB Replication using Oplog

MongoDB contains one primary and multiple secondary nodes. Usually, the primary node receives all write operations.

  1. The primary records all changes to its data sets in its operation log, i.e. oplog.
  2. The secondaries replicate the primary’s oplog and apply the operations to their data sets such that the secondary data sets reflect the primary’s data set. 

Starting in MongoDB 5.0, you can’t perform manual write operations to the oplog on a cluster running as a replica set. 

3. MongoDB Oplog

The oplog (operations log) is a special capped collection that keeps a rolling record of all operations that change the data stored in your databases.

A capped collection is a fixed-sized collection that automatically overwrites its oldest entries when it reaches its maximum size.

3.1. MongoDB Oplog entry

Below is a simple example of Oplog entry:

    "ts" : Timestamp(1374832131, 1),
    "h" : NumberLong("-1336944105039123379"),
    "v" : 2,
    "op" : "i",
    "ns" : "users.users",
    "o" : {
            "_id" : ObjectId("51f24603d98de3716b1db672")
  1. ts: the timestamp when this operation occurred.
  2. h: a unique Id for this operation. Each operation will have a different value in this field.
  3. op: The type of the operation that has values for the operations: Insert: “i”, Delete: “d”, No operation: “n”, update: “u”, Global index key: “xi”, command: “c”
  4. ns: the database and collection affected by this operation. Since this is a no-op, this field is left blank.
  5. o: the actual document representing the op.

All replica set members contain a copy of the oplog, in the local.oplog.rs collection, which allows them to maintain the current state of the database.

Each operation in the oplog is idempotent. That is, oplog operations produce the same results whether applied once or multiple times to the target dataset.

By default, MongoDB does not set a minimum oplog retention period and automatically truncates the oplog starting with the oldest entries to maintain the configured maximum oplog size. However, you can set a minimum oplog retention period.

4. Oplog Size

Before mongod creates an oplog, you can specify its size with the oplogSizeMB option. Once you have started a replica set member for the first time, use the replSetResizeOplog administrative command to change the oplog size. replSetResizeOplog enables you to resize the oplog dynamically without restarting the mongod process.

If your application predominantly performs reads with a minimal amount of write operations, a smaller oplog may be sufficient.

The following workloads might require a larger oplog size.

4.1. Updates to Multiple Documents at Once

The oplog must translate multi-updates into individual operations in order to maintain idempotency.

This can use a great deal of oplog space without a corresponding increase in data size or disk use.

4.2. Deletions

If you delete roughly the same amount of data as you insert, the database will not grow significantly in disk use, but the size of the operation log can be quite large.

4.3. Updates

If a significant portion of the workload is updates that do not increase the size of the documents, the database records numerous operations but does not change the quantity of data on disk.

5. Conclusion

To sum up, we have learned the basics of MongoDB Oplog.

Leave a Reply

Your email address will not be published. Required fields are marked *