mongodb, replicates and error err not master and slaveOkfalse, code 13435
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
MongoDB, a leading NoSQL database, supports a rich array of features designed to enhance scalability and data resilience. Among its pivotal functionalities are replication and error handling, both of which are crucial for ensuring data availability and robustness. This article delves into MongoDB replication mechanisms and explores a specific error scenario to provide a comprehensive understanding of these critical components.
MongoDB Replication
Replication in MongoDB enables data redundancy and enhances database availability by duplicating data across multiple servers. This ensures that data remains accessible even if one or more database servers fail, which is vital for maintaining continuous data access in production environments.
Replication Architecture
- Replica Set:
- A replica set is a group of MongoDB instances that maintain the same dataset.
- Within a replica set, there is one primary node and multiple secondary nodes.
- The primary node receives all write operations, while secondary nodes replicate the primary's oplog (operation log) and apply these operations to their datasets. This guarantees that all nodes contain the same data.
- Primary and Secondary Nodes:
- Primary Node: Handles all write operations. Only one primary exists per replica set.
- Secondary Nodes: Replicate data from the primary in an asynchronous manner. Can be configured for read operations using the
slaveOkparameter.
Replication Configuration
To set up a simple replica set, consider the following example using three MongoDB instances:
Error: {"$err" : "not master and slaveOk=false", "code" : 13435}
This error commonly arises in MongoDB when an application attempts to perform read operations on a secondary node without enabling the appropriate permissions. Understanding this error involves dissecting MongoDB's read preferences and the configuration of secondary nodes.
Explanation of the Error
- The error message indicates that the queried node is not the primary (or master) node and that it does not allow read operations unless specified otherwise.
- Read requests by default should be directed at the primary node unless explicitly indicated to allow reading from secondary nodes using certain mechanisms.
Resolving the Error
To address this error, consider the following approaches:
- Allow Reading from Secondaries:
- Configure the client or driver to allow reads from secondary nodes.
- In MongoDB shell or application code, enable
slaveOkor set appropriate read preferences for secondary reads.
- Direct Reads to the Primary Node:
- Ensure applications direct read operations to the primary node. This might require adjusting DNS configurations or using the network address of the primary node directly.
Read Preference Configuration
MongoDB offers read preferences to control from which members of a replica set the client reads:
- Primary: Read from the primary node.
- Secondary: Read from a secondary node.
- PrimaryPreferred: Read from the primary node if available; else, read from secondaries.
- SecondaryPreferred: Read from a secondary node if available; else, read from primary.
- Nearest: Read from the node with the lowest network latency, prioritizing neither primary nor secondary.
Troubleshooting Other Related Issues
When addressing the aforementioned error or related issues, consider examining:
- Replica set configurations via the
rs.conf()andrs.status()commands. - MongoDB server logs for additional error details or warnings that might indicate underlying replication issues.
- Network connectivity between client applications and the appropriate MongoDB nodes.
Summary Table
| Feature | Description |
| Replica Set | Group of MongoDB instances ensuring data redundancy. Includes one primary and multiple secondary nodes |
| Primary Node | Handles all write operations in a replica set |
| Secondary Node | Replicates data from the primary. Can serve read operations when appropriately configured |
| Error 13435 | Results from attempting to read from a secondary node without enabling slaveOk or setting the correct read preferences |
| Read Preferences | Controls how clients read data from replica sets. Includes options like primary, secondary, primaryPreferred, secondaryPreferred, etc. |
Additional Considerations
- Ensure MongoDB versions across a replica set are compatible to prevent operational discrepancies.
- Regularly monitor the health and synchronization status of a replica set using MongoDB monitoring tools like Ops Manager or Cloud Manager.
- Be mindful of network partitioning and latency, which can impact replica set operations and client connectivity.
By understanding the intricacies of MongoDB replication and how to handle common errors such as "not master and slaveOk=false", developers and administrators can better configure and manage their MongoDB deployments to achieve high availability and data consistency.

