SysDesignAtlas

Proxies: Forward and Reverse

The middleman in front of clients and the middleman in front of servers — what each is for, the jobs a reverse proxy takes off your application servers, and how proxies relate to load balancers, CDNs, API gateways and sidecars.

HLDEasy8 min readproxynetworkingreverse-proxyload-balancing
Confidence

A proxy is a computer that sits between two others and passes messages along, acting on one side's behalf. That's the whole definition. The two kinds differ only in whose side the proxy is on — and that one difference decides everything it's used for.

#The two kinds, side by side

   FORWARD PROXY — works for the clients

     laptop ─┐
     laptop ─┼──▶ [ forward proxy ] ──▶ the internet (any website)
     phone  ─┘     the clients know it's there and send everything through it;
                   websites see the proxy, not the individual clients

   REVERSE PROXY — works for the servers

                                         ┌──▶ app server 1
     anyone on the internet ──▶ [ reverse proxy ] ──▶ app server 2
                                         └──▶ app server 3
                   clients think it IS the website; the servers behind it are hidden

An easy way to remember: a forward proxy hides the clients from the servers; a reverse proxy hides the servers from the clients.

#Forward proxies — on the client's side

The clients are configured to send their requests through it (or the network forces them to). Common uses:

  • Company networks. All staff traffic leaves through one proxy that blocks some sites, scans downloads for malware and logs access.
  • Caching for many clients. A school's proxy fetches a popular file once and serves it to every student.
  • Hiding who is asking. Websites see the proxy's address, not the person's — the basis of privacy tools.
  • Getting out of a locked-down network. Servers in a private network with no direct internet access reach outside services through one controlled proxy, so there's one place to allow, log and audit outbound calls.

In system design interviews, forward proxies come up rarely — mostly that last case, as an egress proxy: "our services call third-party APIs only through an egress proxy, which holds the allow-list and the credentials."

#Reverse proxies — on the server's side

This is the one you'll draw in almost every design. Clients connect to the reverse proxy as if it were the site; it forwards each request to one of the servers behind it and sends back the answer.

Because it handles every request first, it can take whole jobs away from the application servers:

