Reverse proxies and API gateways
A public address must direct each request to the service allowed to handle it. Forwarding without a clear trust boundary lets client input become internal authority. We will build a reverse proxy, place gateway policy around it, and separate routing from business authorization.
#Section 1Reverse proxies
A viewer opens Heron’s score page while an application server is being replaced. The browser should not need that server’s private address or know when its replacement starts. It sends a request to a stable public endpoint, and an intermediary chooses a suitable internal destination.
A reverse proxy receives requests on behalf of servers and forwards them upstream. The browser connects to the proxy; the proxy makes or reuses another connection to the application. Those are separate connections with separate timeout, encryption and failure boundaries. A successful connection to the proxy does not establish that an application can answer.
For Heron’s illustrative peak of 1,000 requests per second and a 0.2-second mean response lifetime, requests are in flight. Read this as arrival rate multiplied by time in the request path. The proxy must carry that work and its associated buffers, even though its own forwarding computation may be small. The five application servers receive 200 requests per second each under equal assignment.
Forwarding includes the method, path, body and selected headers. The proxy also returns the upstream response, or an error when the upstream cannot finish. Streaming a body avoids buffering all uploaded video in memory, but then a failed partially forwarded upload is harder to retry. Buffering and streaming are resource and recovery choices, not interchangeable flags.
The proxy can centralize TLS certificates and hide private topology. It also becomes infrastructure that needs redundancy and bounded work. Adding another hop never removes the need for application permission checks. NGINX's load-balancing guide gives a concrete reverse-proxy configuration; the subsequent parts explain how its destination choice works.
#Section 2API gateways and routing policy
Heron receives a score read and a clip upload through the same public hostname. The read belongs to the score service; the upload belongs to the media service. An API gateway combines request routing with public API policy such as credential checks, request limits and protocol handling.
A route can match a hostname, path prefix and method before choosing an upstream service. GET /scores/match-7 and POST /clips therefore need not reach the same pool. Within the selected pool, a load balancer chooses an endpoint. Service selection and endpoint selection answer different questions, even when one process performs both.
Suppose a rollout sends 5 percent of Heron's 1,000 requests per second to a candidate version. The expected rates are to the candidate and to the established version. Read these as fractions of routing decisions over enough requests, not a guarantee that every short interval contains exactly that split. Persistent connections or user affinity can change the sampling unit.
A gateway can reject a missing credential cheaply, but ownership of a particular clip still belongs to the application with access to the authoritative record. Central checks reduce repeated work; they do not make internal requests trustworthy by themselves. A compromised internal caller must not bypass authorization by avoiding the public gateway.
The failure case is an overgrown gateway that calls many databases and services before forwarding. It now has its own distributed workflow and failure modes. Keep the gateway's policy bounded, document which service owns each business decision, and measure rejected traffic separately from upstream errors. Otherwise the same status code can conceal a failed route, failed credential check or failed application.
#Section 3TLS termination and trusted forwarding headers
The application sees the proxy's socket address rather than the viewer's address. If it needs the original client's address or scheme, the proxy can send forwarding metadata. Accepting that metadata directly from the public request lets a caller claim an arbitrary origin.
TLS termination means the proxy decrypts a client TLS connection. A new encrypted connection can protect the proxy-to-application hop. The second connection has its own peer identity and certificate validation; a public HTTPS URL does not automatically encrypt internal forwarding. Passing encrypted bytes through without terminating them has different routing visibility, which Part II examines.
Heron's payload rate is bytes per second before headers and encryption overhead. Read this as request rate multiplied by response payload size. A terminating proxy sends those response bytes toward clients and receives them from upstreams on misses, so its capacity accounting includes both interfaces and active TLS sessions. It cannot be sized from certificate count alone.
A public-edge proxy should remove or normalize caller-provided forwarding headers before writing its own. Subsequent trusted hops can append metadata according to one documented convention. The application accepts that convention only from authenticated or otherwise restricted proxy peers. The proxy must also normalize ambiguous request framing so that downstreams do not interpret a different body boundary.
One practical mistake is building absolute redirects from an untrusted Host or scheme header. Another is treating source IP as a user identity behind NAT or a proxy. Forwarded metadata can describe transport context, but the authenticated principal and application permission remain separate. This distinction returns in Part VIII when we trace the scorer's write.
#Section 4Connection pools, timeouts and retries
A proxy opens a fresh upstream TCP connection for every score read. Even if the application is fast, connection setup becomes repeated work. An upstream connection pool retains connections for reuse, with limits on active and idle resources.
Heron's five application endpoints each permit an illustrative pool of 20 sockets from one proxy, giving upstream sockets for that proxy. That is a pool limit rather than 100 concurrent application requests in every protocol. A connection may be idle, carry one HTTP request or carry several multiplexed requests. Count the unit the limit actually controls.
A timeout should bound a named interval, such as connecting, waiting for response headers or completing the whole request. Give the upstream the remaining request deadline rather than starting a fresh full deadline at every hop. In the complete route, Heron's 250-millisecond end-to-end budget leaves only 110 milliseconds for application work after the declared network and proxy allowances.
Retries require the same discipline. If three layers independently make up to three attempts, one user request can cause leaf attempts. Read this as multiplying the attempt limits, including each initial attempt. A retry at one owner, within a shared deadline and budget, prevents that multiplication. A timed-out write may already have committed; a stable operation identity is required before retrying it.
A pool also concentrates stale endpoints. When an application drains, stop new assignment and decide whether idle connections should close. Existing requests may finish, while long streams need an explicit drain deadline and reconnect contract. A deploy cannot simply move an established socket to another process.