Load balancing and API gateways
We'll cover the following
- Visual summary
- The problem: one server, ten times the traffic
- Follow one request through the layers
- L4 vs L7: how much does the LB look at?
- Algorithms: how to pick a server
- Why servers must be stateless
- Is the LB a single point of failure?
- LB vs API gateway
- How the gateway finds an instance of Service A
- Knowing the client's IP
- Summary
Visual summary

Download imageFree to share, please link back to this lesson.
The problem: one server, ten times the traffic
One app server can handle about 1,000 requests per second. Your traffic is 10,000. You need at least 10 servers, and in practice a few more so none runs flat out. But users only know one address, api.example.com. Something has to take each incoming request and pick a server. That something is a load balancer (LB).
A load balancer sits in front of many identical servers and spreads requests across them. It gives you scale, and it survives a server dying because it health-checks every server and stops sending traffic to dead ones.
Follow one request through the layers
A real system has more than one tier of balancing. Follow a single request, GET /orders/42, from the user to the code that answers it:
1. Client -> api.example.com (DNS returns the front LB address)
2. LB #1 picks 1 of N gateway instances (balancing: which gateway?)
3. Gateway checks auth + rate limit, then reads the path
/orders/* -> Service A (routing: which service?)
4. Service A one stable name, 100 instances
internal LB or discovery picks 1 (balancing: which instance?)
5. Instance 37 runs the code, replies back down the same path
Client -> LB #1 -> [Gateway x N] -> Service A -> instances
-> Service B -> instancesTwo different jobs are hiding here. Routing decides which service handles the request, based on the path: "which kitchen, pizza or sushi?". The gateway always does this. Balancing decides which instance of that service: "which chef in that kitchen?". Step 2 balances across the gateways, because the gateway is itself a fleet of identical servers. Step 4 balances across the instances of one service. The rest of this lesson zooms into each step.
L4 vs L7: how much does the LB look at?
| L4 (transport) | L7 (application) | |
|---|---|---|
| Looks at | IP and port only (TCP/UDP) | The HTTP request: URL, headers, cookies |
| Can do | Fast forwarding | Path routing (/api vs /images), TLS termination, auth, rate limiting |
| Cost | Very fast, simple | Slower, smarter |
| Example | AWS NLB | AWS ALB, Nginx |
Big systems often use both: L4 at the edge for raw throughput, feeding a fleet of L7 LBs.
What to say in an interview: say "L7 LB" and move on. Mention L4 only for non-HTTP traffic (raw TCP, game servers, databases) or when you need extreme throughput.
Algorithms: how to pick a server
- Round-robin: take turns. Choose it when servers and requests are about equal.
- Least connections: send to the server with the fewest active requests. Choose it when request times vary a lot, so a slow request does not pile up on one server.
- Weighted: bigger servers get more traffic. Choose it when the fleet mixes machine sizes.
- IP hash / consistent hashing: the same client or key always goes to the same server. Choose it for sticky sessions or per-server caches.
Why servers must be stateless
Say a user logs in and their session lives in server 1 memory. The next request lands on server 2, which knows nothing about them, and they are randomly logged out. Any server must be able to handle any request. That is why LBs work at all, and why gateway instances must be stateless too. Fixes, best first:
- Externalize the session into Redis. Every server reads the same store. This is the best option.
- Carry the state in a token (JWT). The request itself proves who the user is.
- Sticky sessions (hash the client to one server). A fragile last resort: if server 1 dies, its sessions die with it.
Is the LB a single point of failure?
One LB in front of everything would be exactly that, so you run more than one:
- Active-passive: a standby takes over a shared virtual IP when the active one fails (keepalived/VRRP).
- Active-active: DNS returns several LB IPs and clients spread across them.
- Managed LB (ALB/NLB, GCP LB): already a distributed fleet across multiple availability zones, so you get this for free.
- Anycast for global setups: the same IP is announced from many places and traffic goes to the nearest healthy one.
LB vs API gateway
- LB: spreads traffic across identical servers. Its job is availability and distribution.
- API gateway: the front door for APIs. Auth, rate limiting, API keys, routing to different microservices, request transformation, versioning. Under the hood it is an L7 proxy.
They stack rather than compete: LB #1 balances across gateway instances, and the gateway routes to services.
How the gateway finds an instance of Service A
Service A has 100 instances that come and go as it scales. The gateway cannot hardcode their addresses. There are two standard answers:
| Option | How | Notes |
|---|---|---|
| 1. Internal LB per service | Gateway sends to a stable address like service-a.internal; that LB picks an instance | Simple; the gateway knows one address per service |
| 2. Service discovery (client-side balancing) | Instances register in a registry (Consul, Eureka, K8s/etcd). Gateway gets the healthy list and picks one itself | No extra hop; Netflix and service meshes (Envoy/Istio) work like this. Gateway acts as a LB too. |
In Kubernetes the gateway calls http://service-a, and K8s DNS plus kube-proxy spread the calls over the pods (option 1 built in). Scaling A from 100 to 200 instances is invisible to the gateway.
Knowing the client's IP
- The source IP is part of every connection; you cannot hide it.
- With an L4 passthrough LB, the backend sees the client IP directly.
- With an L7 proxy, the backend sees the LB's IP instead, so the LB adds an
X-Forwarded-Forheader carrying the real client IP. - Many users behind one office or carrier (NAT) share a single IP. So never identify or rate-limit people by IP alone; use auth tokens.
Check yourselfUsers keep getting logged out at random after you add a second app server. What is wrong and how do you fix it?
Sessions live in the memory of one server, so requests that land on the other server do not find them. Make the servers stateless: store sessions in Redis (best) or carry state in a token such as a JWT. Sticky sessions would work but are fragile, because if that server dies its sessions die.
Check yourselfA request for /orders/42 reaches your gateway. Which part is routing and which is balancing?
Routing is the gateway reading the path and choosing the orders service. Balancing is picking one instance of that service, done by an internal LB or by service discovery. Routing picks the service, balancing picks the instance.
Check yourselfYou put one LB in front of ten servers. Why is that still a risk, and how do you remove it?
The LB itself is now a single point of failure. Run an active-passive pair with a virtual IP, run active-active LBs with DNS returning several IPs, or use a managed multi-AZ LB. For global traffic, use anycast.
Summary: say it in the interview
- "L7 LB with least-connections and health checks in front of stateless app servers. Gateway routes by path to a service name; an internal LB or service discovery resolves that to a healthy instance. Each tier is horizontally scaled."