JobWhat it means
Load balancingSpread requests across healthy servers — see Load Balancing
TLS terminationDo the HTTPS encryption work once, at the edge; talk plain HTTP (or re-encrypted) inside
CachingServe repeated responses without bothering the servers — see Caching
CompressionGzip or Brotli responses, saving bandwidth
Routing/api/* to one group of servers, /images/* to another
Rate limiting and blockingStop abusive clients before they reach the app — see Rate Limiting
Buffering slow clientsHold a slow phone's upload or download so app servers aren't tied up waiting
Hiding the insideClients never learn how many servers exist or their addresses

The buffering one is easy to overlook and genuinely important. A phone on a weak connection might take 20 seconds to send a request. If the app server waits for it, that server is busy doing nothing for 20 seconds. A reverse proxy receives the whole request first, then hands it to the app server in a few milliseconds — and does the same for the response. App servers spend their time computing, not waiting on networks.

#Reverse proxy, load balancer, CDN, API gateway — what's the difference?

People use these words loosely, because they overlap. The honest answer: they're all reverse proxies, named for their main job.

   reverse proxy        the general idea: in front of servers, forwarding requests
     ├─ load balancer    …whose main job is spreading load across servers
     ├─ CDN edge server  …placed near users worldwide, mostly caching
     ├─ API gateway      …that also handles auth, quotas and routing for services
     └─ sidecar proxy    …running next to each service, handling its network calls

So in an interview, you can draw one box labelled "load balancer / reverse proxy" at the front and say what it does — the label matters less than naming its jobs. The CDN is the same idea spread across the globe, and the API Gateway is the same idea with business rules added.

#Layer 4 or layer 7

A proxy can work at two depths:

  • Layer 4 (transport): it forwards TCP connections without looking inside — it only sees addresses and ports, and passes the bytes through. Very fast, works for any protocol, but can't route by URL or cache — it doesn't know what an HTTP request is.
  • Layer 7 (application): it reads the HTTP request — path, headers, cookies — so it can route, cache, rewrite and rate-limit per user. More work per request, much more capability.

Most web systems use layer 7 at the edge. Layer 4 shows up where raw speed matters or the protocol isn't HTTP. Load Balancing covers the trade-off in more detail.

#What the app server stops seeing

Putting a proxy in front changes what the application sees, and forgetting this causes real bugs:

  • The client's address. The app sees every request coming from the proxy. The proxy adds a header — X-Forwarded-For: 203.0.113.7 — and the app must read that instead. But only trust the value your own proxy added. A client can put a fake X-Forwarded-For in its own request, and if the app believes it, an attacker can dodge per-IP rate limits or bans just by claiming a different address.
  • Whether it was HTTPS. If TLS ends at the proxy, the app sees plain HTTP. The proxy passes X-Forwarded-Proto: https so the app builds correct links.
  • Timeouts. The proxy has its own timeout. If the app allows 60 seconds but the proxy gives up at 30, users see errors for requests the app is still happily processing.

#Sidecars and service meshes

In a system of many services, there's a newer place to put proxies: next to every service instance, in the same machine or container group. Each service talks to its local proxy, and the proxies handle retries, timeouts, encryption between services and metrics. A fleet of these is called a service mesh.

It's the reverse-proxy idea applied to service-to-service traffic. It's powerful and has real operational cost — see the service mesh section in API Gateway for when it's worth it.

#In the interview

  • Draw the reverse proxy / load balancer at the front of almost every design, and say which jobs it's doing: "it terminates TLS, balances across the app servers, and caches the public product pages."
  • Mention buffering when there are mobile clients or large uploads.
  • Use "egress proxy" when your design calls outside services and security matters.
  • Don't spend time on the forward/reverse definitions unless asked — knowing them is assumed; using them well is what scores.

#Check yourself

Forward proxy versus reverse proxy: which side is each on, and what does each hide?Show answer
A forward proxy works for clients and hides them from servers (company egress, caching, privacy). A reverse proxy works for servers and hides them from clients — the clients think it is the site.
Name five jobs a reverse proxy takes off your application servers.Show answer
Load balancing, TLS termination, caching, compression, path-based routing, rate limiting and blocking, buffering slow clients, and hiding internal topology.
Why does buffering slow clients matter?Show answer
The proxy receives a slow phone's whole request (and response) before involving the app server, so app servers spend milliseconds computing instead of seconds waiting on bad networks.
How do load balancers, CDNs, API gateways and sidecars relate to reverse proxies?Show answer
They're all reverse proxies named for their main job: spreading load, caching near users worldwide, auth and quotas for many services, or handling one service's network calls.
Your app logs every request as coming from 10.0.0.5. What happened, and what's the catch in the fix?Show answer
It's seeing the proxy's address. Read X-Forwarded-For — but only trust it from your own proxies, because any client can send that header.
What is an egress proxy for?Show answer
Letting services in a locked-down network call outside APIs through one controlled point that holds the allow-list and credentials and logs every outbound call.
Layer 4 versus layer 7 proxying in one line each?Show answer
L4 forwards TCP connections without looking inside — fast, any protocol. L7 reads the HTTP request — it can route, cache, rewrite and rate-limit per user.

#What to carry forward

  • A forward proxy hides clients; a reverse proxy hides servers — and the reverse proxy is in almost every design.
  • Reverse proxies terminate TLS, balance load, cache, route, rate-limit and buffer slow clients.
  • Load balancers, CDN edges, API gateways and sidecars are reverse proxies named for their main job.
  • Behind a proxy, read client IP and scheme from forwarded headers you trust, and align timeouts.
  • Run at least two proxies so the proxy layer is never a single point of failure.
00:00
/ 8m