Optimizing for Fast, Responsive Reads with Cross-Region Replication in MongoDB Atlas
Tags: aws, mongodb, cloud, database
Published: 2018-04-17 17:52:19

---
title: Optimizing for Fast, Responsive Reads with Cross-Region Replication in MongoDB Atlas
published: true
tags: #aws #mongodb #cloud #databases
canonical_url: https://www.mongodb.com//blog/post/optimizing-fast-responsive-reads-cross-region-replication-mongodb-atlas
---

MongoDB Atlas customers can enable cross-region replication for multi-region fault tolerance and fast, responsive reads.

- Improved availability guarantees can be achieved by distributing replica set members across multiple regions. These secondaries will participate in the automated election and failover process should the primary (or the cloud region containing the primary) go offline. 
- Read-only replica set members allow customers to optimize for local reads (minimize read latency) across different geographic regions using a single MongoDB deployment. These replica set members will not participate in the election and failover process and can never be elected to a primary replica set member.

In this post, we’ll dive a little deeper into optimizing for local reads using cross-region replication and walk you through the necessary configuration steps on an environment running on AWS.

### Primer on read preference

[Read preference](https://docs.mongodb.com/manual/core/read-preference/) determines how MongoDB clients route read operations to the members of a replica set. By default, an application directs its read operations to the replica set [primary](https://docs.mongodb.com/manual/reference/glossary/#term-primary). By specifying the read preference, users can:

- Enable local reads for geographically distributed users. Users from California, for example, can read data from a replica located locally for a more responsive experience
- Allow read-only access to the database during failover scenarios

![](https://webassets.mongodb.com/_com_assets/cms/image5-vh6i7z8j0q.png)

A read replica is simply an instance of the database that provides the replicated data from the oplog; clients will not write to a read replica.

With MongoDB Atlas, we can easily distribute read replicas across multiple cloud regions, allowing us to expand our application's data beyond the region containing our replica set primary in just a few clicks.

To enable local reads and increase the read throughput to our application, we simply need to modify the read preference via the MongoDB drivers.

### Enabling read replicas in MongoDB Atlas

We can enable read replicas for a new or existing MongoDB paid cluster in the Atlas UI. To begin, we can click on the cluster “configuration” button and then find the link named _ **"Enable cross-region configuration options."** _

When we click this, we’ll be presented with an option to select the type of cross-replication we want. We'll choose _ **deploy read-only replicas** _:

![](https://webassets.mongodb.com/_com_assets/cms/image1-q7z5p2iagp.gif)

As you can see above, we have our preferred region (the region containing our replica set primary) set to AWS, us-east-1 (Virginia) with the default three nodes. We can add regions to our cluster configuration based on where we think other users of our application might be concentrated. In this case, we will add additional nodes in us-west-1 (Northern California) and eu-west-1 (Ireland), providing us with read replicas to serve local users.

Note that all writes will still go to the primary in our preferred region, and reads from the secondaries in the regions we’ve added will be eventually consistent.

![](https://webassets.mongodb.com/_com_assets/cms/image4-l82awkjmnd.gif)

We’ll click "Confirm and Deploy", which will deploy our multi-region cluster.

![](https://webassets.mongodb.com/_com_assets/cms/image3-tkhye8w0g6.png)

Our default connection string will now include these read replicas. We can go to the "Connect" button and find our full connection string to access our cluster:

![](https://webassets.mongodb.com/_com_assets/cms/image2-fpoel33crq.png)

When the deployment of the cluster completes, we will be ready to distribute our application's data reads across multiple regions using the MongoDB drivers. We can specifically configure readPreference within our connection string to send clients to the "closest replicas". For example, the [Node native MongoDB Driver](https://mongodb.github.io/node-mongodb-native/index.html) permits us to specify our preference:

| **<font color="grey">readPreference</font>** | Specifies the [replica set](https://docs.mongodb.com/manual/reference/glossary/#term-replica-set) read preference for this connection. |
| --- | --- |
|   
  
 | The read preference values are the following:
- [primary](https://docs.mongodb.com/manual/reference/read-preference/#primary)
- [primaryPreferred](https://docs.mongodb.com/manual/reference/read-preference/#primaryPreferred)
- [secondary](https://docs.mongodb.com/manual/reference/read-preference/#secondary)
- [secondaryPreferred](https://docs.mongodb.com/manual/reference/read-preference/#secondaryPreferred)
- [nearest](https://docs.mongodb.com/manual/reference/read-preference/#nearest)
 |

For our app, if we want to ensure the [read preference in our connection string](https://docs.mongodb.com/manual/reference/connection-string/) is set to the nearest MongoDB replica, we would configure it as follows:

```
mongodb://admin:@cluster0-shard-00-00-bywqq.mongodb.net:27017,cluster0-shard-00-01-bywqq.mongodb.net:27017,cluster0-shard-00-02-bywqq.mongodb.net:27017,cluster0-shard-00-03-bywqq.mongodb.net:27017,cluster0-shard-00-04-bywqq.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin?readPreference=nearest
```

### Security and Connectivity (on AWS)

MongoDB Atlas allows us to peer our application server's VPC directly to our MongoDB Atlas VPC within the same region. This permits us to reduce the network exposure to the internet and allows us to use native AWS Security Groups or CIDR blocks. You can review how to configure [VPC Peering here](https://docs.atlas.mongodb.com/security-vpc-peering/).

**_A note on VPCs for cross-region nodes:_**

At this time, MongoDB Atlas **does not** support VPC peering across regions. If you want to grant clients in one cloud region read or write access to database instances in another cloud region, you would need to permit the clients’ public IP addresses to access your database deployment via [IP whitelisting](https://docs.atlas.mongodb.com/security-whitelist/).

With cross-region replication and read-only replicas enabled, your application will now be capable of providing fast, responsive access to data from any number of regions.

* * *

Get started today with a free 512 MB database managed by MongoDB Atlas [here](https://www.mongodb.com/cloud/atlas?utm_medium=dev-synd&utm_source=dev&utm_content=cross-reg&jmp=dev-ref).