Using tailable cursors on the MongoDB oplog for realtime changes
14 Jun 2014MongoDB provides the ability to invoke to retrieve cursors of data that are tailable.
We can exploit this functionality by using on the oplog to provide a trigger-like effect on the Mongo database so that we can respond to changes in real-time.
Using pymongo you can setup a connection to your mongo server’s oplog like so:
This constant feedback loop will just keep pumping results down the pipe as they’re seen. You can already see that having an oplog setup on your database is a requirement of this solution. Without this, we have no way to measure the transactions that have executed.
The dictionary tail_opts
is passed as the second argument to the find
call. You can see that there are a couple of flags set here. The first one is tailable
. tailable
tells mongo that we want new results as they appear in scope of the cursor. await_data
is another option that is set on the cursor to get the server to wait for data as it becomes available.
According to 10gen:
The sequence creates a cursor that will wait for few seconds after returning the full result set so that it can capture and return additional data added during the query
I have wrapped this functionality up into a server of its own (and client library) available from my GitHub repo. mutated-mongo takes the idea in this article and filters out only messages that particular clients have subscribed to. It’s still a work in progress.