Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 29

Understanding Load Balancers and Traffic Distribution

Recommended resources

Hello! Welcome back to our module on Advanced Networking and Core Services.

In our last lesson, we explored how a client and server establish a secure connection using HTTPS and TLS certificates. This ensures that the communication for a single user is private and trustworthy. However, modern applications don't serve just one user; they serve thousands or millions. Relying on a single server is a recipe for disaster—it would quickly become overwhelmed, slow down, and eventually fail.

Today's lesson addresses this exact problem. We will focus on the learning outcome: Explain the concept of a load balancer and its role in distributing traffic. You will learn how load balancers act as the "traffic cops" of the internet, ensuring that application traffic is distributed efficiently across multiple servers to achieve high availability and scalability. This is a cornerstone concept in DevOps, essential for building robust and resilient systems.

1. The "Too Much Traffic" Problem

Imagine a popular e-commerce site on Black Friday. If all customer requests were sent to a single web server, that server would be overwhelmed by the sheer volume of connections. Response times would skyrocket, and the server might crash, taking the entire site offline and costing the business revenue.

The solution is not just to have a more powerful server, but to have more servers. But how do you direct users to all these different servers efficiently? This is where load balancing comes in.

A load balancer is a dedicated device (either hardware or software) that sits in front of a group of servers and distributes incoming network traffic across them.

This diagram shows a load balancer distributing incoming requests from clients to a farm of three backend servers. This prevents any single server from being overwhelmed and ensures the service remains available even if one server fails.


2. The Core Role and Benefits of a Load Balancer

At its heart, a load balancer's job is to make a group of servers look like a single, highly reliable one. To understand this better, let's dive into an excellent introductory article.

What is Load Balancing and How Does It Work?

This article from HAProxy, a popular software load balancer, provides a great overview of what load balancing is and why it's so important.

Please read the introduction, the section 'What is a load balancer?', and the glossary table under 'What terms should you know?'. Focus on the main purpose of a load balancer and familiarize yourself with terms like 'High availability', 'Failover', 'Health checks', and 'Scalability'.

As you read, you'll see that load balancing provides several key benefits that are critical for any production system:

  • High Availability & Redundancy: By distributing traffic across multiple servers, you eliminate a single point of failure. If one server goes down (for maintenance or due to a crash), the load balancer detects this and automatically redirects traffic to the remaining healthy servers. This process is called failover, and it ensures your application stays online.
  • Scalability: When traffic increases, you don't have to replace your single server with a more powerful (and expensive) one. Instead, you can simply add more affordable servers to the group. The load balancer will automatically start sending traffic to them. This is known as horizontal scaling.
  • Performance: By distributing the load, you ensure that all users get a fast response time because no single server is overworked.
  • Security: As the load balancer sits between the client and the server, it acts as a reverse proxy, hiding your backend infrastructure and potentially providing a single point to manage security functions like decrypting SSL/TLS traffic.

3. How Do Load Balancers Choose a Server?

A load balancer needs a strategy for deciding which backend server should get the next incoming request. This strategy is called a distribution algorithm. The choice of algorithm can significantly impact the performance and efficiency of your application.

Let's explore some of the most common algorithms.

API Gateway vs Load-Balancer

This article provides clear explanations of the most common traffic distribution algorithms used by load balancers.

Please read the section 'Load Balancer in Practice' and focus on the subsection 'Key Features of Load Balancers'. Pay close attention to the descriptions of the different 'Traffic Distribution Algorithms' (Round Robin, Least Connections, IP Hash).

Here's a summary of the most important algorithms:

  • Round Robin: This is 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's like dealing cards one by one to a group of players. It works well when all servers are roughly equal in power and are handling similar types of requests.
  • Least Connections: This is a more dynamic algorithm. The load balancer keeps track of how many active connections each server has and sends the new request to the server with the fewest connections. This is useful when some requests take longer to process than others, as it prevents one server from getting bogged down.
  • IP Hash: The load balancer calculates a hash of the client's IP address and uses this hash to determine which server receives the request. The result is that a user from a specific IP address will always be sent to the same server. This is essential for applications that require session persistence (or "sticky sessions"), where user-specific data (like items in a shopping cart) is stored on the server for the duration of a session.

