Search code examples
mongodbdatabase-performancedatabase-replicationmongoengine

Fast Writes to MongoDB Console, Slow to ReplSet


I have a three node MongoDB replica set.

If I write to the primary node via command line, it completes quickly (< 1 sec).

If I write to the replica set via MongoEngine, it takes forever (> 45 seconds)!

I don't see anything abnormal in the logs -- just successful connections.

The data ultimately shows up, but the connections are blocking for each node.

sample insert (console):

start db 2014-06-17 07:34:10.771450
end db 2014-06-17 07:34:10.777390

sample insert (replset):

start db 2014-06-17 07:34:10.771450
end db 2014-06-17 07:34:50.883062

Connection String (MongoEngine):

db.connect('app', host='db1, db2, db3', username='app', password='abcdef', port=27017, replicaSet='rs0')

I tried disabling writeConcern alltogether, but that didn't help.

If I remove the replicaSet parameter (but keep the three hosts), it speeds right up and data syncs as intended.

What could be causing this? Am I missing anything if I leave of replicaSet?


Solution

  • You need to ensure that you are using a replicaSet connection, changing to the URI format:

    mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
    

    Ensuring you have the replicaSet option in the options set.

    Finally, ensure that all the replicaSet hosts are accessible from the application server.