← Building blocks

Terminate TLS at the edge

Networking & deliveryfundamental

Every app server handles certificates and the TLS handshake cost.

Say it like this

Certificates and handshakes are an operational and CPU cost, so I terminate TLS once at the load balancer or edge with automated certificates and keep the fleet behind it on plain HTTP or mesh mTLS.

Basic solution

Terminate TLS at the load balancer or CDN, use plain or mTLS inside the network.

ALB / nginx TLSLet’s Encryptservice mesh mTLS

Terminate once, forward plain

The handshake and decryption happen at the balancer, which then forwards plain HTTP with forwarded headers. Certificates renew on the balancer only; app servers never see a private key.

How it works

  1. The client opens a TCP connection to the load balancer and starts a TLS handshake. In its first message it lists the cipher suites it supports and names the host it wants (the SNI field, Server Name Indication), which is what lets one balancer serve many hostnames.
  2. The balancer looks up the certificate for that hostname, signs part of the handshake with the matching private key to prove it owns the certificate, and both sides derive the session keys that will encrypt the connection. With TLS 1.3 this costs one network round trip; if the client has connected recently and holds a session ticket, resumption costs no extra round trip at all.
  3. From then on the balancer decrypts every incoming byte and encrypts every response. Because it can now read the plain HTTP request, it can route by path, add or strip headers, and apply rate limits before anything reaches an application server.
  4. The balancer forwards the decrypted HTTP request to an application server over the private network. It adds X-Forwarded-For (the original client IP, which the app would otherwise see as the balancer's address) and X-Forwarded-Proto (the original scheme, https) so the app can log, rate limit and build redirects correctly.
  5. Certificates are renewed automatically: an ACME client, or the cloud provider's managed certificate service, requests a fresh certificate before expiry and installs it on the balancer. No application server is touched, redeployed or restarted for a renewal.

What the app servers no longer do

  • They never hold a private key, never run a handshake and never get redeployed for a certificate renewal, so they scale on application work alone.

Worked example

URL shortener redirect fleet

  • 40K redirects/s at peak. Browsers reuse connections, but with many distinct users roughly one in five requests needs a new TLS connection: about 8K handshakes/s.
  • A full TLS 1.3 handshake with an ECDSA P-256 certificate costs roughly 0.5 ms of CPU; 8K/s is about 4 CPU cores of pure handshake work. Session resumption brings most repeat visitors to near zero.
  • Terminating at the balancer: those 4 cores live on the balancer tier, which is sized for it, and the 20 redirect servers run on plain HTTP, each handling 2K requests/s of trivial work.
  • Latency: a user 20 ms away pays one round trip (20 ms) for TLS 1.3 before the first byte, versus two round trips (40 ms) on TLS 1.2. Terminating at a CDN edge 5 ms away cuts that to 5 ms, then the edge reuses a warm connection to the origin.
  • Certificates: one wildcard certificate for the short domain plus per-customer certificates for custom domains, issued through ACME and renewed every 60 days with no deploys.

Variants and when each wins

Terminate at the load balancer

  • The balancer holds the certificates, runs every handshake and forwards plain HTTP over the private network. It wins as the default whenever the network between balancer and app servers is yours alone.

Terminate at the CDN edge, re-encrypt to origin

  • The handshake happens at an edge site a few milliseconds from the user, and the edge keeps a pool of warm, long-lived TLS connections to your origin. It wins when users are far from your region, because the user pays only the short round trip and the origin pays a handshake once per pooled connection.

TLS passthrough

  • The balancer forwards the still-encrypted bytes untouched (layer 4) and the application terminates TLS itself. It wins when the application must see the client's own certificate for mutual TLS, or when policy forbids the balancer seeing plaintext; the cost is that the balancer can route only by SNI, not by path or header.

End-to-end mTLS with a service mesh

  • Every hop between services is encrypted and authenticated using short-lived workload certificates that the mesh issues and rotates. It wins in zero-trust or shared networks where internal traffic cannot be assumed private; the cost is a sidecar or library on every service and more handshakes inside the cluster.

Recommendation: terminate at the edge or balancer, keep internal hops plain in a private network, and move to mesh mTLS when the network is shared or compliance requires it.

Failure modes

Expired certificate

  • Every client starts seeing a certificate error at the same moment. A manual renewal was missed, so the certificate passed its expiry date while still installed. Automate issuance with ACME, renew at two thirds of the certificate's lifetime, and alert on days-to-expiry so a failed renewal is noticed weeks before it matters.

Trusting forwarded headers from anyone

  • Rate limits and audit logs show client IPs that are plainly spoofed. The application trusts the X-Forwarded-For header no matter where the request came from, so a client can write any address into it. Have the balancer overwrite that header with the address it actually accepted the connection from, and have the application accept forwarded headers only when the request arrived from the balancer's own addresses.

Mixed-content and redirect loops

  • The application redirects every request from HTTP to HTTPS, forever. It sees plain HTTP arriving from the balancer, does not read X-Forwarded-Proto, and so believes the user is on HTTP. Honour the forwarded scheme when deciding whether to redirect, and set an HSTS header at the edge so browsers go straight to HTTPS in future.

Handshake CPU saturating the balancer during a traffic burst

  • Clients get connection errors while the application servers sit mostly idle. A reconnect storm, for example a mobile network flapping or a deploy that closed every connection, drives thousands of full handshakes per second and exhausts the balancer's CPU. Enable session tickets so repeat connections resume cheaply, use ECDSA certificates whose handshakes cost less than RSA, keep connections alive longer, and leave CPU headroom in the balancer tier.

Plaintext on a shared network

  • An audit flags internal traffic as unencrypted. TLS is terminated at the edge and the hop to the origin crosses a network that other tenants can reach, so packets travel in the clear. Re-encrypt from the edge to the origin, or adopt mesh mTLS so every internal hop is encrypted.

Interviewer follow-ups

Answer out loud before you open each one.

What does the balancer need to pass to the app once it terminates TLS?

The original client address (X-Forwarded-For or the PROXY protocol), the original scheme (X-Forwarded-Proto) and the original host. The app uses them for rate limiting, redirects and logging. It must only trust those headers when the request arrives from the balancer.

How does TLS 1.3 change the latency story?

A full handshake drops from two round trips to one, and resumption allows sending data on the first flight (0-RTT) for idempotent requests. For a user 100 ms away that is 100 ms saved on every new connection. Combined with edge termination, the user-visible handshake is a few milliseconds.

How would you handle thousands of customer custom domains?

Issue a certificate per domain through ACME after the customer proves control with a DNS or HTTP challenge, store them in the balancer or edge certificate store, and select by SNI at handshake time. Renewal is automated; the limit is usually the provider quota, not the balancer.

When would you not terminate at the edge?

When the edge is a third party and policy forbids it seeing plaintext (some financial and health data), or when the application needs client certificates for mutual TLS. Then the balancer does TLS passthrough and routes by SNI, giving up path-based routing and header injection at that layer.

Connections

Pairs with
Spread traffic across servers · Put a reverse proxy in front · Serve static bytes from the edge · Protect data and secrets · One front door for many services

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Application Load BalancerLoad balancerCloud Load BalancingApplication Gateway, Azure Load BalancerNGINX, HAProxy, Envoy
Amazon EC2 Auto ScalingRedirect serversCompute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad