Mongodb The OpLog Explained
As I watch the mongodb mailing list and the IRC channel I have come to understand that there is a lack of understanding about the mongodb oplog. While most of this information can be found in the mongodb online documentation I figured I’d make a blog about it to make it more clear for some people.
What The Heck Is An OpLog
From the mongodb website “Replication uses an operation log (“oplog”) to store write operations. These operations replay asynchronously on other nodes.” Now you must be asking your self what the heck does that mean. Basically the OpLog is a collection (a collection can be thought of a table in a database) of write operations (updates, deletes, inserts) which are stored in order of operation, this allows other members of the replica set (cluster) to apply these changes to the local copy of their database.
How Big Is The OpLog?
This is a very good question. The OpLog is a capped collection which means it only stores a certain number of records after that number is reached the oldest record is dropped from the OpLog. By default 5% of the partition where the dbpath lives is reserved for the oplog, but this is a configurable option.
--oplogSize
Querying The OpLog
A common question that I see in the mongodb forums is “can I query the mongodb oplog” and the answer is most certainly yes. The oplog can be queried like any other collection. From the shell here is an example of how to query the oplog:
MongoDB shell version: 2.0.4 connecting to: mongodb:27017/test PRIMARY> use local PRIMARY> db.oplog.rs.find()
This will show you all of the items in the oplog.rs collection.
Can I use oplog to trigger another program? I mean as soon as a data operation is performed on mongodb, can I use Oplog to execute some program?
Yes you can do this. You can use multiple programming languages to watch the oplog and then trigger events based on items in the OpLog.
This is a great article.
I am trying to use the oplog with python to send changes to a RabbitMQ. Any suggestions.
Also please clarify this statement: Is oplog (replication onlu available with Mongo Enterprise Edition as opposed to the community edition?
Thanks