Jaconir

Load Balancing

The traffic cop of the internet, ensuring no single server gets overwhelmed.

What is a Load Balancer?

A load balancer is a device or software that sits in front of a cluster of servers (often called a server farm or pool) and routes client requests across all servers capable of fulfilling those requests. This maximizes performance, prevents server overload, and provides high availability by rerouting traffic if a server goes down.

Users

Load Balancer

Server 1

Server 2

Server 3

The load balancer intelligently distributes incoming user traffic across multiple servers.

Why Use a Load Balancer?

  • High Availability & Fault Tolerance: If one server fails, the load balancer automatically reroutes traffic to healthy servers, preventing downtime.
  • Improved Performance: By distributing the workload, it reduces response times and prevents any single server from becoming a bottleneck.
  • Scalability: Makes it easy to add or remove servers from the pool (horizontal scaling) without disrupting service.
  • Increased Reliability: Simplifies maintenance, as individual servers can be taken offline for updates without impacting the application's availability.
Types of Load Balancers

Layer 4 (L4) Load Balancer

Operates at the transport layer (TCP/UDP). It makes routing decisions based on information like IP address and port number. L4 load balancers are very fast because they don't inspect the content of the traffic.

Layer 7 (L7) Load Balancer

Operates at the application layer. It can inspect the content of the traffic (e.g., HTTP headers, URLs, cookies) to make more intelligent routing decisions. For example, it can route requests to different servers based on the URL path (`/api` vs. `/images`).

Load Balancing Algorithms
See how different algorithms distribute incoming requests to servers.

Queue

Server 1

Load: 0

Server 2

Load: 0

Server 3

Load: 0

Log

Click "Send Request" to start.

Common Load Balancing Algorithms

Round Robin

The simplest method. The load balancer cycles through the list of servers and sends each new request to the next server in the list. It works best when servers have similar processing power.

Least Connections

A more dynamic approach. The load balancer checks which server currently has the fewest active connections and sends the new request to that server. This is effective when some requests take longer to process than others.

IP Hash

A hash of the client's IP address is calculated to determine which server receives the request. This ensures that a particular user will consistently connect to the same server, which is useful for maintaining session state (often called "sticky sessions").

Coding Challenge
Implement a `LoadBalancer` class that distributes requests to a list of servers using the Round Robin algorithm. It should have an `addServer` method and a `next` method to get the next server.
Deepen Your Understanding

For a comprehensive overview of system design concepts, "System Design Interview – An Insider's Guide" by Alex Xu is an industry-standard resource.

J
Dafin Edison J
Creator & Developer of Jaconir

I build things for the web. From immersive games to practical tools, my goal is to create products that people love to use and to share the knowledge I've gained along the way.