Question: How can you configure MongoDB to read from a secondary replica?

Answer

MongoDB Replica Sets provide redundancy and high availability, and are the basis for all production deployments. By default, read operations in MongoDB are directed at the primary node of a replica set. However, there are scenarios where it might be beneficial to balance the read load or access stale data by reading from one of the secondary replicas. This process is known as 'reading from a secondary'.

Configuring Read Preference

To direct read operations to secondary replicas, you'll need to set the read preference for your database connection or operation. MongoDB supports several read preferences:

  • Primary: Default mode. All read operations are directed to the primary node.
  • Secondary: All read operations are directed to secondary nodes.
  • PrimaryPreferred: Reads go to the primary node but if itโ€™s not available, reads will go to a secondary node.
  • SecondaryPreferred: Reads go to a secondary node if available; otherwise, they go to the primary node.
  • Nearest: Reads are sent to the node with the lowest network latency among primaries and secondaries.

To direct reads to a secondary, you'll use either the Secondary, SecondaryPreferred, or Nearest read preference, depending on your specific requirements.

Example in Python (Using PyMongo)

from pymongo import MongoClient, ReadPreference # Connect to the MongoDB cluster client = MongoClient('mongodb://localhost:27017/', replicaset='yourReplicaSetName') # Select your database db = client['yourDatabaseName'] # Set the read preference to Secondary db.read_preference = ReadPreference.SECONDARY # Now, any query made using this db instance will attempt to read from a secondary node result = db.yourCollectionName.find({}) for document in result: print(document)

Considerations

  1. Latency vs. Freshness: Reading from secondaries can reduce load on the primary and decrease read latency if secondaries are closer to the application. However, data on secondaries might be slightly stale.
  2. Consistency: If your application requires strong consistency, reading from the primary might be preferable.
  3. Failover: Be prepared for failovers. During a failover, secondaries are elected as the new primary, which could temporarily affect read queries directed to secondaries.

By adjusting the read preferences, you can balance the needs of your application between consistency, latency, and load distribution.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.