Load Balancing Queries with NGINX
We’re overhauling Dgraph’s docs to make them clearer and more approachable. If you notice any issues during this transition or have suggestions, please let us know.
There might be times when you’ll want to set up a load balancer to accomplish goals such as increasing the utilization of your database by sending queries from the app to multiple database server replicas. You can follow these steps to get started with that.
Setting up NGINX load balancer using Docker Compose
Download ZIP
Download the contents of this gist’s ZIP file and extract it to a directory
called graph-nginx
, as follows:
Two files will be created: docker-compose.yml
and nginx.conf
.
Start Dgraph cluster
Start a 6-node Dgraph cluster (3 Dgraph Zero, 3 Dgraph Alpha, replication setting 3) by starting the Docker Compose config:
Setting up NGINX load balancer with Dgraph running directly on the host machine
You can start your Dgraph cluster directly on the host machine (for example, with systemd) as follows:
Install NGINX using the following apt-get
command
After you have set up your Dgraph cluster, install the latest stable NGINX. On Debian and Ubuntu systems use the following command:
Configure NGINX as a load balancer
Make sure that your Dgraph cluster is up and running (it this case we will refer
to a 6 node cluster). After installing NGINX, you can configure it for load
balancing. You do this by specifying which types of connections to listen to,
and where to redirect them. Create a new configuration file called
load-balancer.conf
:
and edit it to read as follows:
Next, disable the default server configuration; on Debian and Ubuntu systems you’ll need to remove the default symbolic link from the sites-enabled folder.
Now you can restart nginx
:
Use the increment tool to start a gRPC LB
In a different shell, run the dgraph increment
(docs) tool against the NGINX gRPC load balancer
(nginx:9080
):
If you have Dgraph installed on your host machine, then you can also run this from the host:
The increment tool uses the Dgraph Go client to establish a gRPC connection
against the --alpha
flag and transactionally increments a counter predicate
--num
times.
Check logs
In the NGINX access logs (in the docker-compose
up shell window), or if you
are not using Docker Compose you can tail logs from /var/log/nginx/access.log
.
You’ll see access logs like the following:
With gRPC load balancing, each request can hit a different Alpha node. This can increase read throughput.
These logs show that traffic is being load balanced to the following upstream addresses defined in alpha_grpc in nginx.conf:
nginx to: 172.20.0.7
nginx to: 172.20.0.2
nginx to: 172.20.0.5
Load balancing methods
By default, NGINX load balancing is done round-robin. By the way There are other
load-balancing methods available such as least connections or IP hashing. To use
a different method than round-robin, specify the desired load-balancing method
in the upstream section of load-balancer.conf
.
Was this page helpful?