Hello there 👋
This is probably the last part in microservices basics series, but certainly not the least. Today we're going to look at how we can scale up individual services and apply load balancing. This will give a major performance boost in high traffic scenarios and increase the up-time of our application.
Without further ado, let's dive in
Spawning multiple containers
First things first, we have to create/spawn multiple containers of the service we want to apply load balancing to. Let's use the Products service for this experiment.
We can make changes to the products
service definition in the docker-compose.services.yml
file as follows
products:
extends:
file: products/docker-compose.yml
service: app
env_file: .env
scale: 3 # new option "scale" to tell docker-compose to spawn 3 containers for this service
And now, when we bring up the services using the command
docker-compose --file docker-compose.services.yml up
we see the logs indicating there are 3 container running for the products service.
Awesome!! we're already halfway there.
Implementing Load Balancing
Remember when I talked about defining server groups in the nginx.conf
file. Well, so far we only had one server in the group, so calling it a group didn't make much sense.
But now that we have multiple containers running, in other words, multiple servers running, we can use the server group to its fullest.
upstream products {
server microservices-shop_products_1:4000;
server microservices-shop_products_2:4000;
server microservices-shop_products_3:4000;
}
It is as simple as that! we have achieved load balancing among our 3 products service containers.
And, we have the logs to verify that
These logs ☝️ are generated when we send multiple requests to any /products
endpoint.
Since, we've only specified the names of the servers (container names and port numbers), the default algorithm used by Nginx is a Round Robin algorithm.
You can read more about the supported algorithms here.
Wanna see more
Also, if you have any suggestions, improvements, or anything more in general that you'd like to see in this series, please add a comment. I'll try my best to include it here.
Wrap up
I hope you enjoyed this series and learnt something new.
Feel free to post any questions you have in the comments below.
Cheers!
Top comments (0)