Question

My code is like this:

Replset = {<<"rs1">>, [{localhost, 27017}, {localhost, 27018}, {localhost, 27019}]},
  Conn_Pool = resource_pool:new (mongo:rs_connect_factory(Replset), 10),
  ...

  Conn = resource_pool:get(Conn_Pool)
  case mongo:do(safe, master, Conn, ?DATABASE,
    fun() ->
     mongo:insert(mytable, {'_id', 26, d, 11})
  end end)
  ...

27017 is the primary node, so ofc I can insert the data successfully.

But, when I put only one secondary node in the code instead of all of mongo rs instances: Replset = {<<"rs1">>, [{localhost, 27019}]}, I can also insert the data.

I thought it should have thrown exception or error, but it had written the data successfully.

why that happened?

Was it helpful?

Solution

When you connect to a replica set, you specify the name of the replSet and some of the node names as seeds. The driver connects to the seed nodes in turn and discovers the real replica set membership/config/status via 'db.isMaster()' command.

Since it discovers which node is the primary that way, it is able to then route all your write requests accordingly. The same technique is what enables it to automatically failover to the newly elected primary when the original primary fails and a new one is elected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top