SkillSprout
Chapter 8 · Lesson 1
Beginner
3 min read

What Is a Load Balancer? Layer 4 vs Layer 7

Overview

A load balancer sits in front of a group of servers and spreads incoming requests across them. Users see one address; behind it, many servers share the work.

It is the piece that makes horizontal scaling usable: adding servers only helps if something sends them traffic.

Detailed Notes

What is a load balancer

A load balancer receives client requests and forwards each one to one of several backend servers. It is a special kind of reverse proxy (from the Proxies chapter): clients talk to it, and it talks to the servers.

Key terms:

  • Listener: the port and protocol the balancer accepts traffic on (for example HTTPS on port 443).
  • Backend pool (target group): the list of servers that can receive traffic.
  • Health check: a regular test that tells the balancer whether a server is alive.

The problem it solves

Scalability taught us that when one server is not enough, we add more servers (horizontal scaling). But then:

  • Which server should a user connect to?
  • What if one server dies? Users sent there get errors.
  • How do we add or remove servers without telling every user?

The load balancer answers all three. Clients know one address, the balancer picks a healthy server, and servers can come and go behind it.

How it works

  1. The client resolves the service name (DNS) and connects to the load balancer.
  2. The balancer accepts the request on its listener.
  3. It chooses a healthy server from the pool.
  4. It forwards the request, then returns the server's response to the client.

Balancers come in two main types, named after network layers:

  • Layer 4 (transport): looks only at IP addresses and ports (TCP/UDP). It never reads the request content, so it is very fast.
  • Layer 7 (application): understands HTTP. It can read the URL, host name, headers and cookies, so it can route /api to one pool and /images to another, and can decrypt HTTPS for the servers (TLS termination).
Layer 4Layer 7
Decides usingIP and portURL, host, headers, cookies
SpeedVery fastSlower (parses the request)
Content-based routingNoYes
Typical useRaw TCP/UDP, huge throughputWebsites and APIs

A simple example

A restaurant host seats guests at tables with space. A Layer 4 host only counts group size. A Layer 7 host also asks "patio or indoors?" before choosing.

System design example

An online shop runs four identical app servers behind a Layer 7 load balancer. During a sale, traffic jumps tenfold and the team adds more servers to the pool; users notice nothing. When one server is restarted for an update, health checks fail for it and the balancer sends traffic only to the other three.

One requirement matters: servers should be stateless (as covered in Scalability) so any server can answer any request. The balancer is also critical, so it must not be a single point of failure (the last lesson covers that).

Summary

  • A load balancer spreads requests across a pool of servers behind one address.
  • It enables horizontal scaling and hides failed servers from users.
  • Layer 4 routes on IP and port (fast); Layer 7 routes on HTTP content (flexible).
  • Stateless servers are what make "any server will do" true.

Architecture Diagram

A load balancer in front of a server pool
Architecture
Clients
Load BalancerListener 443

Backend pool

Server A
Server B
Server C

Requests are shared across healthy servers; unhealthy ones are skipped.

  1. 1Clients connect to the load balancer's single address.
  2. 2The listener accepts the request.
  3. 3The algorithm chooses a healthy server from the pool.
  4. 4The server's response returns through the balancer.

Common Interview Questions

Finished this lesson?

0 of 91 lessons done · 0% of the course