node.js - How do I prevent MongoDB replica set from querying the primary? -
read-operations-only:
is there way have mongodb replica set, make mongodb instance on box connected to, mongodb gets queried?
i have 3 ec2 instances behind aws load balancer.
on each ec2 instance runs mongodb, part of replica set.
i have express endpoints on nodejs , connect replica set 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 distribute load of queries evenly across 3 instances of mongodb replica set instead of default having queries routed ec2 instance primary mongodb defined, because destroys point of load balancer (which balance load of queries across 3 instances evenly).
my understanding when connecting mongodb replica set, primary instance selected unless primary instance down, , therefore purpose of every secondary instance serve backup.
in example people call hot,cold,cold. because there 2 instances never used.
the reason have query load distributed evenly set can warm,warm,warm.
there in future when number of users increases, keep query performance optimal, can add more ec2 instances behind load balancer, improve query performance.
and when data grows, such single instance slow anymore return data on time, when shard each instance other ec2 instances.
note: source code of every ec2 instance same. using git, , push , pull source code every instance in replica set.
you want use readpreference, allow direct read requests secondaries. example code mongodb node.js driver can seen here.
mongodb.mongoclient.connect('mongodb://1.1.1.1,2.2.2.2,3.3.3.3./?readpreference=secondary', ...