100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Load Balancers Basics

Learn how Layer 4 and Layer 7 load balancers distribute traffic across servers using algorithms like round robin and least connections.

Network DevicesIntermediate11 min readJul 8, 2026
Analogies

Introduction

A load balancer distributes incoming network traffic across multiple backend servers so that no single server becomes overwhelmed, improving availability, reliability, and scalability. Load balancers are essential in any system that needs to serve traffic beyond what a single server can handle, and they operate at different layers of the network stack depending on how much information they use to make routing decisions.

🏏

Cricket analogy: A load balancer is like a captain rotating bowlers across an over so no single bowler gets overworked, distributing the workload across the attack to keep the team performing reliably through a long innings.

Explanation

A Layer 4 load balancer operates at the transport layer, making forwarding decisions based on IP addresses and TCP/UDP port numbers without looking at the actual application data. It typically works by forwarding or translating packets for an entire connection to a chosen backend server, keeping that mapping for the life of the connection. Because it does not need to parse application protocols, it is fast and has low overhead, but it cannot make decisions based on content such as a URL path, cookie, or HTTP header — it only sees connection-level information like source/destination IP and port.

🏏

Cricket analogy: A Layer 4 load balancer is like an usher directing spectators to a stand based only on their ticket's gate number, not which specific match they're watching, keeping the process fast but blind to seating preferences.

A Layer 7 load balancer operates at the application layer and can inspect the actual content of requests, such as the HTTP method, URL path, query string, headers, or cookies. This allows content-aware routing decisions — for example, sending requests for /api/* to one server pool and /images/* to another, or routing based on a session cookie for sticky sessions. Because it terminates the client connection and typically opens a new connection to the chosen backend, it requires more processing than Layer 4 balancing, but it enables far more intelligent, application-aware traffic distribution.

🏏

Cricket analogy: A Layer 7 load balancer is like a ground steward who reads your actual ticket details, members' pavilion versus general stand, and routes you to the correct entrance accordingly, taking a moment longer but getting you exactly where you belong.

Regardless of layer, load balancers use algorithms to choose which backend server receives each new connection or request. Round robin cycles through the list of available servers in order, sending each new request to the next server in the rotation, treating all servers as equally capable. Least connections instead sends each new request to whichever backend currently has the fewest active connections, which adapts better when requests have very different processing times or when servers have different capacities. Other algorithms include weighted round robin (servers get a share proportional to assigned weight) and IP hash (the same client is consistently routed to the same server based on a hash of its IP).

🏏

Cricket analogy: Round robin is like a captain giving every bowler an equal number of overs in strict rotation regardless of form, while least connections is like handing the ball to whichever bowler currently has the fewest runs conceded, adapting to who's actually performing lightest.

Example

python
# Simplified round robin vs least connections load balancing

servers = ["server-a", "server-b", "server-c"]
active_connections = {"server-a": 2, "server-b": 5, "server-c": 1}

# Round robin: cycles through servers in fixed order
rr_index = 0
def round_robin():
    global rr_index
    server = servers[rr_index % len(servers)]
    rr_index += 1
    return server

# Least connections: picks the server with the fewest active connections
def least_connections(active_connections):
    return min(active_connections, key=active_connections.get)

# Example: Layer 7 content-based routing
def layer7_route(http_request):
    if http_request.path.startswith("/api/"):
        return "api-server-pool"
    elif http_request.path.startswith("/static/"):
        return "static-server-pool"
    return "default-server-pool"

print(round_robin())            # e.g. 'server-a'
print(least_connections(active_connections))  # 'server-c' (only 1 connection)

Analysis

The choice between Layer 4 and Layer 7 load balancing, and the choice of algorithm, depends on the workload's needs. Layer 4 load balancing is ideal when raw throughput and low latency matter most and content-based routing is unnecessary — for example, balancing generic TCP traffic like a database proxy. Layer 7 load balancing is preferable when different types of requests should be routed to specialized backend pools, or when features like SSL termination, header inspection, or cookie-based session affinity are required, at the cost of extra CPU work to parse and proxy application traffic. Similarly, round robin works well when all requests are roughly equal in cost and all servers have similar capacity, but it can overload a server that happens to be handling several slow requests simultaneously since it ignores current load. Least connections adapts dynamically to real-time server load, making it a better fit for workloads with variable request processing times, though it requires the load balancer to continuously track connection counts, adding a small amount of state and complexity compared to the stateless simplicity of round robin.

🏏

Cricket analogy: Layer 4 balancing suits a simple net-practice queue where every ball takes the same time, while Layer 7 suits match-day routing where VIP and general fans need different handling; likewise round robin works when every bowler's over takes the same time, but least connections adapts better when one bowler is stuck bowling a slow, defensive spell.

Key Takeaways

  • Layer 4 load balancers route based on IP/port only, without inspecting application content; fast but not content-aware.
  • Layer 7 load balancers inspect HTTP-level content (URL, headers, cookies) enabling content-based routing and sticky sessions, at higher processing cost.
  • Round robin cycles requests evenly across servers regardless of current load.
  • Least connections sends new requests to the server with the fewest active connections, adapting to real-time load.

Practice what you learned

Was this page helpful?

Topics covered

#Python#ComputerNetworksStudyNotes#ComputerNetworks#LoadBalancersBasics#Load#Balancers#Explanation#Example#StudyNotes#SkillVeris