Reverse Proxy (Web Server)¶
What it is¶
A reverse proxy is a web server that centralizes internal services and provides a unified interface to the public. Client requests are forwarded to a server that can fulfill them; the reverse proxy returns that server's response to the client.
Benefits¶
-
Increased security — hide backend server info, blacklist IPs, limit connections per client.
-
Increased scalability & flexibility — clients only see the proxy's IP, so you can scale or reconfigure servers freely.
-
SSL termination — decrypt/encrypt at the edge so backends don't need to (removes need for X.509 certs on each server).
-
Compression — compress responses.
- Caching — return responses for cached requests.
- Static content — serve HTML/CSS/JS, photos, videos directly.
Load balancer vs reverse proxy¶
| Load balancer | Reverse proxy | |
|---|---|---|
| Best when | You have multiple servers serving the same function | Even a single web/app server (still gains security, caching, SSL, etc.) |
| Role | Distribute load | Centralize/unify access + add edge features |
- Tools like NGINX and HAProxy can do both layer-7 reverse proxying and load balancing.
Disadvantages¶
- Introduces complexity.
- A single reverse proxy is a single point of failure; configuring multiple (with failover) adds more complexity.
Key takeaways¶
-
A reverse proxy sits in front of servers and acts on their behalf; a load balancer distributes among servers.
-
In practice the two roles frequently overlap in the same software.
Common Interview Questions¶
Design an API gateway
Why it's asked here: an API gateway is a reverse proxy with extra routing/edge concerns.
Key points to discuss:
- Centralize auth, rate limiting, SSL termination, and logging at the edge.
- Route by path/header to internal services (layer-7 reverse proxying).
- Caching and compression as reverse-proxy features.
- Trade-off: a single entry point = single point of failure (add failover).
flowchart LR
C[Client] --> G[API Gateway]
G -->|authenticate| A[Auth service]
G -->|route| S1[Service A]
G -->|route| S2[Service B]
G -->|rate limit| R[(Redis)]
Design a web crawler's fetching layer
Why it's asked here: crawler fetchers commonly use proxies and respect robots/rate limits.
Key points to discuss:
- Reverse proxy vs forward proxy roles in fetching.
- Throttling per domain, retries, and timeouts at the fetch layer.
- How a proxy centralizes connection management and hides origin identity.
- Connection pooling to reduce overhead.
flowchart LR
F[Fetch worker] --> P[Proxy]
P -->|throttle per domain| T[Target site]
F --> Q[(URL frontier queue)]
Further reading¶
Design an API Gateway¶
An API gateway is the single entry point for external clients and fronts all internal services. For a large consumer or enterprise platform, assume roughly 10 million daily users, 50,000 requests per second at peak, and about 100 downstream microservices. The functional requirements are to route requests to the correct service, authenticate callers, enforce rate limits, transform protocols or payloads when needed, collect logs and metrics, and return a unified error or response. The non-functional requirements are low added latency, high availability, horizontal scalability, security, and operational observability.
The high-level architecture places a highly available gateway tier behind public DNS and load balancers. Clients connect over HTTPS to the gateway. The gateway terminates TLS, authenticates the request, applies rate limiting and security policies, then routes the request through a router component. The router consults the service registry and route table to choose the destination service instance. The gateway forwards the request through a pluggable filter chain that can add headers, retry, or transform payloads. Responses flow back through the same chain so logging and metrics are captured uniformly.
The primary data model is route and policy configuration, plus transient rate-limit and session state. Route rules map public paths such as /users or /orders to internal service names and methods, and may include version, timeout, retry, and circuit-breaker settings. API keys, OAuth scopes, plans, and rate-limit policies are stored in a relational database or replicated configuration store because they change rarely and must be queryable by admin tooling. Hot rate-limit counters, short-lived tokens, and session metadata are kept in Redis because they are high-volume, latency-sensitive, and can be evicted safely. The gateway itself prefers in-memory configuration with asynchronous updates from the control plane, so the request path does not query the configuration database on every call.
The core mechanism is the request filter chain. Each incoming request is normalized into a canonical context containing the client identity, route, headers, and tracing ID. Filters execute in order: authentication validates the credential and attaches a principal, authorization checks scopes, rate limiting consumes a token or counter, routing selects an upstream, and transport forwarding sends the request. When a downstream service fails or becomes slow, the gateway applies timeout, retry, and circuit breaking to prevent cascading failure. This chain makes the gateway a reverse proxy with cross-cutting policy enforcement rather than a simple router.
For scaling and availability, the gateway tier is stateless and can run as many instances behind an L4 load balancer or anycast IP. Shared request state such as rate-limit counters and token revocation lists is stored in replicated Redis or etcd, so any instance can handle any request. Service discovery is integrated with Kubernetes, Consul, or etcd; gateway instances subscribe to changes and update their in-memory routing tables. Failure handling includes active health checks to remove unhealthy upstreams, retries with jitter and budget, circuit breakers, and graduated fallbacks. The gateway itself is deployed across availability zones with redundant load balancers so no single instance or zone is a point of failure. Observability is built into the request path with structured logs, distributed tracing, and metrics exported from each filter.
The main trade-offs are centralization versus operational simplicity, and added latency versus security and reliability. A single gateway makes authentication, rate limiting, and observability consistent, but it adds a network hop and must be highly available or it becomes a bottleneck. Offloading shared policy state to Redis keeps gateway replicas stateless but adds a dependency and potentially one extra round trip. Aggressive routing features such as retries and payload transformation improve resilience and compatibility but increase configuration complexity and CPU cost. Finally, embedding many policies in one service can become a monolith; some teams choose multiple specialized gateways or split the control plane from the fast data plane, accepting more moving parts to avoid coupling every policy change to the request path.
flowchart LR
C[Client] --> DNS[DNS / Anycast]
DNS --> LB[L4 Load Balancers]
LB --> G1[API Gateway Instances]
LB --> G2[API Gateway Instances]
G1 --> F[Filter Chain: Auth / Rate Limit / Route / Transform]
G2 --> F
F --> A[Service Registry / Config]
F --> R[(Redis: rate counters / sessions)]
F --> U1[Internal Service A]
F --> U2[Internal Service B]
F --> U3[Internal Service C]
U1 --> F