Server selection times out sometimes but not always

Hi,

I’m having issues connecting to my MongoDB cluster.

I’m connecting from GCP through a serverless VPC connector with a static IP address. Previously, I was using VPC peering without any problems, but I needed a static IP, hence the change.

Sometimes I can connect without issues, but very often I cannot. Currently, the only way to resolve this is by upgrading (then downgrading) my cluster, which temporarily restores connectivity.

The error I receive is shown below. The Primary node is marked as “Unknown,” which seems to be the root cause. I’ve already tried the following troubleshooting steps:

  1. Verified the IP is whitelisted and static
  2. Changed the connection timeouts from 30 to 60 and 90 seconds without success
  3. Verified that DNS returns the correct IP addresses for all shards (confirmed by comparing with my local machine)
  4. Confirmed I can connect from my local machine without problems, suggesting it’s not an Atlas-side limit issue
{
  "message": "Server selection timed out after 60000 ms",
  "reason": {
    "type": "ReplicaSetNoPrimary",
    "servers": {
      "mongo-custer-shard-00-00.p5z11.mongodb.net:27017": {
        "address": "mongo-custer-shard-00-00.p5z11.mongodb.net:27017",
        "type": "RSSecondary",
        "hosts": [
          "mongo-custer-shard-00-00.p5z11.mongodb.net:27017",
          "mongo-custer-shard-00-01.p5z11.mongodb.net:27017",
          "mongo-custer-shard-00-02.p5z11.mongodb.net:27017"
        ],
        "passives": [],
        "arbiters": [],
        "tags": {
          "provider": "GCP",
          "region": "EUROPE_WEST_4",
          "nodeType": "ELECTABLE",
          "workloadType": "OPERATIONAL",
          "availabilityZone": "europe-west4-a",
          "diskState": "READY"
        },
        "minWireVersion": 0,
        "maxWireVersion": 17,
        "roundTripTime": 2.9,
        "minRoundTripTime": 2,
        "lastUpdateTime": 5222765,
        "lastWriteDate": {
          "$date": "2025-01-27T14:25:35Z"
        },
        "error": null,
        "topologyVersion": {
          "processId": {
            "$oid": "6794fec901c10dfc17107655"
          },
          "counter": 3
        },
        "setName": "atlas-nk0i6g-shard-0",
        "setVersion": 2,
        "electionId": null,
        "logicalSessionTimeoutMinutes": 30,
        "maxMessageSizeBytes": 48000000,
        "maxWriteBatchSize": 100000,
        "maxBsonObjectSize": 16777216,
        "primary": "mongo-custer-shard-00-02.p5z11.mongodb.net:27017",
        "me": "mongo-custer-shard-00-00.p5z11.mongodb.net:27017",
        "$clusterTime": {
          "clusterTime": {
            "$timestamp": {
              "t": 1737987935,
              "i": 2
            }
          },
          "signature": {
            "hash": {
              "$binary": {
                "base64": "removed",
                "subType": "00"
              }
            },
            "keyId": 7428150405197988000
          }
        },
        "iscryptd": false
      },
      "mongo-custer-shard-00-01.p5z11.mongodb.net:27017": {
        "address": "mongo-custer-shard-00-01.p5z11.mongodb.net:27017",
        "type": "RSSecondary",
        "hosts": [
          "mongo-custer-shard-00-00.p5z11.mongodb.net:27017",
          "mongo-custer-shard-00-01.p5z11.mongodb.net:27017",
          "mongo-custer-shard-00-02.p5z11.mongodb.net:27017"
        ],
        "passives": [],
        "arbiters": [],
        "tags": {
          "region": "EUROPE_WEST_4",
          "availabilityZone": "europe-west4-b",
          "diskState": "READY",
          "provider": "GCP",
          "workloadType": "OPERATIONAL",
          "nodeType": "ELECTABLE"
        },
        "minWireVersion": 0,
        "maxWireVersion": 17,
        "roundTripTime": 2.6,
        "minRoundTripTime": 2,
        "lastUpdateTime": 5222764,
        "lastWriteDate": {
          "$date": "2025-01-27T14:25:35Z"
        },
        "error": null,
        "topologyVersion": {
          "processId": {
            "$oid": "67950166556fb57bcd11314a"
          },
          "counter": 3
        },
        "setName": "atlas-nk0i6g-shard-0",
        "setVersion": 2,
        "electionId": null,
        "logicalSessionTimeoutMinutes": 30,
        "maxMessageSizeBytes": 48000000,
        "maxWriteBatchSize": 100000,
        "maxBsonObjectSize": 16777216,
        "primary": "mongo-custer-shard-00-02.p5z11.mongodb.net:27017",
        "me": "mongo-custer-shard-00-01.p5z11.mongodb.net:27017",
        "$clusterTime": {
          "clusterTime": {
            "$timestamp": {
              "t": 1737987935,
              "i": 2
            }
          },
          "signature": {
            "hash": {
              "$binary": {
                "base64": "removed",
                "subType": "00"
              }
            },
            "keyId": 7428150405197988000
          }
        },
        "iscryptd": false
      },
      "mongo-custer-shard-00-02.p5z11.mongodb.net:27017": {
        "address": "mongo-custer-shard-00-02.p5z11.mongodb.net:27017",
        "type": "Unknown",
        "hosts": [],
        "passives": [],
        "arbiters": [],
        "tags": {},
        "minWireVersion": 0,
        "maxWireVersion": 0,
        "roundTripTime": -1,
        "minRoundTripTime": 0,
        "lastUpdateTime": 5162976,
        "lastWriteDate": 0,
        "error": null,
        "topologyVersion": null,
        "setName": null,
        "setVersion": null,
        "electionId": null,
        "logicalSessionTimeoutMinutes": null,
        "maxMessageSizeBytes": null,
        "maxWriteBatchSize": null,
        "maxBsonObjectSize": null,
        "primary": null,
        "me": null,
        "$clusterTime": null,
        "iscryptd": false
      }
    },
    "stale": false,
    "compatible": true,
    "heartbeatFrequencyMS": 10000,
    "localThresholdMS": 15,
    "setName": "atlas-nk0i6g-shard-0",
    "maxElectionId": null,
    "maxSetVersion": null,
    "commonWireVersion": 0,
    "logicalSessionTimeoutMinutes": 30
  }
}

Hi Daan_Vermunt

VPC peering is not supported for MongoDB Atlas Serverless. Please see a prior post with a similar question. MongoDB Atlas Serverless - VPC Peering - #3 by Jason_Tran

Hee Anurag,

Thank you very much for you reaction.

I’ve switched from VPC peering to a serverless adaptor on the GCP side. So from Atlas’ perspective from VCP peering to using a public connection with a whitelisted IP.

Both wher using Atlas’ dedicated servers.

Any idea what else it might be? I did see there were limits number of connections from a single IP on serverless, but those should not be present on an M40 right?

Thanks in advance,

Daan

Oh you’re using an M40? In that case VPC peering should be supported. I would recommend reaching out to MongoDB support here to investigate further.