Course contents

02. Networking and Internet Lesson 10 of 10

Reverse Proxies

What a reverse proxy does in front of your servers: one public address, TLS and routing offload, rate limits, hidden failures, and why it must run redundantly.

13 minBeginner9 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • A reverse proxy owns the public address and forwards requests to servers that clients never see.
  • It centralises TLS, compression, routing, caching, buffering, headers and logging.
  • Rate limits at the proxy reject excess cheaply and keep servers fast for admitted requests.
  • With health checks, servers can fail, be replaced or be added without clients noticing.
  • Every request passes through it, so it must be redundant and closely monitored.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

A reverse proxy is a server that sits in front of one or more application servers and receives client requests on their behalf. Clients connect to the proxy's public address; the proxy opens its own connection to a backend, forwards the request and relays the response. The backends are usually on a private network and unreachable from the internet.

A forward proxy is the mirror image: it acts for clients (for example a corporate egress proxy or a privacy proxy) and hides them from the servers they visit. A load balancer is a reverse proxy whose main job is spreading traffic over several backends; most modern reverse proxies (nginx, HAProxy, Envoy) do both.

Why it exists

Exposing application servers directly couples clients to specific machines and forces every server to deal with the internet: TLS, slow clients, abusive traffic, compression and static files. A single front door decouples clients from servers and lets you implement those cross-cutting concerns once, in software built for them, instead of in every application.

How it works

Connection handling. The proxy accepts client connections, often terminates TLS, and reads the request. It then picks a backend (by host, path, header or a balancing algorithm) and sends the request over a separate connection, typically a pooled, keep-alive connection on the private network.

