Usually, our systems are configured such that only servers in the same network can access it. So all servers in the same network were able to access the mongo db in its network. However, due to some twisted reason, we had one machine outside this network that needed access. So we opened the IP of this lonesome machine to the mongo db server.
One unfortunate day, everything breaks. Mongo connection timeout logs everywhere! But login to mongo machine and open mongo terminal, it works. And the dbs are intact. Check the security groups to see if the servers have access to mongo, its all fine. Then what is the issue?
We look at logs more carefully now. There is only one IP being logged..and this was the secondary. In the code, we were always connecting to one configured IP, which was the previous PRIMARY. The mongo nodes had switched its states. So, we made the code change to read IPs using the replica set name. This way, it reads the current PRIMARY IP, at all times. A change is made in all components to read all private IPs in the connection url. (Except the lonesome one, of course)
MongoClient mongoClient = new MongoClient( new MongoClientURI( "mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myReplicaSet"));
On the lonesome machine's application we configured all the public IPs, with the replicaSet name and deploy. All applications except the one on lonesome machine is not able to connect to mongo. On looking at the logs, we find that it still is using private IPs, although the configured IPs were public. Strange...
Then we do an rs.status() on the mongo to find that all the "name" fields of the configured nodes have private IPs. There! Even if you configure with public IPs in your application, it uses the replica set and gets the names and tries to connect with those IPs. So we had to keep it the same as before, configure using a single public IP. But this doesn't ensure that you're application will work when the primaries switch. (For us it was still ok, since we did not have a replica set on prod)
One unfortunate day, everything breaks. Mongo connection timeout logs everywhere! But login to mongo machine and open mongo terminal, it works. And the dbs are intact. Check the security groups to see if the servers have access to mongo, its all fine. Then what is the issue?
We look at logs more carefully now. There is only one IP being logged..and this was the secondary. In the code, we were always connecting to one configured IP, which was the previous PRIMARY. The mongo nodes had switched its states. So, we made the code change to read IPs using the replica set name. This way, it reads the current PRIMARY IP, at all times. A change is made in all components to read all private IPs in the connection url. (Except the lonesome one, of course)
MongoClient mongoClient = new MongoClient( new MongoClientURI( "mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myReplicaSet"));
On the lonesome machine's application we configured all the public IPs, with the replicaSet name and deploy. All applications except the one on lonesome machine is not able to connect to mongo. On looking at the logs, we find that it still is using private IPs, although the configured IPs were public. Strange...
Then we do an rs.status() on the mongo to find that all the "name" fields of the configured nodes have private IPs. There! Even if you configure with public IPs in your application, it uses the replica set and gets the names and tries to connect with those IPs. So we had to keep it the same as before, configure using a single public IP. But this doesn't ensure that you're application will work when the primaries switch. (For us it was still ok, since we did not have a replica set on prod)