The most straightforward load balancer you’ll write in Go

Fermin Blanco
Google Cloud - Community
2 min readOct 5, 2023

--

Reverse Proxy

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:

  1. Caching
  2. Increased security
  3. Load balancing
  4. 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.

  1. Session affinity
  2. Health Checks
  3. The Protocol to use to connect to the backends
  4. 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.

Backend Service Struct

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.

Load Balancer Algorithm

Run

Creating a Load Balancer out of a reverse proxy

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

Resources

Google Bard writes a Load Balancer for me

--

--