What is Load Balancer

A load balancer is like a traffic cop sitting in front of your servers. Its job is to distribute incoming network traffic across multiple servers so no single server gets overwhelmed — improving performance, reliability, and uptime.

Imagine a busy highway with several lanes — if all vehicles tried to use just one lane, there would be constant traffic jams. Similarly, if all user requests go to one server, it would slow down or even crash. A load balancer steps in to direct each request to the least busy or most appropriate server, balancing the load evenly.


🌐 OSI (Open Systems Interconnection) Model Basics (Why Layer 4 and Layer 7?)

The OSI model has 7 layers. For this topic:

  • Layer 4 = Transport Layer (deals with TCP/UDP ports, IP addresses)
  • Layer 7 = Application Layer (deals with HTTP, HTTPS, cookies, headers)

🔁 What is a Layer 4 Load Balancer?

✅ Layer 4 LB: Think of it as a “Postman who only cares about the address.”

  • Works based on IP address and TCP/UDP ports.
  • Doesn’t know what’s inside the request (doesn’t read HTTP data).
  • Faster and more lightweight.
  • Used for raw TCP/UDP traffic (e.g., FTP, SMTP, custom protocols, even HTTP if you don’t need inspection).

🏗️ Architecture

        Client
          |
    +-------------+
    | Layer 4 LB  |  ← (Decides based on IP/Port)
    +-------------+
     /     |     \
Server1 Server2 Server3
  • It sees: “Client wants to go to IP:port”, and forwards to one backend based on that.

📦 Example:

Let’s say you have a game server using TCP on port 27015.

  • Client sends request to the LB at game.example.com:27015.
  • Layer 4 LB forwards the TCP packet to one of the game servers (based on round robin, least connection, etc).

🧠 What is a Layer 7 Load Balancer?

✅ Layer 7 LB: Think of it as a “Smart receptionist who reads your letter.”

  • Works at HTTP/HTTPS level, understands the full web request.
  • Can route traffic based on:
    • URL paths (/api, /login)
    • Hostnames (api.example.com, www.example.com)
    • Headers, Cookies, or even Payload content.
  • Can do SSL termination, caching, authentication, etc.
  • Slightly slower than Layer 4, but smarter.

🏗️ Architecture

        Client (Browser)
           |
    +-------------------+
    |   Layer 7 LB      |  ← (Inspects HTTP requests)
    +-------------------+
     |      |       |
   Web1   Web2    API Server
  • It sees: “This is an HTTPS request for /api/user”, and forwards it to API server.
  • For /images, it can send to a different server.

📦 Example:

Your website has:

  • www.example.com for frontend
  • api.example.com for APIs

With Layer 7 LB:

  • If request is for www.example.com, send to frontend servers
  • If request is for api.example.com, send to backend API servers

⚖️ Layer 4 vs Layer 7 — Comparison Table

FeatureLayer 4 LBLayer 7 LB
OSI LevelTransport (TCP/UDP)Application (HTTP/HTTPS)
Routing Based OnIP address + PortURL, Hostname, Cookies, Headers
Protocol SupportTCP, UDPHTTP, HTTPS
SSL Termination❌ No✅ Yes
Content Awareness❌ No✅ Yes
Speed✅ Faster⚠️ Slower (but smarter)
Use CaseGame servers, Mail, DBWeb apps, APIs, Microservices

🛠️ Real-World Tools (Examples)

Load BalancerLayer SupportNotes
HAProxyLayer 4 & 7Open-source, very flexible
NginxPrimarily Layer 7Can also work in Layer 4 mode
AWS ELBL4 & L7Application LB = Layer 7, Network LB = L4
Kubernetes ServicesL4 (default), L7 via Ingress
CloudflareLayer 7Reverse proxy & security focus

📌 Summary

Use ThisWhen You Need
Layer 4 LBHigh speed, low overhead, simple routing
Layer 7 LBAdvanced rules, content-based routing, SSL termination

About Anant 449 Articles
Senior technical writer