No history yet

Scaling Request Traffic

Scaling Up or Scaling Out

A single server can only handle so much traffic before it slows down or crashes. When your application becomes popular, you have two primary ways to handle the increased load: vertical scaling and horizontal scaling.

Vertical scaling, or scaling up, means making your server more powerful. You add more CPU, RAM, or faster storage to the existing machine. Think of it like swapping out a car's four-cylinder engine for a V8. It's the same car, just more powerful. This approach is simple to implement but has significant limits. There's a physical cap on how powerful a single machine can be, costs increase exponentially, and you still have a single point of failure. If that one super-server goes down, your entire application is offline.

Horizontal scaling, or scaling out, involves adding more servers to share the load. Instead of one powerful server, you might have ten less powerful ones working together. This is like a grocery store opening more checkout lanes instead of trying to find one superhumanly fast cashier. This approach is more complex to set up but is far more resilient and cost-effective at scale. It forms the foundation of modern distributed systems.

Horizontal scaling is the standard for building resilient, high-traffic applications. It avoids a single point of failure and allows for nearly limitless growth.

To make horizontal scaling work, you need a system that can intelligently distribute incoming requests among your fleet of servers. This system is called a load balancer.

The Traffic Director

A load balancer acts as a reverse proxy, sitting in front of your application servers and routing client requests across all of them. To the outside world, your service appears as a single entity with a single IP address. Internally, the load balancer ensures no single server gets overwhelmed, improving response times and increasing availability.

Load balancers can operate at different layers of the , primarily Layer 4 (Transport) and Layer 7 (Application). The layer you choose determines how much information the load balancer has to make its routing decisions.

Layer 4 (L4) Load Balancing An L4 load balancer works at the transport layer, dealing with TCP and UDP packets. It doesn't inspect the content of the packets. It simply forwards traffic based on IP addresses and port numbers. Because it does less work, it's extremely fast and efficient.

Layer 7 (L7) Load Balancing An L7 load balancer operates at the application layer, which means it can inspect the content of the traffic, such as HTTP headers, URLs, and cookies. This allows for much smarter routing decisions. For example, an L7 load balancer can route requests to /api/video to a set of servers optimized for video processing, while requests to /api/images go to a different set of servers. This content-aware routing is powerful but comes with higher latency and computational cost compared to L4.

Distribution Algorithms

A load balancer needs a strategy for deciding which server gets the next request. This is determined by its configured algorithm. While many algorithms exist, three are fundamental.

AlgorithmHow It WorksBest For
Round RobinDistributes requests sequentially to each server in a list, looping back to the start.Evenly powered servers and stateless traffic.
Least ConnectionsSends the next request to the server with the fewest active connections.Workloads where requests have varying completion times.
IP HashUses the client's source IP address to calculate a hash, which determines which server receives the request.Applications requiring session persistence (sticky sessions).

The choice of algorithm depends entirely on your application's needs. Round Robin is simple and effective for uniform traffic. Least Connections is more dynamic, adapting to servers that might be bogged down by a long-running request. IP Hash is crucial when a user's subsequent requests must land on the same server, a concept known as or "sticky sessions."

For example, if a user's shopping cart data is stored in memory on a specific server, all their requests for that session must be routed to that same server. If a request went to a different server, the shopping cart would appear empty. IP Hash ensures this consistency by making the routing decision predictable based on the user's IP.

Staying Healthy

Distributing traffic is only half the battle. What happens if one of your application servers crashes or becomes unresponsive? Sending users to a dead server is a surefire way to create a bad experience. This is where health checks come in.

A load balancer periodically polls its registered servers to ensure they are healthy and able to handle traffic. This is often done by sending a request to a specific endpoint, like /health, and expecting a 200 OK response.

If a server fails a health check, the load balancer temporarily removes it from the pool of available servers. It will stop sending traffic to the unhealthy server until it begins passing health checks again. This process of automatically adding and removing servers from the pool is a core principle of high availability.

Health checks turn a potential outage into a non-event for the end-user by transparently routing traffic away from failed nodes.

This automated management is part of a broader concept called , where components in a distributed system can find and communicate with each other dynamically without hardcoded addresses. As new servers come online (for example, due to auto-scaling), they register themselves with a central registry. The load balancer then queries this registry to know which servers are available to receive traffic.

Now, let's test your understanding of these scaling concepts.

Quiz Questions 1/5

An e-commerce website is preparing for a massive holiday sale. They expect a 10x increase in traffic and decide to add a fleet of new, identical servers to their infrastructure. What is this scaling strategy called?

Quiz Questions 2/5

A load balancer that routes traffic based on the content of a request, such as the URL or HTTP headers, is operating at which layer of the OSI model?

By combining horizontal scaling with intelligent load balancing and automated health checks, you can build systems that are not only fast but also resilient, capable of handling millions of requests without a single point of failure.