Layer 7 Load Balancing: Routing Each Request by Its Content
A Layer 7 load balancer reads each web request, such as its path, host name, headers, or cookies, and picks the backend server from what it finds.
In short
A Layer 7 load balancer reads each web request, such as its path, host name, headers or cookies, and picks the backend server from what it finds.
8 min readFree
What You Will Learn
Large websites run the same app on many servers. A load balancer sits in front of those servers and sends each incoming request to one of them, so no single machine takes all the work. This lesson covers one style of load balancer, called Layer 7. Layer 7 means the application layer, the top level of network work, where web requests and their contents live. A Layer 7 load balancer reads what each request asks for, then picks the server that should handle it.
You will learn how this works step by step, what it makes possible, what it costs, and when to choose it. A worked example on an online shop shows the idea in use.
Key words
- Load balancer
- A program or device that stands in front of several servers and sends each request to one of them.
- Request
- A message from a browser or app that asks a server for something, such as a web page.
- Backend server
- A server behind the load balancer that does the real work for a request.
- HTTP
- The set of rules browsers and servers use to send requests and replies on the web.
- Domain name
- The human-friendly name of a site, such as shop.example.com.
- URL path
- The part of a web address after the domain name, such as /checkout.
- Header
- Extra labelled information sent with a request, such as the browser type or the host name.
- Cookie
- A small piece of data a server asks the browser to store and send back with later requests.
- TLS termination
- Ending the encrypted HTTPS connection at the load balancer so it can read the plain request.
- Sticky session
- A rule that keeps one user going to the same backend server for a period of time.
How Does a Layer 7 Load Balancer Work?
A Layer 7 balancer works at the application layer, so it can see inside each request. It does not only pass along raw network traffic. Each request goes through four steps.
- 1
Accept the connection
The user connects to the load balancer, not to a backend server. The load balancer ends that connection itself, which is what lets it read the request.
- 2
Inspect the request
It reads the URL path, the host name, the headers, and any cookies. Some products also read the request method, such as GET for fetching a page or POST for sending a form.
- 3
Match a rule
It checks its routing rules in order and uses the first rule that fits the request.
- 4
Forward to a backend
It sends the request to a server in the chosen group, then returns that server's reply to the user.
Because the choice is made after reading the request, two requests from the same user can reach two different server groups. A search and a checkout can use the same domain name and still land on different servers.
What Does Layer 7 Make Possible?
Reading the request opens the door to features that a simpler balancer cannot offer. Four are common.
Content-based routing
Send each kind of request to the group of servers built for it, such as /api requests to API servers.
Sticky sessions
Keep one user on the same server for a while, by using a cookie.
TLS termination
Decrypt HTTPS at the balancer, so backend servers receive plain requests and do less work.
Caching and compression
Some Layer 7 products can keep copies of replies for reuse, or make replies smaller before they reach the user.
How Does Routing by Content Work?
Content-based routing is the main reason to choose Layer 7. Rules can match the host name, the path, a header, or a query string, which is the part of the address after a question mark. Many modern apps are built as microservices, which means one app is split into several small, separate services. Each service can run on its own servers. A Layer 7 balancer lets one public address front all of them. Users never see the split, and each service team can scale its own servers.
What Are Sticky Sessions with Cookies?
Some apps keep a user's login or shopping cart in the memory of one server. If the next request lands on a different server, the user may look logged out. A sticky session prevents this by sending the same user to the same server again. A Layer 7 balancer can do this with a cookie. A Layer 7 balancer can route a user to the same backend using cookies.
Tip
Sticky sessions should be used carefully, because too much affinity can cause uneven load. Many teams store session data in a shared place that every server can read, and use sticky sessions only when they must.
Why End Encryption at the Balancer?
HTTPS encrypts requests so that people on the network cannot read them. It uses TLS, which stands for Transport Layer Security. To read a request, the balancer must decrypt it first. Doing this at the balancer is called TLS termination. The balancer holds the site's certificate, which is the digital proof of the site's identity. It decrypts the request, reads it, and sends plain traffic on to the backend. The certificate is kept in one place, and backend servers skip the encryption work.
Worked Example: An Online Shop
Take a shop at shop.example.com. The name is made up for this lesson. Its pages come from three groups of servers: a search service, a payments service, and a media pool that holds product images and the logo. All three sit behind one domain name, so customers see one site.
- 1
A customer asks for checkout
The browser sends a request for /checkout over HTTPS. The balancer decrypts it and reads the path.
- 2
The checkout rule matches
The rule for /checkout sends the request to the payments service servers.
- 3
A search request arrives
A request for /search uses the same steps and goes to the search service servers.
- 4
The logo is requested
A request for /static/logo.png matches the media rule and goes to the media server pool.
- 5
The customer sees one site
The browser only knows shop.example.com. It never needs to know that three server groups are behind it.
rules = [("/checkout", "payments"), ("/search", "search"), ("/static/", "media"), ("/", "web")]
def pick(path):
for prefix, pool in rules:
if path.startswith(prefix):
return pool
for path in ["/checkout/confirm", "/search?q=shoes", "/static/logo.png", "/about"]:
print(path, "->", pick(path))
Output
/checkout/confirm -> payments /search?q=shoes -> search /static/logo.png -> media /about -> web
The sample shows the same idea in a few lines. It checks the path, tries the rules in order, and stops at the first match. A real balancer uses its own rule format, but the logic is the same.
The Trade-offs
Layer 7 is more flexible, but it is not free. A Layer 4 balancer works lower down. It routes by IP address and port and does not read what a request contains, so it is simpler and faster for raw traffic.
More flexible
Rules can use paths, host names, headers, and cookies. A Layer 4 balancer cannot see any of these.
More CPU work
Decrypting, reading, and parsing every request, which means breaking it into its parts, takes processing power.
Slightly more delay
Each request must be read and checked before it is forwarded.
Tip
Use Layer 7 when the routing decision needs request content. If you only need to spread connections evenly and the traffic is not HTTP, a simpler balancer is usually enough.
Choosing Layer 7
Choose Layer 7 when routing depends on the path, host name, header, or cookie. It also fits when you want TLS termination in one place, or when one domain fronts several services. Choose something simpler for traffic that is not HTTP, such as a database connection, where there is no web request to read.
Check Your Understanding
Quick check
A video site uses one domain. Requests to /upload should go to powerful servers, and requests to /watch should go to servers that lean on caching. The team proposes a simple balancer that only looks at IP addresses and ports. Will this work?
Quick check
A ticket site keeps each shopper's held seats in the memory of one server. The balancer sends each new request to any free server, and shoppers lose their held seats halfway through checkout. Which fix fits best?
Sources
- AWS Elastic Load Balancing documentation overview
- System Design Academy: What Is Layer 7 Load Balancing?
- A10 Networks: Layer 7 Load Balancer vs Layer 4: Key Differences Explained
- ITU Online: What Is Layer 7 Load Balancing?
- Contabo: Layer 4 vs Layer 7 Load Balancer: Differences, Use Cases & How to Choose
- Layrs: Layer 7 Load Balancing, System Design Interview Guide
- Cloudoptimo: What You Need to Know About AWS Application Load Balancer
- Ajit Singh: Load Balancing Explained: L4 vs L7
- Upclass.ai: L4 vs L7 load balancers: when to use each
Last updated on 17 November 2026
Related lessons
Learn with me
Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.