Handling disaster recovery and geographic distribution with Nginx

Davide Cerbo
4 min readSep 28, 2019

Clustering a microservice, or a web application, in a single datacenter can help us in performance and availability, but what’s happens when our users are distributed worldwide, or there is a disaster and our data center isn’t more available?

TL;DR: Jump directly to the Demo section!

So, we decide to serve our application from the closest datacenter to the user, to improve network latency, database load, and in general performance, and, moreover, we will use the other datacenter as a backup when the principal datacenter isn’t available, in order to have high availability (HA), also in case of a disaster recovery.

In the image above, devices on West Central US will contact Backend One Principal in the datacenter on the left, while devices in South East Asia will be redirected on Backend Two Principal in the datacenter on the right. When datacenter in West Central US isn’t more available the load balancer will redirect requests to the datacenter in South East Asia, and vice versa.

What about data?

The data are replicated between datacenter using well-known replication mechanism provided by databases like Cassandra, MongoDB and so on, so it isn’t our business. To simplify transactions and locks on data, we can decide to partition our data. Only one application (or cluster of applications) in a geographic location has responsibility on that bunch of data, while the other geographic locations own only a copy, only for querying, not for editing. This will redux the complexity of data handling in our system. In this demo the “Backend One” is serving data of West-central US, and replicating data of South East Asia, and “Backend Two” the vice-versa.


The demo provides an Nginx set up to serve an active/passive service in a multi-datacenter/geo-replicated scenario.

We will start an Nginx load balancer that redirects the client that performs request without header to a data center and the client that uses the header “X-SERVER-SELECT” with value “two” to another datacenter. I choose this mechanism, but you can also try to perform the redirection based on the IP location.

Davide Cerbo

Open-space invader, bug hunter and software writer