Best practice for having one centralized database across different regions (MongoDB)

Viewed 286

App is hosted in North America, Europe and Asia. All regions have the same instance of the app, but connected to different MongoDB databases hosted in Atlas in the same region as the app.

Each region's app is connected to different database because it would take long for database requests if the app is not hosted in the same region as the database itself.

The biggest issue now is if the user is registered in North America's instance, his user record will be stored in North America's database. So if the same user tries to login later in the Europe or Asia instance, he would not succeed since these databases do not have that user record.

What is the best practice to have one centralized database that would be fast for all regions globally. Can that be done with Atlas? Is it possible to host replicas set of one cluster in different regions?

All suggestions and recommendations are welcomed.

1 Answers

If I understand correctly, you want low global latency while keeping the integrity of your data no mather where your user queries from.

Since you are currently using Atlas, I highly suggest you switch to a Global Cluster with local read-only nodes. Here's why:

On the integrity of your cluster

  • You would have one cluster sharded by the country code from your regions as location. No need to manage multiple clusters anymore, the location would be used to distribute geographically. You will also need another shard key for a proper distribution, but that will not be a problem.

When the user creates the document from a region

  • When writing data, your application will need to indicate to which region it belongs by using the location field.

When the user reads from the region he created the document

  • You can read by passing the location field and the request would be sent to the correct region. But you end up with a high latency problem if the user is requesting from a different region. See the correct solution below.

When the user reads from a different region

  • To support low latency when a user requests from a different region, you need to make your read preference to nearest and indicate the location of the data. Your application will always fetch the copied version of your data from the same (or closest) region as the local read-only nodes. You need to make sure you toggle the option from the screenshot below when setting up the cluster.

local_reads_option

Related