Don't Forget Health Checks!

An algorithm is only effective if the load balancer knows which servers are actually operational. This is accomplished through health checks. The load balancer periodically sends a small request (like a ping or an HTTP request) to each backend server to check its status.

If a server fails to respond correctly to a health check, the load balancer marks it as "unhealthy" and temporarily removes it from the pool of available servers. Once the server starts responding correctly again, it is automatically added back into rotation. This mechanism is the foundation of achieving high availability.

Test your understanding!

You are managing an e-commerce website where a user's shopping cart data is stored in memory on the web server they connect to. Which load balancing algorithm would be most appropriate to ensure a user doesn't lose their cart when they click to a new page? Why?

Show answer

The IP Hash algorithm (or another method for enabling sticky sessions) would be the most appropriate. This algorithm ensures that all requests from the same user (identified by their IP address) are consistently routed to the same backend server. If you used Round Robin, the user's second request might go to a different server that has no knowledge of their shopping cart, leading to a poor user experience.


4. Layer 4 vs. Layer 7 Load Balancing

Not all load balancers are the same. A crucial distinction, especially in DevOps, is whether a load balancer operates at Layer 4 or Layer 7 of the OSI model.

  • Layer 4 (Transport Layer) Load Balancer: This type of load balancer makes routing decisions based on information from the network transport layer (TCP/UDP). It looks at the source/destination IP addresses and ports. It doesn't inspect the content of the traffic itself.

    • Pro: Very fast and efficient.
    • Con: Not "application-aware." It can't make decisions based on the type of content being requested (e.g., an image vs. an API call).
  • Layer 7 (Application Layer) Load Balancer: This is a more sophisticated load balancer that can inspect the content of the traffic, such as HTTP headers, URLs, and cookies.

    • Pro: Enables intelligent, content-based routing. For example, you can route requests to /api/* to your API servers and requests to /images/* to a different set of servers optimized for serving static files. It can also handle tasks like SSL termination (decrypting HTTPS traffic).
    • Con: Slower than Layer 4 because it has to do more processing for each request.

In modern cloud environments, you'll often use a combination of both. For example, the diagram in the following resource shows a common microservices architecture where both external and internal load balancers are used.

API Gateway vs Load-Balancer

This diagram illustrates how load balancers are used in a real-world, complex architecture. It shows how different types of load balancers can work together.

Scroll down to the section 'The Synergy: How They Work Together' and study 'Figure 2: API Gateway and Load Balancer in a Microservices Architecture'. Pay attention to the roles of the 'External Load Balancer' and the 'Internal Load Balancer' and how they manage traffic at different stages.

In this architecture:

  1. An External Load Balancer faces the public internet, distributing traffic across multiple API Gateway instances. This is for high availability of the entry point itself.
  2. An Internal Load Balancer sits behind the API Gateway and distributes traffic among the various backend microservices. This allows each individual service to be scaled and managed independently.

Conclusion

In this lesson, you've learned how load balancers form the backbone of scalable and resilient applications. They solve the critical problem of handling high traffic volumes by distributing requests intelligently across a fleet of servers.

Key Takeaways:

  • A load balancer distributes network traffic across multiple backend servers to improve application availability, scalability, and performance.
  • Key benefits include high availability through failover, easy scalability by adding more servers, and improved user experience.
  • Load balancers use algorithms like Round Robin, Least Connections, and IP Hash to decide where to send traffic.
  • Health checks are essential for ensuring traffic is only sent to operational servers.
  • Layer 4 load balancers are fast and operate at the network level, while Layer 7 load balancers are application-aware and enable more intelligent routing.

In our next lesson, we'll shift our focus from managing traffic flow to securing our network perimeter. We will learn how to implement basic firewall rules using ufw to control what kind of traffic is allowed to reach our servers in the first place.