Quick revision
The short answer
The old layer 7 balancer passed each client's address in an X-Forwarded-For header; the new layer 4 balancer either proxies the TCP connection or applies source NAT, so the service now sees only the balancer's few internal addresses, and the rate limiter keyed on source IP puts all users into a handful of buckets. We get the addresses back with the PROXY protocol enabled on both the balancer and the server, or a balancer mode that preserves the client IP, trusting forwarded addresses only from known proxies and preferring API keys for rate limiting.
What to remember
In this answer, we saw that the old layer 7 balancer passed client addresses in X-Forwarded-For, while the new layer 4 balancer proxies or source-NATs connections, so the service saw only the balancer's addresses and the IP-keyed rate limiter blocked everyone together. We restore addresses with the PROXY protocol on both balancer and server, or an address-preserving mode such as externalTrafficPolicy: Local with its uneven-load trade-off, trust forwarded data only from known proxies, and prefer authenticated identities for rate limiting.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The old layer 7 balancer passed each client's address in an X-Forwarded-For header; the new layer 4 balancer either proxies the TCP connection or applies source NAT, so the service now sees only the balancer's few internal addresses, and the rate limiter keyed on source IP puts all users into a handful of buckets. We get the addresses back with the PROXY protocol enabled on both the balancer and the server, or a balancer mode that preserves the client IP, trusting forwarded addresses only from known proxies and preferring API keys for rate limiting.
Why this question is asked
Moving from a layer 7 to a layer 4 balancer looks like an infrastructure change that the application should not notice. But many applications quietly depend on something the layer 7 balancer did for them: telling them who the client is. Interviewers ask this to see whether we understand how client addresses travel through proxies, and whether we can restore them safely, without letting clients forge their own address. In this answer, we will see what the old balancer provided, why the new one hides the address, why the rate limiter collapsed, and then go through the fixes: the PROXY protocol, address-preserving modes, and trusted proxy rules, ending with the Spring Boot configuration.
1What the Layer 7 Balancer Used to Do
A layer 7 balancer terminates the client's connection and opens a new one to the service. From the service's point of view, every TCP connection comes from the balancer. To keep the original information, the balancer adds headers to each HTTP request: X-Forwarded-For with the client's IP, and usually X-Forwarded-Proto and X-Forwarded-Host, or the standard Forwarded header. The service, configured to trust those headers from the balancer, used them as the client address.
The rate limiter was keyed on that address, so each client had its own bucket.
2Why the Layer 4 Balancer Hides the Address
A layer 4 balancer does not parse HTTP, so it cannot add headers. Depending on its mode, it either proxies the TCP connection itself, opening a new connection from its own address, or forwards packets with source NAT, rewriting the source address to one of its own so replies come back through it. In both cases the service sees the balancer's internal addresses as the source of every connection, and there is no X-Forwarded-For anymore.
With a handful of balancer nodes, every request appears to come from a handful of IPs. A rate limiter allowing 100 requests per minute per IP now allows 100 per minute for everyone behind each balancer node, so nearly all users are blocked together.
| Setup | Source address at the service | Client IP available? |
|---|---|---|
| L7 balancer | balancer IP | yes, X-Forwarded-For header |
| L4 proxy or source NAT | balancer IP | no, only a few internal IPs |
| L4 with PROXY protocol | balancer IP | yes, in the PROXY header |
| L4 preserving source IP | client IP | yes, directly |
3Fix 1: The PROXY Protocol
The PROXY protocol, defined by HAProxy and supported by most cloud and software balancers, solves exactly this problem at layer 4. When the balancer opens a connection to the backend, the first bytes it sends are a small header with the original source and destination addresses and ports, before any application data. Version 1 is a human-readable text line; version 2 is a compact binary format that can carry extra fields.
Both sides must agree. The balancer must be configured to send it, and the server must be configured to expect it, because a server not expecting it will read the header as the start of an HTTP request and fail, and a server expecting it will reject connections that lack it.
Notice that the protocol is a connection-level header, so it works for the new TCP protocol as well as for HTTP, which is why it fits this team's reason for moving to layer 4.
4Fix 2: Preserve the Client IP Without Proxying
Some layer 4 balancers can forward packets without rewriting the source address, so the service sees the real client IP directly; cloud network load balancers often offer this. In Kubernetes, a Service of type LoadBalancer with externalTrafficPolicy: Local keeps the client's address, because traffic is only delivered to pods on the node that received it, with no second hop through another node.
The catch is uneven load. With Local, the balancer sends traffic only to nodes that have a pod of the service, and each node gets an equal share regardless of how many pods it runs. A node with one pod gets the same traffic as a node with three, so pods can be loaded very differently. Spreading pods evenly across nodes reduces the effect.
5Trust and a Better Rate Limit Key
Whichever mechanism we use, we must trust address information only from proxies we control. If the service accepts X-Forwarded-For from anyone, a client can send X-Forwarded-For: 1.2.3.4 and choose its own rate limit bucket. The service should accept forwarded addresses only when the connection comes from a known proxy range, and take the right-most untrusted address in the chain.
Better still, rate limits for an API should be keyed on an authenticated identity, such as an API key or user id, when one exists. IP addresses are shared by many users behind NAT and mobile carriers, and change for one user, so they are a weak key. IP-based limits remain useful for unauthenticated endpoints such as login.
6Testing the Fix
Before switching traffic again, we can test in staging: send requests from two known client addresses through the layer 4 balancer and check that the application logs show the right addresses and that each gets its own rate limit bucket. We also send a forged X-Forwarded-For from outside and confirm it is ignored. Adding the observed client address to access logs makes a regression visible immediately.
7Follow-up questions
How would you configure Spring Boot to use forwarded headers safely?
Set server.forward-headers-strategy to native, which uses the servlet container's RemoteIpValve on Tomcat, or to framework, which uses Spring's ForwardedHeaderFilter. With Tomcat's native support, configure server.tomcat.remoteip.internal-proxies to a pattern matching only your proxy addresses, so X-Forwarded-For is honoured only on connections from them; request.getRemoteAddr() then returns the client address. Check your Boot version for the exact property names, and never trust the header from arbitrary sources.
Why not just switch back to the layer 7 balancer?
For HTTP traffic that might be the simplest answer, but the team moved to layer 4 to support a new TCP protocol that the layer 7 balancer cannot handle. A common resolution is to run both: layer 7 for HTTP routes and layer 4 with the PROXY protocol for the TCP protocol.
What happens if the balancer sends PROXY headers but the server does not expect them?
Every connection fails, because the server reads 'PROXY TCP4 ...' as the start of the application protocol and rejects it as malformed. That is why the server should be configured first, ideally accepting both for a short transition, and then the balancer is switched on.
8Conclusion
In this answer, we saw that the old layer 7 balancer passed client addresses in X-Forwarded-For, while the new layer 4 balancer proxies or source-NATs connections, so the service saw only the balancer's addresses and the IP-keyed rate limiter blocked everyone together. We restore addresses with the PROXY protocol on both balancer and server, or an address-preserving mode such as externalTrafficPolicy: Local with its uneven-load trade-off, trust forwarded data only from known proxies, and prefer authenticated identities for rate limiting.
Open with
The layer 7 balancer terminated each connection and added an X-Forwarded-For header with the client's address, and the service was configured to trust it. The new layer 4 balancer does not understand HTTP, so it adds no headers, and it either proxies the TCP connection or applies source NAT.
Then, if they want more
So the service now sees only the balancer's few internal IPs as the source, and the rate limiter, keyed on source IP, puts all users into a handful of buckets that hit the limit together.
To get the addresses back, I would enable the PROXY protocol on the balancer and configure the server to expect it, so each connection starts with a small header carrying the original client address and port; version 1 is text and version 2 binary. Both sides must change together or connections fail.
Alternatively, some layer 4 balancers can preserve the client IP, and in Kubernetes externalTrafficPolicy: Local keeps the source address, at the cost of uneven load across nodes. I would trust forwarded addresses only from known proxy ranges, and where possible key the rate limiter on an API key or user id instead of the IP.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What happened to the client addresses, and how do you get them back?": https://scrivio-interview-library.netlify.app/architecture/load-balancing-networking/a4c9423cbe/