Scrivio Interview library
ConceptFoundations5 min read · updated 13 November 2026

Load balancers work at different layers. A layer 4 balancer forwards TCP or UDP connections without reading their content; a layer 7 balancer understands the application protocol, usually HTTP, and handles each request.

Explain what each kind can see and do, and which you would choose for a public REST API, a PostgreSQL connection pool endpoint and a game server using UDP.

A layer 4 balancer sees only addresses, ports and the transport protocol; it picks a backend once per connection and forwards the bytes, which makes it fast and protocol-agnostic but unable to route by URL or retry a single request. A layer 7 balancer terminates the connection, parses HTTP and can route by host, path or header, balance each request, retry, rewrite headers and terminate TLS. I would use layer 7 for the public REST API and layer 4 for the PostgreSQL endpoint and the UDP game server.

Why this question is asked

Every request to a production service passes through at least one load balancer, and the layer it works at decides what it can do for us. Choosing wrongly gives either a balancer that cannot route our API by path, or one that tries to parse a protocol it does not understand. Interviewers ask this to check that we know what each layer can see, and can reason from that to a choice for different traffic. In this answer, we will recap the layers, look at what a layer 4 and a layer 7 balancer each see and do, compare them, choose for the three workloads, and see how large platforms combine both.

1A Quick Recap of the Layers

The layer numbers come from the OSI model. Layer 4 is the transport layer: TCP and UDP, identified by IP addresses and port numbers. Layer 7 is the application layer: the protocol the application speaks over that transport, such as HTTP, gRPC or the PostgreSQL wire protocol.

A load balancer working at layer 4 makes its decisions using only transport information. One working at layer 7 reads the application protocol, which gives it much more information and much more work to do per request.

2What a Layer 4 Balancer Sees and Does

A layer 4 balancer sees the source and destination addresses and ports and whether the traffic is TCP or UDP. When a new TCP connection arrives, it picks a backend, using round robin, least connections or a hash of the addresses, and from then on every packet of that connection goes to the same backend. It either forwards packets with address translation or proxies the byte stream, without ever looking inside.

That makes it very fast, able to handle millions of connections, and protocol-agnostic: it works for HTTP, databases, message brokers or anything else on TCP or UDP. But because it does not understand requests, it cannot route /orders to one service and /users to another, cannot balance individual requests inside one long connection, and cannot retry a failed request on another backend.

3What a Layer 7 Balancer Sees and Does

A layer 7 balancer, a reverse proxy such as Envoy, NGINX, HAProxy in HTTP mode or a cloud application load balancer, terminates the client's connection, meaning the client's TCP and TLS session ends at the proxy. It parses each HTTP request and opens or reuses its own connection to a backend.

Because it sees each request, it can route by host, path or header, balance per request, retry idempotent requests on another backend, add or rewrite headers such as X-Forwarded-For, compress responses, enforce rate limits and authentication, and log every request with its status and latency. For HTTPS it must decrypt the traffic, so it needs the TLS certificate and private key; that is usually what we want, as it centralises TLS termination.

Layer 4 versus layer 7
AspectLayer 4Layer 7
SeesIPs, ports, TCP or UDPfull HTTP request: host, path, headers
Balancesper connectionper request
Routing by path or hostnoyes
Retry a failed requestnoyes, for idempotent requests
TLSpasses it throughterminates it, needs the key
Protocolsany TCP or UDPthe ones it parses, mainly HTTP and gRPC
Cost per requestvery lowhigher: parsing, TLS, buffering

4Choosing for the Three Workloads

The public REST API fits layer 7. We want path-based routing to several services behind one domain, per-request balancing so long-lived keep-alive connections do not pin a client to one backend, request logs and metrics per route, and TLS terminated in one managed place. A cloud application load balancer or an Envoy or NGINX gateway does all of this.

The PostgreSQL connection pool endpoint fits layer 4. The balancer does not need to understand the PostgreSQL protocol, connections are long-lived and stateful, and each connection should stay on one backend for its whole life. A TCP balancer, or a pooler such as PgBouncer behind one, is the usual choice; if we needed routing by query, such as reads to replicas, we would use a database-aware proxy instead, which is a specialised layer 7 tool.

The UDP game server also fits layer 4, and the balancer must support UDP, which not all do. UDP has no connections, so the balancer tracks flows by source address and port and keeps a client on the same server, called affinity, because the game server holds that player's state.

5Combining Both Layers

Large platforms rarely choose only one. A common shape is a layer 4 tier at the edge that spreads connections across a fleet of layer 7 proxies, which then do TLS, routing and per-request balancing to the services. The layer 4 tier scales the proxy fleet horizontally and survives proxy failures, while the layer 7 tier provides the application features.

Step throughA typical combined setup
  1. Clientsbrowsers, mobile apps
  2. Layer 4 tierspreads TCP connections, very high throughput
  3. Layer 7 proxy fleetTLS, routing by host and path, retries, rate limits
  4. Servicesper-request balancing across pods

4 layers, from the top down. Step through them one at a time.

6Follow-up questions

Why can a layer 4 balancer not retry a failed HTTP request on another backend?

Because it never sees the request as a unit. It forwards a stream of bytes on a connection already bound to one backend, so it does not know where one request ends, whether a response was an error, or whether the request is safe to repeat. By the time the backend fails, the bytes have been sent and the client's connection is tied to that backend. Only a proxy that buffers and understands requests, a layer 7 balancer, can resend one elsewhere.

Does a layer 4 balancer always hide the client IP?

Not always. Some forward packets directly and keep the client's source address, while others proxy the connection or apply source NAT, in which case the backend sees the balancer's address. The PROXY protocol is the standard way to pass the original address in that case.

Can a layer 7 balancer handle WebSockets?

Yes, most can: the connection starts as an HTTP upgrade request, so it can be routed by path or host, and then the proxy forwards the long-lived stream. Balancing is then per WebSocket connection, not per message.

7Conclusion

In this answer, we saw that a layer 4 balancer works with addresses, ports and connections, which makes it fast and protocol-agnostic but unable to route or retry requests, while a layer 7 balancer terminates connections and understands HTTP, enabling routing, per-request balancing, retries and TLS termination. The REST API belongs behind layer 7, PostgreSQL and the UDP game server behind layer 4, and large platforms often put a layer 4 tier in front of a layer 7 fleet.

What to say in the interviewAbout 80 seconds aloud

Open with

A layer 4 balancer works with IP addresses, ports and TCP or UDP. It picks a backend when a connection opens and then forwards the bytes without understanding them, so it is very fast and works for any protocol, but it cannot route by URL, cannot balance individual requests inside a connection, and cannot retry a failed request because it never sees requests at all.

Then, if they want more

A layer 7 balancer terminates the client connection, parses HTTP, and then can route by host, path or header, balance each request, retry idempotent requests, add headers like X-Forwarded-For, compress and enforce authentication or rate limits; for HTTPS it needs the certificate and key, which is how we centralise TLS.

For the public REST API I would choose layer 7, for path routing, per-request balancing, request logs and TLS in one place. For the PostgreSQL endpoint I would choose layer 4, because the protocol does not need to be understood and connections are long-lived.

For the UDP game server, layer 4 with UDP support and affinity by client address, since the server holds player state. In large platforms I would often combine them: layer 4 in front of a fleet of layer 7 proxies.

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 "Explain what each kind can see and do, and which you would choose for a public REST API...": https://scrivio-interview-library.netlify.app/architecture/load-balancing-networking/db8fcc8e90/