Skip to content

Reverse proxy and API gateway

A reverse proxy is a server that clients believe is the application. It receives their requests, forwards each one to an internal server, and returns the answer as its own. That position at the front door makes it the natural place for TLS, caching, compression, security filtering and routing. An API gateway is a reverse proxy specialised for APIs: it adds identity, quotas and API-level routing for many backend services.

A forward proxy acts for clients: a corporate egress proxy that filters which sites employees reach, for example. A reverse proxy acts for servers: clients reach it at the public address, and it chooses which internal server handles the request.

flowchart LR
  client["Client"] -->|"HTTPS"| rp["Reverse proxy"]
  rp -->|"/static/*"| files["Static files or cache"]
  rp -->|"/app/*"| app["App servers"]
  rp -->|"/api/*"| api["API servers"]
  • Security and isolation. Backends get private addresses and are not directly reachable. The proxy can hide server banners, enforce request size and header limits, block IPs, cap connections per client and absorb slow-client attacks (slowloris) by buffering requests before passing them on. It is also where a web application firewall (WAF) usually runs.
  • Flexibility. Clients see only the proxy. You can add, replace, move or re-platform backends, or split one app into several, without changing a DNS record or a client.
  • TLS termination. Certificates live in one place and can be renewed automatically: Caddy, for example, obtains and renews certificates through ACME with no configuration. Backends receive plain HTTP on a trusted network, or re-encrypted traffic where policy requires it.
  • Compression. gzip, Brotli or zstd responses for clients that accept them, without burdening application code.
  • Caching. Store responses to repeated requests and answer them without touching the app (NGINX proxy_cache, Varnish). This is the same idea as a CDN, but in your own data centre.
  • Static content. Serve HTML, CSS, JS, images and video directly from disk or object storage, leaving app servers for dynamic work.
  • Protocol translation. Accept HTTP/2 and HTTP/3 from the internet while talking HTTP/1.1 or gRPC to backends. Upgrade connections to WebSocket.
  • Connection management. Many short client connections become a few long-lived, reused upstream connections, and slow mobile clients no longer tie up an app worker.

Pass the original client details downstream with X-Forwarded-For / X-Forwarded-Proto or the standard Forwarded header (RFC 7239). Backends should trust these headers only when they come from the proxy.

Reverse proxyLoad balancer
Main purposeA single, controlled front door with added featuresSpread load and survive backend failures
Useful with one backendYes: TLS, caching, compression, securityNot really
LayerL7 (HTTP-aware)L4 or L7
Typical extrasCaching, static files, rewrites, WAFHealth checks, balancing algorithms, draining

The distinction is about role, not product. NGINX, HAProxy, Envoy, Caddy and Traefik all proxy, balance and terminate TLS, and cloud “application load balancers” are managed L7 reverse proxies. In a design diagram, label the box by what it does for the system. See load balancers.

With many backend services, every one of them needs the same edge features: check the token, apply quotas, log the call, route /v2/orders to the right place. An API gateway does this once, at the edge of the service estate.

flowchart TB
  web["Web app"] --> gw["API gateway:<br/>auth, quotas, routing"]
  mobile["Mobile app"] --> gw
  partner["Partner API client"] --> gw
  gw --> users["User service"]
  gw --> orders["Order service"]
  gw --> catalog["Catalog service"]
  idp["Identity provider"] -.->|"keys for<br/>token checks"| gw

Typical responsibilities:

  • Authentication and coarse authorisation. Validate OAuth 2.0 / OIDC access tokens or API keys, then pass a verified identity to services, for example as a signed internal token or trusted headers. Fine-grained “may this user edit this order?” checks stay in the service that owns the order.
  • Rate limiting and quotas. Per API key, user, IP or plan, usually with token buckets backed by a shared store. See rate limiting.
  • Routing and versioning. Map public paths and versions to internal services, shift a percentage of traffic to a new version, and hide internal service names.
  • Request and response transformation. Header injection, protocol translation (REST to gRPC), response shaping for old clients.
  • Request aggregation. Fan out one client call to several services and merge the results, saving mobile clients from making many round trips over slow networks.
  • Observability and monetisation. One place for access logs, per-consumer metrics, tracing headers, developer portals and usage billing.

One gateway serving web, iOS, Android and partners becomes a compromise that suits nobody, and every client team waits on the gateway team. The backend-for-frontend pattern, described by Sam Newman, gives each user experience its own thin API layer owned by that client’s team. The BFF aggregates and shapes data for exactly what its screens need. A shared edge gateway can still handle TLS, authentication and global rate limits in front of the BFFs. GraphQL servers often play the BFF role. See API styles.

  • The “god gateway”. Business rules, orchestration and data joins creep into gateway plugins or scripts. Now every feature change needs a gateway deploy owned by a central team, the logic is hard to test, and the gateway knows everyone’s domain. Thoughtworks warned against these “overambitious API gateways” years ago, and the warning still holds. Keep the gateway to cross-cutting concerns. Domain logic belongs in services or BFFs.
  • Bottleneck and blast radius. All traffic crosses it. A bad config push, an expired certificate or a slow auth plugin takes down every API at once. Run it as a horizontally scaled, multi-zone fleet, roll out config gradually, and keep plugins on the hot path cheap.
  • Added latency. Each hop adds latency, and remote calls from the gateway (token introspection, a central rate-limit store) add more. Prefer local JWT signature checks with cached keys and local rate-limit counters with periodic sync.
  • Aggregation pitfalls. Fan-out multiplies tail latency: the slowest of five calls sets the response time. Use timeouts, partial responses and the patterns in resilience patterns.
  • Another tier to deploy, configure, monitor and secure, and its configuration is code that needs review and tests.
  • A single proxy or gateway instance is a single point of failure. Running several behind a load balancer or anycast fixes that but adds complexity.
  • Debugging needs correlation IDs and tracing across the extra hop, or 502 and 504 errors become guesswork.
  • Reverse proxies / L7 load balancers: NGINX, HAProxy, Envoy, Caddy, Traefik, Varnish (caching). Managed: AWS ALB, Google Cloud Application Load Balancer, Azure Application Gateway and Front Door.
  • Self-hosted API gateways: Kong Gateway, Apache APISIX, Tyk, KrakenD, Envoy Gateway. Most Kubernetes-native options now implement the Kubernetes Gateway API (Gateway, HTTPRoute, GRPCRoute), the successor to the Ingress API.
  • Managed API gateways: Amazon API Gateway, Azure API Management, Google Cloud Apigee and API Gateway.
  • AI gateways: a newer variant for traffic to LLM providers (Kong AI Gateway, Cloudflare AI Gateway, Envoy AI Gateway). Same edge role, but quotas count tokens and cost rather than requests, and typical features are provider failover, response caching and usage logging.
OptionGood forCosts
Plain reverse proxyTLS, caching, static files, simple routingNo API-level identity or quotas
Central API gatewayConsistent auth, quotas, routing and analytics for many servicesBottleneck and blast radius; risk of god-gateway logic
BFF per clientClient-tailored APIs, team autonomy, fewer round tripsMore services to run; some duplicated logic
Managed gatewayNo ops, built-in portal and analyticsPer-request pricing, vendor limits, extra latency

Based on The System Design Primer by Donne Martin (CC BY 4.0). Restructured, rewritten and updated; site content is licensed under CC BY 4.0.