Sunil
Sunil

Reputation: 3434

MongoDB: Different applications connecting to different replicas

We use Mongodb as the central Database for our application; a consumer facing mobile app. At present its a 7-member replica-set with replica-set-1 being the master at the moment. The backend which connects to the mongo replica is build in Ruby on Rails and we use mongoid as the ODM.

There are mainly 3 pieces connecting to the MongoDB replica-set.

  1. The consumer application
  2. The Admin and customer care management application
  3. The Data retrieval application ( for analytics and such purposes )

All these 3 apps connect to the same replica set as of now.

What I would like to know is whether is it possible to connect different applications to specific replicas.

For example, the mobile app connects to the primary for writes and the replicas 2-4 
to read; the customer care management application connects to the primary 
( for writes ) and replicas 5-7 for reads. 

I dont think explicitly mentioning specific replicas in the mongoid.yml configuration is working. Even though I have already mentioned only replica-set-7 in the mongoid hosts file for the data retrieval application, I do see certain queries in the log file of replica-set-2 and 3.

So obviously, MongoDB decides the criteria to distribute the queries among its replicas despite the configuration specified at the client mongoid end.

I would really love to know if such a thing is possible at all using MongoDb and mongoid as it would help us solve a lot of our load issues. Right now heavy queries from the customer care and data retrieval apps affects the consumer facing mobile app as well; as the reads are not segragated. So basically would like to separate out the reads.

Also, if at all this is possible, I would again have my eyes raised on any possible pitfalls for this; specially that all 3 applications can write to the DB. For example, replica-3 suddenly becomes the primary after an election and its not explicitly mentioned in the configuration of the data retrieval application. What might happen there would become a concern.

I am not at all sure whether this is possible; but just wanted to know if theres a way to figure out this. Any help would be really appreciable.

Upvotes: 1

Views: 248

Answers (1)

Frederick Cheung
Frederick Cheung

Reputation: 84124

When you connect to any member of a replica set, the client is told the full state of the replica set and can connect to any of them. The initial set of hosts are just the seeds for that process - as long as your application can reach one of those hosts, it doesn't matter which hosts are in that configuration.

Mongo does have the concept of tagged replica set members. When creating a connection or executing a query you can specify the tags to use to select the replica set member to read from.

Upvotes: 2

Related Questions