Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector

I am getting the following error

Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}.

Client view of cluster state is {type=REPLICA_SET, servers=[
  {address=mongo01:27017, type=UNKNOWN, state=CONNECTING*},
  {address=mongo02:27017, type=REPLICA_SET_SECONDARY, roundTripTime=2.5 ms, state=CONNECTED},
  {address=mongo03:27017, type=REPLICA_SET_SECONDARY, roundTripTime=1.1 ms, state=CONNECTED}

using spring data mongo

its a multitenant environment
change/create the mongoclient connection per request. Database/user/password change as tenant request.

Issue is intermittent and keeps happening randomly.

Hi @Kushal_Somkuwar,

Looking at the logging message - the driver is having trouble connecting to the primary node. The client view of the cluster state shows the state as seen from the Java driver (which spring data uses underneath).

Connecting appears to take longer than the 30s timeout to connect to the mongo01 node. So things to check are the server logs - has there been a change in the replicaset topology - eg new primary and secondaries? Also have there been any networking issues between the application and the mongodb nodes?


1 Like

Hi @Ross_Lawley ,

so application connection with mongodb works fine initially but if the data json is big than it become slow for application and start showing the above error.

we have checked the server logs there so no such of connection and I primary node is running fine there is no issue with cluster as well we suspect the same but why would it connect initially if there is a network issue.


Hi @Kushal_Somkuwar,

If the driver initially connects then later cannot connect, then that points to some networking issue. It may be intermittent and not happen all the time. But from the drivers perspective it is timing out trying to select a server. The log message just shows you the current view of the servers after selection has failed, which is it is in the process of connecting to the mongo1 node.


Hi @Ross_Lawley

I also think the same but we are not getting any exception on mongodb server logs and application just get that error. Is there a way to identify what is exactly is the network issue ?