Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection to MongoDB replica set takes a minute+ in PHP when a secondary is unreachable

I have a replica set consisting of 5 members: primary+arbiter on server 1, secondary+arbiter on server 2, hidden secondary (backup node) on server 3. I expect this configuration to work if one server goes down or loses connectivity temporarily.

However, when server 2 gone down (the one with secondary+arbiter nodes), I've encountered a weird problem. Any connection to the replica set from PHP was taking more than a minute. I tried modifying connection string to exclude the server that was down (secondary node) from it, but it did not help.

At the same time connecting via mongo console worked just fine. Primary node was remaining primary. PHP error log did not contain any errors.

The only thing that helped was removing the nodes on the server that gone down from the replica set.

However, I am now worried about the failover of the configuration. As I realize now, if the server with secondary+arbiter nodes will go down the whole configuration will stop working properly. Is there any way to avoid it? I need the PHP client to be able to connect to the primary regardless of whether secondary+arbiter server is available or not. How to achieve that?

Version of the mongo PHP client library is 1.6.x, version of the server is 3.0.

like image 753
mephisto123 Avatar asked Apr 28 '15 08:04

mephisto123


People also ask

What is the MongoDB connection Uri?

The connection URI provides a set of instructions that the driver uses to connect to a MongoDB deployment. It instructs the driver on how it should connect to MongoDB and how it should behave while connected. The following example explains each part of a sample connection URI:

How do I deploy a replica set in production?

Before you can deploy a replica set, you must install MongoDB on each system that will be part of your replica set . If you have not already installed MongoDB, see the installation tutorials. In production, deploy each member of the replica set to its own machine and if possible bind to the standard MongoDB port of 27017.

What are the advantages of a replica set?

Three member replica sets provide enough redundancy to survive most network partitions and other system failures. These sets also have sufficient capacity for many distributed read operations. Replica sets should always have an odd number of members. This ensures that elections will proceed smoothly.


2 Answers

Your configuration will not work if server 2 goes down. Your replica set has 3 voting nodes, two of which are on server 2. If server 2 goes down, the set has 1/3 members up, and so cannot maintain or elect a primary. You need 2/3 able to talk to each other to elect a primary.

Your description of the problem is not consistent with how replica sets behave - connecting to the primary through the shell when 2/3 are down would not show the primary remaining primary. It would step down to secondary. Are you sure you have the right symptoms?

like image 113
wdberkeley Avatar answered Nov 16 '22 02:11

wdberkeley


Just wondering what your connection string looks like.

In my experience something like this would work on a replica set configuration:

$m = new MongoClient("mongodb://rs1.example.com:27017,rs2.example.com:27017");

But when rs2 goes down, I would have similar experience as you described. You may want to make sure you use the correct connection string format:

$m = new MongoClient("mongodb://rs1.example.com:27017", array("replicaSet" => "myReplSetName"));

or

$m = new MongoClient("mongodb://rs1.example.com:27017,rs2.example.com:27017/?replicaSet=myReplSetName");

like image 45
blubear Avatar answered Nov 16 '22 02:11

blubear