Artem Mezhenin
Artem Mezhenin

Reputation: 5757

Handling queries with both master and slaves/primary and secondaries in MongoDB

I want to scale reads with MongoDB. To do this i can setup master-slave replication or replica set, but if I create connection to Mongo like this:

from pymongo import ReplicaSetConnection, ReadPreference
from pymongo.errors import ConnectionFailure

try:
    connection = ReplicaSetConnection("somehost:10000", replicaSet='myapp_repl',
                                  read_preference=ReadPreference.SECONDARY) 
except ConnectionFailure ...

or:

from pymongo.master_slave_connection import MasterSlaveConnection
from pymongo.errors import ConnectionFailure

try:
    master = Connection(host="somehost", port=10000)
    slave1 = Connection(host="somehost", port=10001)
    slave2 = Connection(host="somehost", port=10002)
    connection = MasterSlaveConnection(master, slaves=[slave1, slave2])
except ConnectionFailure ...

pymongo driver will distribute queries among replica set secondaries/slaves. In this situation primary/master will not handle queries, so if I'll have 2 nodes I will not enhance reading capabilities, because only 1 node will handle queries. How can I make both master and slaves (primary and secondaries) handle queries?

Upvotes: 2

Views: 756

Answers (1)

Randall Hunt
Randall Hunt

Reputation: 12582

This is a bit of a hack but:

connection = MasterSlaveConnection(master, slaves=[slave1, slave2, master)

MasterSlaveConnection is deprecated though.

I am not sure of any other way of fixing this.

You might want to look into sharding or adding an arbiter to vote in new primaries: http://www.mongodb.org/display/DOCS/Adding+an+Arbiter

Upvotes: 1

Related Questions