MongoDB - No server chosen with java async driver and replica set
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The MongoDB error "No server chosen" means the Java driver could not find any server in the cluster description that satisfied the request before server selection timed out. In replica set setups, this usually points to topology discovery rather than to your query code. Common causes include wrong seed hosts, the wrong replica set name, unreachable member addresses, TLS or auth issues, or read preferences that do not match the available nodes.
What the Driver Is Trying to Do
When you connect to a replica set, the driver does more than open one socket. It:
- contacts one or more seed hosts
- discovers the replica set topology
- checks which servers are primary, secondary, or unknown
- selects a server matching the operation requirements
If none of the discovered servers are usable, you get a server selection error such as "No server chosen."
That means the problem is often upstream of your actual database command.
Start with the Connection String
A correct replica set URI usually looks like this:
Key points:
- include multiple members when possible
- specify the correct replica set name
- use hostnames the client can actually reach
If the replicaSet name is wrong, the driver may discover servers but reject them because the topology does not match the expected set.
A Minimal Async Example
If this fails with a server selection error, the next step is to inspect topology and connectivity, not the callback.
The Most Common Real Problem: Unreachable Advertised Hosts
This is especially common in Docker, Kubernetes, and mixed-network environments. The seed host may be reachable, but the replica set members may advertise internal names or IPs that the Java client cannot reach.
Example failure pattern:
- client connects to
mongo1.example.com - replica set replies that members are
mongo-0.internal,mongo-1.internal - those internal names are not resolvable from the Java app
- topology discovery succeeds partially, but no usable server is chosen
So always compare:
- the addresses the client can reach
- the addresses the replica set members advertise in
rs.conf()orhello
If those worlds differ, the driver can discover a topology it cannot actually use.
Check Replica Set Health
From a Mongo shell or admin connection, inspect the replica set:
Look for:
- correct replica set name
- a healthy primary
- secondaries in reachable states
- member hostnames matching what clients should use
If there is no primary, writes requiring a primary cannot succeed. If members are unhealthy or misnamed, driver selection will suffer even if one seed host initially answers.
Read Preference Can Also Block Selection
If your code or connection string requires a secondary, but no secondary is available, the driver may fail server selection even though a primary exists.
Similarly, a write operation requires a primary-capable server.
So keep the request and the topology aligned:
- writes usually need primary
- secondary reads need healthy secondaries
- custom read preference rules can narrow the candidate set
This matters more than many people realize.
Timeouts, TLS, and Authentication
Network and security settings can also cause "No server chosen" outcomes:
- TLS mismatch
- bad certificates
- firewall or network policy blocks
- authentication configuration that prevents successful handshake
In these cases the topology may remain in unknown or failed states long enough that selection times out.
That is why good troubleshooting includes both:
- driver logs
- low-level connectivity checks to each advertised host
Increase Logging and Read the Topology Description
The server selection exception often includes a topology summary. That summary is high-value information, because it tells you whether the driver saw:
- unknown servers
- unreachable hosts
- missing primary
- mismatched replica set membership
Do not stop at the first error headline. The topology description is often the fastest route to the actual root cause.
Common Pitfalls
The biggest mistake is assuming the seed host is the whole connection. In replica sets, the driver must use the topology advertised by the cluster, not just the original hostname you typed.
Another issue is omitting or misspelling the replicaSet name, which prevents proper topology validation.
Developers also often run into environment mismatches where the replica set advertises internal addresses unreachable from the application network.
Finally, read preference and topology state matter. A cluster can be partially alive and still provide no server that matches the operation you are trying to run.
Summary
- "No server chosen" usually means the Java driver could not select any suitable replica set member before timing out.
- Start by checking the connection string, especially seed hosts and
replicaSetname. - Make sure the replica set advertises hostnames the client can actually reach.
- Verify replica set health, primary availability, and read preference compatibility.
- Use the topology details in the exception to distinguish network, configuration, and replica set state problems.