Common responsibilities:

  • TLS termination: certificates and handshakes live in one place; backends speak plain HTTP or internal mTLS.
  • Routing: /api/* to the API service, /static/* to object storage, admin.example.com to another service.
  • Compression and caching: gzip or Brotli responses; serve cached responses without touching the backend.
  • Buffering: absorb slow uploads and downloads so application workers are not held hostage by slow networks.
  • Protection: request size limits, rate limits, IP allow and deny lists, basic web application firewall rules.
  • Resilience: health checks, retries of idempotent requests on another backend, timeouts.
  • Observability: access logs, request IDs and metrics for every request in one format.

Rate limiting. A token bucket per key (client IP, API key, user) refills at a fixed rate up to a burst size. Each request takes a token; without one the proxy answers immediately with HTTP 429, often with a Retry-After header. The excess costs almost nothing, and the backends stay within capacity.

When to use it

  • Any internet-facing web application or API: almost every production deployment has one.
  • When several services share one domain and need routing by path or host.
  • When you want TLS, compression, logging and rate limits handled consistently.
  • In front of servers you want to scale, replace or deploy without clients noticing.

When not to

  • Internal service-to-service calls where a service mesh sidecar or client-side balancing already provides the same features; another central proxy adds a hop.
  • Extremely latency-sensitive paths where even a local hop matters and the features are not needed.
  • Protocols the proxy cannot understand, where only a layer-4 pass-through makes sense.

Trade-offs

Choice You gain You pay
Reverse proxy in front Hidden, replaceable servers; central TLS, routing, limits An extra hop; a critical component to run
TLS terminated at the proxy Simpler backends, one place for certificates Traffic inside the network is plaintext unless re-encrypted
Caching at the proxy Fewer backend requests, lower latency Invalidation and staleness to manage
Rate limiting at the proxy Backends protected, abuse contained Legitimate bursts may be rejected; limits need tuning per client
Proxy retries Hides brief backend failures Duplicate work; dangerous for non-idempotent requests

Alternatives

  • Managed load balancers (cloud layer-7 balancers): the same role, operated for you.
  • API gateways: reverse proxies specialised for APIs, adding authentication, quotas, request transformation and developer keys.
  • CDNs: globally distributed reverse proxies that cache at the edge, close to users.
  • Service meshes: a small proxy next to every service instance, for internal traffic.
  • Client-side load balancing: clients discover backends themselves and connect directly.

Failure modes

  • Single point of failure: one proxy instance down means every request fails, however healthy the backends are.
  • Proxy saturation: CPU for TLS, connection limits or file descriptors run out before the backends do.
  • Misrouting: a configuration error sends traffic to the wrong backend or drops a path entirely; configuration changes need review and staged rollout.
  • Timeout mismatch: a proxy timeout shorter than the backend's work makes clients retry requests that are still running; a longer one ties up connections.
  • Retry amplification: proxy retries on top of client retries multiply load during an incident.
  • Lost client information: without X-Forwarded-For or the Forwarded header, backends see the proxy's IP instead of the client's.

In production

Run at least two proxy instances across availability zones, behind a floating IP, DNS with health checks, or a managed network load balancer. Keep configuration in version control and roll it out gradually. Set explicit timeouts, body size limits and per-client rate limits; forward the original client IP and protocol in standard headers; and propagate a request ID for tracing. Monitor the proxy's own latency, error rate, connection counts and certificate expiry alongside backend metrics. Common choices are nginx, HAProxy, Envoy, Caddy and Traefik, or the cloud provider's layer-7 balancer.

Common misconceptions

  • "A reverse proxy is only for load balancing." Routing, TLS, caching, buffering and protection are often the main reasons to have one, even with a single backend.
  • "It makes the system slower." The extra hop costs a millisecond or two inside a data centre, and offloading TLS, compression and slow clients often makes the whole system faster.
  • "It removes single points of failure." It hides backend failures but is itself one unless run redundantly.
  • "Forward and reverse proxies are the same thing." They sit on opposite sides and serve opposite parties.

Interview angle

In a system design interview the reverse proxy or load balancer is usually the first box after the client. Name what it does beyond balancing: TLS termination, routing by path, rate limiting and caching. Expect follow-ups on how you make it highly available, where TLS ends, how backends learn the client's IP, and how rate limits are keyed. Mentioning that CDNs and API gateways are specialised reverse proxies shows you see the pattern.

Model assumptions

The simulation is a simplified model, not a benchmark. The reverse proxy is modelled as a load balancer with round robin, 1 ms of processing, health checks every 500 ms (2 failures eject, 3 successes restore) and an optional token-bucket rate limit (60 requests per second with a burst of 20). App servers have 6 workers, an 80 ms mean service time (lognormal, cv 0.35) and room for 12 waiting requests. TLS, compression, path routing and caching are described but not simulated; the static file store is drawn for illustration only. Arrivals are random (Poisson); network hops take a fixed 1–30 ms. Numbers show behaviour and orders of magnitude only.

The lesson as textEvery step of the simulation, written out.
  1. 1. Browsers talk straight to the app

    Browsers send about 40 requests per second directly to the application server. Its address is public, so it has to do everything itself: accept connections from the whole internet, handle TLS, compress responses and serve images next to API calls.

    It works. But every client knows exactly which machine to talk to, so that machine can never be replaced, moved or multiplied without clients noticing.

    Scene: Browsers connect directly to App 1, which has a public address. It is about half busy and every request succeeds.

    Key idea: Without an intermediary, clients are coupled to a specific server.

  2. 2. Put a reverse proxy in front

    A reverse proxy now owns the public address. Browsers connect to it; it opens its own connection to App 1, forwards the request and relays the response. App 1 is no longer reachable from the internet.

    It is called *reverse* because a forward proxy acts for clients (a company proxy that employees browse through) while this one acts for servers. The extra hop inside the data centre costs only a few milliseconds.

    Scene: Browsers now connect to a reverse proxy, which forwards every request to App 1 over a private connection. Latency rises by only a few milliseconds and nothing fails.

    Key idea: A reverse proxy stands in for your servers: clients see only the proxy.

  3. 3. Jobs the proxy takes over

    Because every request passes through it, the proxy is the natural place for work that every server would otherwise repeat: TLS termination, compression, request size limits, security headers and access logs. It can also route by path: here /static/* goes to a file store, so App 1 only sees API calls.

    It can cache responses, buffer slow clients so app workers are not held by a phone on a bad network, and retry an idempotent request on another server.

    Scene: The reverse proxy terminates TLS, compresses responses and routes /static requests to a static file store and /api requests to App 1. The file store is drawn for illustration and is not simulated.

    Key idea: Centralise cross-cutting HTTP work in the proxy so application servers only run application code.

  4. 4. A traffic spike reaches the app

    One second in, a client starts hammering the API and traffic jumps to 160 requests per second, twice what App 1 can serve.

    The proxy faithfully forwards all of it. App 1 saturates, its queue fills and it rejects requests, and even the requests that succeed wait in line. Every user suffers, not only the noisy client.

    Scene: Traffic rises to 160 requests per second. App 1 is overloaded: its queue is full, latency is high and some requests are rejected.

    Key idea: A proxy that forwards everything passes overload straight through to the servers.

  5. 5. Limit the rate at the door

    We configure a rate limit on the proxy: a token bucket that admits about 60 requests per second with short bursts of up to 20. Anything above that is answered immediately by the proxy with HTTP 429 (Too Many Requests).

    App 1 now receives only what it can handle and runs about 80% busy, no longer saturated. The excess fails fast and cheaply at the edge instead of slowly at the server, and the requests that are admitted are quick again. In production you would limit per client or API key, so only the noisy client is slowed down.

    Scene: The proxy admits about 60 requests per second and answers the rest with 429. App 1 is about 80% busy instead of saturated, and admitted requests are fast again.

    Key idea: Reject excess load cheaply at the proxy so the servers behind it stay fast for admitted requests.

  6. 6. Change the servers, not the address

    The spike is over and traffic is back to 60 requests per second. We add App 2 behind the proxy and raise the rate limit. Browsers notice nothing: they still talk to the same address.

    The proxy spreads requests over both servers. This is the same mechanism a load balancer uses; many reverse proxies (nginx, HAProxy, Envoy) are load balancers too. Deploys, migrations and scaling now happen behind the front door.

    Scene: The proxy forwards 60 requests per second to App 1 and App 2 in turn. Both are lightly loaded and nothing fails.

    Key idea: Behind a stable proxy address, servers can be added, removed or replaced freely.

  7. 7. The proxy hides a failed server

    One second in, App 1 crashes. Its in-progress requests fail, and a few more are sent there before the proxy’s health checks notice, about a second later.

    After that the proxy routes everything to App 2 and errors stop. Browsers never learned that App 1 existed, so nothing on the client side has to change.

    Scene: App 1 crashes. A few requests fail until the proxy’s health checks remove it, then App 2 serves all traffic without errors.

    Key idea: Health checks in the proxy turn a server failure into a short blip instead of an outage.

  8. 8. The front door is now critical

    App 1 is back. One second in, the proxy itself goes down. Both app servers are healthy, but no request can reach them: every request fails.

    The component that hides failures has become a single point of failure. Production setups run at least two proxy instances behind a floating IP, DNS with health checks or a managed balancer, so that losing one is the same short blip you just saw for App 1.

    Scene: Both app servers are healthy, but the reverse proxy goes down and every request fails because nothing else can reach the servers.

    Key idea: Run the reverse proxy redundantly; it is on the path of every request.

  9. 9. What a reverse proxy gives you

    One stable public address; servers hidden and replaceable; TLS, compression, routing, caching and rate limits in one place; failures and deploys absorbed behind the door.

    The costs: an extra hop (small) and a critical component (large), so run it redundantly and monitor it as closely as the application. CDNs and API gateways are reverse proxies with extra features.

    Scene: The reverse proxy is back and forwards traffic to App 1 and App 2 without errors.

    Key idea: A reverse proxy is the front door: powerful, central, and worth running twice.