Understanding Primary and Secondary Nodes

Tutorial 2 of 5

1. Introduction

In this tutorial, we will explore the differences between primary and secondary nodes in MongoDB. These nodes play vital roles in data management and consistency across distributed systems.

By the end of this tutorial, you should be able to:

  • Understand the roles and relationships between primary and secondary nodes in MongoDB.
  • Identify the key differences between primary and secondary nodes.
  • Use and manage these nodes in your MongoDB deployments.

Prerequisites for this tutorial include a basic knowledge of MongoDB and understanding of distributed systems.

2. Step-by-Step Guide

In MongoDB, nodes are instances of the database that store data. There are mainly two types: primary and secondary nodes.

A primary node is responsible for all writes to the database. The data written to the primary node is then replicated to secondary nodes.

Secondary nodes replicate the data set of the primary node and apply the operations from the primary node's oplog (operation log) to their data sets.

Best Practices and Tips:

  • Maintain an odd number of voting nodes to avoid a split vote during an election for a new primary.
  • In systems with heavy write operations, consider adding more secondary nodes to distribute the read operations.

3. Code Examples

Let's assume you have a MongoDB setup with a primary and two secondary nodes.

To check which node is the primary, use the rs.status() command:

rs.status()

This command returns a status document. The primary node is indicated in the "members" array with "stateStr" : "PRIMARY".

{
    // other fields
    "members" : [
        {
            "_id" : 0,
            "name" : "mongodb0.example.net:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY", // This indicates that the node is the primary.
            // other fields
        },
        // other members
    ],
    // other fields
}

To read from a secondary node, use the readPreference option:

db.collection.find(query, { readPreference: 'secondary' })

4. Summary

In this tutorial, we've learned about primary and secondary nodes in MongoDB. We've identified the roles of each type of node and how to interact with them. The primary node handles all write operations and the secondary nodes replicate the data of the primary node.

The next steps for learning would be to delve deeper into MongoDB's replication features and how to handle failovers in your MongoDB deployments.

5. Practice Exercises

  1. Setup a MongoDB replica set with 1 primary and 2 secondary nodes.
  2. Write data to the primary node and verify that the data is replicated to the secondary nodes.

Solutions and Explanations

  1. Follow the MongoDB manual's guide on deploying a replica set: https://docs.mongodb.com/manual/tutorial/deploy-replica-set/
  2. After writing data to the primary node, use the db.collection.find() command with the { readPreference: 'secondary' } option on both secondary nodes. If the data is replicated correctly, the find command should return the same data on the secondary nodes as on the primary node.

For further practice, consider exploring how MongoDB handles failovers and how to configure your replica set for automatic failover.