Example of NGINX Load Balancing (Consistent Hashing)
Load balancing across multiple application instances is a commonly used technique for optimizing resource utilization, maximizing throughput, reducing latency, and ensuring fault‑tolerant configurations.
This repo provides an example of how to configure a NGINX load balancer and test sample video services using the algorithm of consistent hashing.
Consistent Hashing is a distributed hashing scheme that operates independently of the number of servers or objects in a distributed hash table by assigning them a position on an abstract circle, or hash ring. This allows servers and objects to scale without affecting the overall system.
-
Download NGINX Plus license files, and copy them to
./docker/build-context/ssl/
nginx-repo.crt nginx-repo.key
-
Run a NGINX load balancer and 5 sample video servcies as containers:
make start
-
Check if the above containers are successfully started:
make watch
-
Request sample APIs:
-
Check the status of load balancer:
Note:
Try killing some of containers and restart them to see how the load balancer works based on consistent hashing algorithm.
docker kill video-service-05 curl localhost/videos docker kill video-service-01 curl localhost/videos curl localhost/dummy make start
-
Stop containers and clean the images:
make down make clean
upstream video_services {
zone video_services 64k;
hash $request_uri consistent;
server host.docker.internal:11001;
server host.docker.internal:11002;
server host.docker.internal:11003;
server host.docker.internal:11004;
server host.docker.internal:11005;
}