Navigation

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.

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 primary node.
1

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.

2

On the cluster configuration screen, select a mode from the Read Preference dropdown.

The read preference dropdown menu

The following read preference modes are available:

ModeDescription
primaryRealm routes all read operations to the current replica set primary node. This is the default read preference mode.
primaryPreferredRealm 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.
secondaryRealm routes all read operations to one of the current replica set secondary nodes.
secondaryPreferredRealm 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.
nearestRealm routes read operations to the replica set member that has the lowest network latency relative to the client.
3

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.

Info With Circle IconCreated with Sketch.Note
Atlas Cluster Tag Sets

MongoDB Atlas clusters are configured with pre-defined tag sets for each member node depending on the member's type. For detailed information on which tags are defined nodes in an Atlas cluster, see Atlas Replica Set Tags.

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.

The read preference tags table
4

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.

Give Feedback