Reputation: 2846
read-operations-only:
is there a way to have a MongoDB replica set, but to make the MongoDB instance on the box that is connected to, be the MongoDB that gets queried?
I have three EC2 instances behind an AWS load balancer.
on each EC2 instance runs MongoDB, which is part of the replica set.
I have express endpoints on nodeJS and I connect to the replica set as follows
mongodb.MongoClient.connect('mongodb://1.1.1.1,2.2.2.2,3.3.3.3.',
function (err, db)
{
if (err)
{
complete('{}');
}
else
{
I would like to distribute the load of queries evenly across all three instances of my MongoDB replica set instead of by default having all queries routed to the EC2 instance where my primary MongoDB is defined, because this destroys the point of my load balancer (which is to balance the load of queries across all three instances evenly).
my understanding is that when connecting to a MongoDB replica set, that the primary instance is always selected unless the primary instance is down, and therefore the purpose of every secondary instance is just to serve as a backup.
in my example some people would call this HOT,COLD,COLD. because there are two instances that never get used.
the reason that I would like to have the query load distributed evenly is so my set up can be WARM,WARM,WARM.
there for in the future when the number of users increases, to keep query performance optimal, I can add more EC2 instances behind the load balancer, to improve query performance.
and when the data grows, such that a single instance is too slow anymore to return the data on time, that is when I would shard each instance into other EC2 instances.
note: I would like the source code of every EC2 instance to be the same. I am using git, and I would like to easily push and pull source code to every instance in my replica set.
Upvotes: 1
Views: 1088
Reputation: 2763
You want to use ReadPreference, which will allow you to direct read requests to secondaries. Example code for the MongoDB node.js driver can be seen here.
mongodb.MongoClient.connect('mongodb://1.1.1.1,2.2.2.2,3.3.3.3./?readPreference=secondary', ...
Upvotes: 1