← Building blocks

Put a reverse proxy in front

Networking & deliveryfundamental

App servers handle TLS, compression, static files, buffering and slow clients themselves.

Say it like this

I put a reverse proxy in front of the application servers so TLS, compression, static files, buffering of slow clients and routing are handled once in a tool built for it, and the app only sees clean, complete requests.

Basic solution

A reverse proxy handles those and forwards clean requests to the app.

nginxEnvoyCaddyTraefik

One front, several pools

The proxy terminates TLS, buffers slow clients, compresses responses and routes by path to separate pools, each with its own timeouts and health checks. App workers only ever see complete requests.

How it works

  1. All public traffic arrives at the proxy's address. The proxy terminates TLS and reads the complete request headers, so it knows the host, path, method and size before it decides what to do with the request.
  2. It routes by host and path. Requests under /static are served from a local directory or forwarded to the CDN origin, /api goes to the pool of API servers, and /ws is upgraded to a WebSocket and handed to the connection-server pool. These rules live in the proxy's configuration, so moving a pool never changes application code.
  3. It buffers. A client on a slow mobile link may take 20 seconds to send a 2 MB request body; the proxy absorbs those bytes as they trickle in and hands the complete request to an application worker in one go, so the worker is never tied up waiting for a slow network. Responses work the same way in reverse: the app writes the whole response into the proxy's buffer in milliseconds and the proxy drains it to the client at whatever speed the client can take.
  4. It transforms. Responses are compressed with gzip or Brotli, security headers are added, X-Forwarded-For and X-Forwarded-Proto record the original client address and scheme, and requests that are too large or malformed are rejected with 413 or 400 before they reach the application.
  5. It protects. Per-IP connection and rate limits, timeouts on idle connections, and health checks that remove a failing backend from the pool all happen here, in a component built for it.

Reverse vs forward

  • A forward proxy sits in front of clients and talks to the internet on their behalf. A reverse proxy sits in front of servers and talks to clients on their behalf. Same software, opposite direction.

Worked example

Chat app connection tier

  • 10M concurrent WebSocket connections across 40 connection servers, plus 20K HTTP requests/s for history and media.
  • Slow-client buffering: a message history response is 200 KB. If the app server wrote it directly to a client on a 500 kbps link, the worker would be busy for about 3 s. Through nginx the app writes 200 KB into the proxy buffer in 2 ms and moves on; the proxy drains it to the client over the 3 s.
  • Worker maths: 20K requests/s with 3 s of waiting would need 60K concurrent workers; with the proxy absorbing the wait and app work at 5 ms, 100 workers per server suffice.
  • Compression: JSON history compresses about 5×. 20K/s × 200 KB = 4 GB/s uncompressed; after Brotli about 0.8 GB/s on the wire. The proxy does the compression with its own CPU budget.
  • Routing: /ws to the connection servers with a 1 h idle timeout and HTTP/1.1 upgrade allowed; /api to stateless API servers with a 10 s timeout; /media to the CDN origin bucket. One config, zero application changes when the pools move.

Variants and when each wins

nginx or HAProxy

  • Mature, fast proxies driven by a configuration file. They win as the default in front of a fixed fleet where the rules change rarely.

Envoy (often as a service mesh sidecar)

  • Configured dynamically through an API, with deep per-route observability, retries and circuit breaking, and native gRPC and HTTP/2 support. It wins on Kubernetes and in service meshes where pools change constantly and you want per-route metrics.

Cloud load balancer as the proxy

  • A managed balancer such as an ALB does TLS, routing and health checks with no server for you to run. It wins when you want zero operational burden; it offers fewer knobs for buffering and header manipulation, so pair it with a lightweight proxy per host if you need those.

Caddy or Traefik

  • Proxies with automatic certificates and built-in service discovery that need almost no configuration. They win for small fleets and container platforms where convenience matters more than tuning.

Recommendation: a cloud balancer for TLS and coarse routing, nginx or Envoy per node for buffering, compression and fine-grained rules; Envoy when you need per-route retries and metrics.

Failure modes

Trusting forwarded headers from the client

  • Rate limits are being bypassed and logs show fake client IPs. The proxy appends the client-supplied X-Forwarded-For value instead of overwriting it, or the application trusts that header from any source, so a client can claim any address. Have the proxy set the header from the address it actually accepted the connection from, and have the application trust it only when the request arrived via the proxy.

Buffer disk thrash on large uploads

  • Proxy nodes run out of disk during a burst of uploads. The proxy buffers large request bodies to disk by default, and many concurrent uploads fill it. Route uploads straight to object storage with pre-signed URLs so the proxy never sees the bytes, and raise in-memory buffer limits only for the specific routes that justify it.

WebSocket connections dropped every 60 seconds

  • Clients reconnect on a regular timer and sessions never last. The proxy's idle timeout is shorter than the interval between the client's heartbeats, so the proxy closes connections it considers idle. Set a long idle timeout on the /ws route and send heartbeats well inside it.

Proxy becomes the bottleneck

  • Application servers sit idle while the proxy runs at 100 percent CPU. TLS handshakes and compression for the whole site are running on one undersized proxy node. Scale the proxy tier separately from the app tier, offload TLS termination to the load balancer, and compress only text content types rather than already-compressed media.

Health check too shallow or too deep

  • Either dead backends keep receiving traffic, or one dependency blip ejects the whole pool at once. A shallow check (a static page) stays green when the app is broken; a deep check that calls every downstream fails for all backends the moment one shared dependency hiccups. Use a check that confirms the process can serve a request, without calling every downstream dependency.

Interviewer follow-ups

Answer out loud before you open each one.

Why does buffering at the proxy matter so much for app server capacity?

An app worker is a scarce, expensive resource; a proxy connection is cheap. Without buffering, each slow client holds a worker for the full transfer time. With it, the worker finishes in milliseconds and the proxy, which handles tens of thousands of idle connections per core, does the waiting.

How do you avoid X-Forwarded-For spoofing?

The proxy overwrites the header with the address it accepted the connection from, or appends and the app takes the last trusted hop from a configured list of proxy addresses. Behind a CDN you trust the CDN-provided client IP header only from the CDN ranges. Never read the first value blindly.

What belongs in the proxy and what belongs in the app?

Anything protocol-level and uniform belongs in the proxy: TLS, compression, static files, size limits, timeouts, coarse routing, basic rate limits. Anything that needs business context belongs in the app: authorisation beyond a token check, per-user limits, request validation against the domain.

How is a reverse proxy different from an API gateway?

Technically they overlap; the gateway is a reverse proxy with product features: authentication, per-client quotas, request transformation, API versions, developer keys. A plain reverse proxy is infrastructure in front of one application; a gateway is the product boundary in front of many services.

Connections

Pairs with
Terminate TLS at the edge · Spread traffic across servers · One front door for many services · Send fewer bytes · Pull broken instances out of rotation

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Application Load Balancernginx / EnvoyCloud Load BalancingApplication Gateway, Azure Load BalancerNGINX, HAProxy, Envoy
Amazon EC2 Auto ScalingAPI poolCompute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad
Amazon EC2Connection serversCompute EngineAzure Virtual MachinesDigitalOcean Droplets, any VM or bare metal
Amazon S3Static / media originCloud StorageBlob StorageCloudflare R2, MinIO, Ceph