Author: Francis Ndungu
Last Updated: Tue, Feb 8, 2022When deploying the fully-managed Vultr load balancer in your application, there are different algorithms that you can implement. These are techniques for intelligently distributing traffic across multiple servers to provide horizontal scaling, security, and continuous availability.
The two available options include the round-robin and the least connections algorithms. In a round-robin setup, the Vultr load balancer sends requests to all servers in a rotational manner without considering the current load of each server. However, the least connections algorithm is more intelligent and only routes clients' requests to a server with the least number of active connections.
Both algorithms are useful and come with different pros and cons. In this tutorial, you'll learn how to choose the right option to ease the implementation process.
The round-robin algorithm is the most widely used because it is easy to understand and implement. For instance, if you have a set of 3 backend servers in your cluster and you send 5 requests, the algorithm will select the servers in turns as illustrated below:
client request 1: Server 1
client request 2: Server 2
client request 3: Server 3
client request 4: Server 1
client request 5: Server 2
...
The above approach is quite simple. However, it won't perform optimally for a scenario where you've deployed servers with different specifications (for example, RAM and CPU). The method puts the same load on the different backend servers without considering their resources.
However, the round-robin algorithm is suitable in the following scenarios:
In real-life applications, the actual period of time that clients spend on the backend server varies depending on the complexity of the tasks they're executing. If the load balancing algorithm doesn't consider the active connections, some servers may be overloaded with processes that might take a very long time to process.
The least connections algorithm is a better fit in such situations. When a new request comes in, the method routes it to the server which is not serving any requests, and then other subsequent requests are forwarded to the servers with the least active connections in ascending order. For instance, assume you've 3 backend servers handing the following connections.
Server 1 - 15 active connections
Server 2 - 0 active connections
Server 3 - 10 connections
If you send 3 more requests to the servers, the least connections method will select the servers in the following order.
client request 1: Server 2
client request 2: Server 3
client request 3: Server 1
The least connections method is suitable in the following situations:
In this guide, you've seen the pros and cons of Vultr's load balancing algorithms. With the above tips in mind, you can choose the best method that works for your use case. The round-robin algorithm is simple to implement, but the least connections method is always better when you want to put equal loads on your backend services.
For more information about Vultr's load balancer, visit the following resources: