Layer 4 Load Balancing: Spreading Connections by Address and Port Alone
How a transport-layer balancer picks a server for each connection using only IP addresses and ports, why it is fast, and when to choose it over Layer 7.
In short
A Layer 4 load balancer picks a server for each connection using only IP addresses and ports, which makes it fast but unaware of the request content.
10 min readFree
What you will learn
A load balancer stands in front of a group of servers and decides which server receives each piece of incoming work. This lesson covers one kind, the Layer 4 load balancer. It makes its choice using only the address and port details that sit on the outside of each network message, so it never opens the message to read what is inside. By the end, you should be able to explain how it works, list what it can and cannot do, and decide when it is the right tool.
Key words
- Load balancer
- A machine or service that spreads incoming traffic across several servers, so no single server gets overloaded.
- Backend server
- One of the real servers behind the balancer, the one that does the actual work.
- IP address
- A number that identifies a machine on a network, such as the sender or the receiver of a message.
- Port
- A number that points to one service on a machine. For example, port 443 is the usual port for secure web traffic.
- TCP and UDP
- Two transport protocols. TCP keeps a connection open between two machines. UDP sends small independent packets with no connection set up first.
- Packet
- A small chunk of data sent across a network. Each packet has a header with addresses and ports, followed by the payload, which is the actual content.
- OSI model and layers
- A way of sorting network jobs into numbered levels. Layer 4 is the transport level, where TCP and UDP live. Layer 7 is the application level, where web requests such as HTTP are read.
How does a Layer 4 balancer work?
Clients send their connections to one public address that belongs to the balancer. The balancer then hands each connection to one backend server. The client usually does not know which backend it reached, because the balancer answers for the whole group.
The balancer makes this choice by looking at a small set of facts. Those facts are the transport-level details of the packet, not the meaning of the data.
What it looks at
The sender's IP address and port, the receiver's IP address and port, and the protocol, which is TCP or UDP. That is the whole picture the balancer has.
What it cannot see
The payload. It does not read URLs, HTTP headers, cookies, or the text of a database command. To the balancer, the payload is just a block of bytes.
When it decides
It makes one decision per connection. For UDP, where there is no connection, it treats a stream of packets from the same sender and port as one flow and keeps that flow together.
Next, the balancer has to deliver the packets to the chosen server and make sure replies come back the right way. It usually does this in one of several ways. In one approach, it rewrites the address details in each packet. This is called NAT, short for network address translation. In the other approach, it forwards the packet while keeping the original destination. The key point is the same in both: the balancer makes sure all packets of one connection reach the same backend.
- 1
A new connection arrives
The first packet of a new connection reaches the balancer's public address and port.
- 2
The balancer picks a backend
It checks its list of backend servers and chooses one using a simple rule. The rule only needs the connection details, not the content.
- 3
Packets are forwarded
The balancer sends the packet to the chosen backend, often by rewriting the address details so the backend can reply through the balancer.
- 4
Later packets follow the same path
Every further packet from that connection goes to the same backend, so the conversation stays on one server until it ends.
The small program below is a teaching model, not how any real product is built. It shows the core idea: the first packet of each connection picks a backend, and later packets of that same connection reuse the choice.
backends = ["10.0.0.11:443", "10.0.0.12:443", "10.0.0.13:443"]
table = {}
next_index = 0
def route(src_ip, src_port):
global next_index
key = (src_ip, src_port)
if key not in table:
table[key] = backends[next_index % len(backends)]
next_index += 1
return table[key]
packets = [
("203.0.113.5", 51000),
("203.0.113.5", 51000),
("203.0.113.5", 51000),
("203.0.113.9", 40022),
("203.0.113.5", 51001),
]
for ip, port in packets:
print(f"{ip}:{port} -> {route(ip, port)}")
Output
203.0.113.5:51000 -> 10.0.0.11:443 203.0.113.5:51000 -> 10.0.0.11:443 203.0.113.5:51000 -> 10.0.0.11:443 203.0.113.9:40022 -> 10.0.0.12:443 203.0.113.5:51001 -> 10.0.0.13:443
Read the output closely. The first three packets share the same sender address and port, so they all reach 10.0.0.11. A new sender gets the next backend in line, and a new port from the same address counts as a new connection. The program never reads a payload. Real Layer 4 balancers also decide from address and port details, though large-scale ones add BGP, equal-cost multipath, and consistent hashing across identical machines.
What are its strengths and limits?
Layer 4 balancing trades intelligence for speed. Because it looks at so little, it does little work for each packet. That gives it a clear set of strengths and a clear set of limits.
Very high speed
Sources that compare the two levels describe Layer 4 as fast and low in performance cost, because it makes decisions from connection details alone and needs little processing per packet.
Low CPU cost
A balancer that does not parse messages needs less processing power. That means one machine can carry a lot of connections for the same cost.
Any TCP or UDP protocol
Because it does not care what the data means, it can carry database traffic, game traffic, video streams, mail, and plain web traffic. It is not limited to HTTP.
The limits follow from the same design. A Layer 4 balancer cannot route by URL path, host name, HTTP header, or cookie, since it never reads those fields. It also cannot decrypt secure traffic in the normal case, because the encrypted data is only bytes to it. Finally, it cannot make a choice based on what a request asks for. It can only spread connections.
Tip
If your rule sounds like 'send this page to that group', you need a Layer 7 balancer, which reads the request. A Layer 4 balancer can only say 'send this connection to some server in this group'.
Worked example: an AWS Network Load Balancer on port 443
Amazon Web Services sells a Layer 4 product called the Network Load Balancer. It is a good real-world case because its settings show the ideas above. In this example, the balancer accepts connections on TCP port 443 and sends each connection to one of several servers. It does not decrypt the traffic or read the HTTP requests inside it.
Setting up the example has three parts: a group of servers, a listener, and a forward rule. The screens below use the AWS console names. The exact labels can change over time, so check the current console if a label differs.
- 1
Create a target group
Choose the TCP protocol and port 443 for the target group, then register your backend servers in it. A target group is the pool of servers that the balancer can send traffic to. Its port and protocol are set here.
- 2
Create the balancer
Choose the Network Load Balancer type. It works at the transport layer, so it only needs IP addresses, ports, and the protocol to do its job.
- 3
Add a TCP listener on port 443
A listener is the part that waits for connection requests on a chosen protocol and port. Pick TCP, not TLS. With a TCP listener, the balancer passes encrypted traffic to the servers without decrypting it.
- 4
Forward the listener to the target group
Set the listener's default action to forward connections to the target group you made. The balancer now has one rule: send each new connection to a registered server.
- 5
Check the target group screen
On the target group page in the AWS console, you can see the servers registered in the group. This is the list the balancer chooses from for each new connection.
Once this is running, a visitor's secure connection reaches the balancer on port 443. The balancer picks a server and forwards the packets. The server that receives the packets is the one that does the TLS handshake and reads the HTTP request. The balancer only moves the bytes.
Tip
Choose a TLS listener only when you want the balancer to decrypt traffic. Choose a TCP listener on port 443 when the servers must decrypt it themselves. This is a choice you make in the listener settings, and it changes what the balancer can see.
When should you choose Layer 4?
Choose a Layer 4 balancer when the job is to move a large number of connections quickly and no part of the request needs to be read. The three cases below cover most of the good fits.
Raw throughput
When you need to move a very large volume of traffic with little processing, Layer 4 keeps the balancer cheap and fast.
Non-HTTP traffic
Databases, mail, game servers, and video streams do not speak HTTP. A Layer 4 balancer handles them because it works with any TCP or UDP protocol.
A front tier
Many systems use Layer 4 as the first tier. It spreads raw connections across a fleet of Layer 7 balancers, and those balancers make the smart, request-level decisions.
Quick check
A small studio runs a racing game. Players send UDP packets to port 7777 on a fleet of game servers, and the team wants the cheapest, fastest way to spread the packets across the servers. Which balancer fits best, and why?
How does Layer 4 compare with Layer 7?
Layer 4 is the contrast point for Layer 7 balancing. A Layer 7 balancer ends the client's connection, reads the full request, and can route by URL path, host name, header, or cookie. That gives it more control, but it does more work for every request. A Layer 4 balancer does less work and cannot see inside the request. Many real systems use both. The Layer 4 tier spreads raw connections, and the Layer 7 tier makes the request-level choices behind it.
Quick check
A team runs a MySQL database on TCP port 3306 across three servers. The same team also runs a website where /login and /images go to different server groups. Which balancer should handle each job?
Key takeaways
Address and port only
A Layer 4 balancer decides from IP addresses, ports, and the TCP or UDP protocol. It never reads the content of the data.
One server per connection
It picks a backend for a connection and keeps every packet of that connection on the same server.
Fast, but blind
It is fast and works with any TCP or UDP protocol, but it cannot route by URL, header, or cookie.
When you design a system, ask one question first: does any part of the routing decision depend on what the request says? If not, Layer 4 is a strong choice. If yes, use Layer 7 for those paths, and consider a Layer 4 tier in front of it for raw volume.
Sources
- AWS Elastic Load Balancing: Listeners for your Network Load Balancers
- AWS Elastic Load Balancing: Target groups for your Network Load Balancers
- AWS Elastic Load Balancing: Network Load Balancers overview
- Systemdesign.academy: What Is Layer 4 Load Balancing? Definition and Examples
- DNS Panopticon: Load Balancers, Layer 4 vs Layer 7
- OneUptime: How to Implement Layer 4 vs Layer 7 Load Balancing
- OneUptime: How to Set Up Nginx as a TCP/UDP Load Balancer for IPv4 Traffic
- Factory open-source wiki: NGINX stream proxy
- Contabo: Layer 4 vs Layer 7 Load Balancer, Differences, Use Cases and How to Choose
- A10 Networks: Layer 7 Load Balancer vs Layer 4, Key Differences Explained
- GeeksforGeeks: Layer 4 Load Balancing vs. Layer 7 Load Balancing
- Educative: Create an AWS Load Balancer
- Medium: Layer 4 vs Layer 7 Load Balancing by Kalana Malshan
Last updated on 17 November 2026
Related lessons
- System designLayer 7 Load Balancing: Routing Each Request by Its Content
- System designLoad Balancing Algorithms: How a Balancer Picks the Right Server
- System designApplication Layer: Where Business Logic Runs Between Web Servers and Data
- System designLoad Balancers: Sharing Requests Across Servers and Skipping Broken Ones
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.