Specify Read Preference for a MongoDB Cluster¶
You can configure the read preference for a linked MongoDB Atlas cluster to control the type of replica set member that MongoDB Realm routes database read requests to. You can also specify a tag set to target specific members of the replica set.
You cannot set the read preference on a linked Data Lake.
By default, Realm uses a read preference of
primary, which routes
all read requests through the primary node of a replica set.
When To Specify Read Preference¶
The default read preference (
primary) should be sufficient for most
use cases. Consider specifying a cluster read preference when you need
to do the following:
- Read from a specific secondary that has a custom configuration, such as an analytics node with special indexes optimized for reporting workloads.
- Read from a node in a specific region of a globally distributed replica set.
- Maintain read availability during a replica set failover, i.e.,
continue to read potentially stale data when there is no
Navigate to the Cluster Configuration Screen¶
You can configure the read preference for a linked cluster from the cluster's configuration screen in the Realm UI. To get to the configuration screen, click Linked Data Sources beneath Manage in the left-hand navigation and then select the cluster for which you want to configure read preference.
Specify a Cluster Read Preference¶
On the cluster configuration screen, select a mode from the Read Preference dropdown.
The following read preference modes are available:
|primary||Realm routes all read operations to the current replica set primary node. This is the default read preference mode.|
|primaryPreferred||Realm routes all read operations to the current replica set primary node if it's available. If the primary is unavailable, such as during an automatic failover, read requests are routed to a secondary node instead.|
|secondary||Realm routes all read operations to one of the current replica set secondary nodes.|
|secondaryPreferred||Realm routes all read operations to one of the replica set's available secondary nodes. If no secondary is available, read requests are routed to the replica set primary instead.|
|nearest||Realm routes read operations to the replica set member that has the lowest network latency relative to the client.|
Specify Read Preference Tags¶
If you specify a read preference other than
primary, you can also
specify one or more read preference tags. To
serve a read request, a replica set member must include all of the specified read
preference tags in its node configuration.
To specify a read preference tag, click Add Tag and then enter the tag's Key and Value in the new row of the Read Preference Tags table.
Save the Cluster Configuration¶
Once you have specified a Read Preference and any Read Preference Tags, click Save. Once saved, MongoDB Realm routes all incoming database read requests for the cluster according to your preference.