The most straightforward load balancer you’ll write in Go
Abstract
Load balancing enable our applications to better utilize the physical resources
(memory, storage, CPU)
that power them execution.
This will be a series for sure, and as the first article in the series we will come to the basics. In order to do so, we’ll implement a load balancer with a random algorithm. A Load Balancer builds upon a reverse-proxy.
STOP THIS MADNESS AND SHOW ME THE CODE
What the heck is a reverse proxy?
The reverse-proxy is software that sits in the middle between two parts of the communication, the Cloud Service that provides ‘a service to their clients’ and their clients. A reverse-proxy provides intelligence to the communication and by intelligence I mean:
- Caching
- Increased security
- Load balancing
- Among others …
To effectively code a load balancer we need to define its behaviour through a Backend Service.
Backend Services
A backend service is a set of configurations that controls the Load Balancer behaviour.
- Session affinity
- Health Checks
- The Protocol to use to connect to the backends
- An associated Backend (the machines hosting the service our clients are interested in)
The Backends
They represent the instances running the service we are interested in putting behind a load balancer.
But how does it exactly decides to which application instance direct the traffic from an specific request?
Load balancing algorithm
The load balancing algorithm is set by the selectService
function. As it can be better elaborate it enables to depict in a glance how the Load Balancer distributes traffic.
Run
Common Issues
http: proxy error: unsupported protocol scheme “”
You got to specify the scheme
when defining the host
, like below:
// inside the Director function
r.URL.Scheme = "http"
r.URL.Host = targetBackendService