Error
Error Code:
133
MongoDB Error 133: Failed Read Preference
Description
This error indicates that MongoDB was unable to find an available replica set member that satisfies the read preference specified by the client application. This typically occurs when the replica set topology or health doesn't match the requested read criteria, preventing the database from fulfilling the read operation.
Error Message
Failed To Satisfy Read Preference
Known Causes
4 known causesInsufficient Healthy Members
The replica set does not have enough healthy and available members online that match the specified read preference (e.g., not enough secondaries for 'secondaryPreferred').
Network or Firewall Blocks
Network connectivity problems or firewall rules are preventing the client application from reaching suitable MongoDB replica set members.
Invalid Read Preference
The read preference configuration in the application or driver is incorrectly specified or does not align with the actual replica set topology.
Replica Set Unavailability
The replica set is in a degraded state, such as an ongoing election, primary failover, or multiple members being down, making it impossible to satisfy the read preference.
Solutions
4 solutions available1. Verify Replica Set Member Availability easy
Ensure all members of your replica set are running and accessible.
1
Connect to one of your MongoDB instances, preferably the primary, using the `mongosh` shell.
mongosh
2
Run the `rs.status()` command to check the status of your replica set.
rs.status()
3
Examine the output for any members that are not in an 'UP' or 'PRIMARY' state. Ensure they are running and reachable from the client application and other replica set members.
/* Inspect the 'members' array for state and health */
4
If a member is down, investigate why (e.g., process not running, network issues, disk space) and restart it.
/* Example: To restart mongod on a Linux system if it's not running */
sudo systemctl start mongod
2. Adjust Read Preference in Application or Driver medium
Modify the read preference to a setting that can be satisfied by available members.
1
Identify where the read preference is being set in your application code or connection string. This is often done when establishing the MongoDB connection.
/* Example in Node.js with Mongoose */
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/mydatabase?readPreference=primary', { useNewUrlParser: true, useUnifiedTopology: true });
/* Example in connection string */
mongodb://host1:port1,host2:port2/?replicaSet=myReplicaSet&readPreference=secondaryPreferred
2
If you are using `primary` or `primaryPreferred` and the primary is unavailable, this error can occur. Consider changing the read preference to `secondary` or `secondaryPreferred` if you have healthy secondary members.
/* Change to secondaryPreferred */
'mongodb://host1:port1,host2:port2/?replicaSet=myReplicaSet&readPreference=secondaryPreferred'
3
Alternatively, if you don't strictly require reading from the primary, `nearest` can be a good option as it reads from the member closest to the client, which might be a secondary.
/* Change to nearest */
'mongodb://host1:port1,host2:port2/?replicaSet=myReplicaSet&readPreference=nearest'
4
Re-deploy your application or restart your MongoDB client process after updating the connection string or driver configuration.
/* No specific code snippet, depends on application restart procedure */
3. Configure Client Connection Timeout and Retry Logic medium
Allow more time for the client to connect to an available member and implement retries.
1
Locate the MongoDB connection configuration in your application or client. Look for settings related to connection timeouts and retry attempts.
/* Example in Python with PyMongo */
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/', serverSelectionTimeoutMS=5000, connectTimeoutMS=5000, retryWrites=True)
2
Increase the `serverSelectionTimeoutMS` (or equivalent) to give the driver more time to discover and connect to an available replica set member. A value of 5-10 seconds is often reasonable.
/* Increase timeout to 10 seconds (10000 milliseconds) */
serverSelectionTimeoutMS=10000
3
Ensure that retry logic is enabled for writes if applicable, as transient network issues can cause write operations to fail. For reads, the driver's behavior might vary; some drivers automatically retry read operations if configured.
/* Enable retries for writes */
retryWrites=true
4
Test your application thoroughly after making these changes to ensure stability and responsiveness.
/* No specific code snippet */
4. Check Network Connectivity and Firewall Rules medium
Verify that replica set members can communicate with each other and that clients can reach them.
1
From each replica set member, attempt to connect to other members using `mongosh` to confirm network reachability.
/* From member A, try connecting to member B */
mongosh --host <member_B_hostname> --port <member_B_port>
2
From your client application's host, attempt to connect to the MongoDB instances using `mongosh` to verify client-to-server connectivity.
/* From client host */
mongosh --host <mongodb_host> --port <mongodb_port>
3
Review firewall rules on all MongoDB servers and any intermediate network devices to ensure that the MongoDB port (default 27017) is open for traffic between replica set members and from clients.
/* Example: Check firewall rules on Linux using iptables */
sudo iptables -L -n | grep 27017
4
If firewall rules are blocking traffic, update them to allow the necessary connections. Consult your network administrator for assistance if needed.
/* Example: Allow incoming traffic on port 27017 */
sudo iptables -A INPUT -p tcp --dport 27017 -j ACCEPT