Dev tools
API Gateway vs Load Balancer: What Each One Does
API gateway vs load balancer: one manages and secures API calls, the other spreads traffic across servers. See how they differ and work together.
In API gateway vs load balancer, the short answer is that they solve different problems and are usually used together. A load balancer spreads incoming traffic across several copies of the same server so that none is overloaded and a failed one is skipped. An API gateway is the single front door to your APIs: it knows what each request is asking for, checks who is calling, enforces limits and sends the request to the right service. One is about capacity and availability; the other is about API management and control.
This guide explains the difference between API gateway and load balancer in plain terms: what each does, where each sits in the request path, a table of the key differences, a worked request flow, which comes first, how to choose, and the mistakes that make people buy one when they needed the other. The examples use AWS terms because its documentation is public and precise, but the ideas apply to any cloud or to self-hosted tools.
API Gateway vs Load Balancer at a Glance
| Load balancer | API gateway | |
|---|---|---|
| Main job | Distribute traffic across servers | Manage, secure and route API calls |
| Typical layer | Layer 4 (TCP/UDP) or layer 7 (HTTP) | Layer 7 (application) |
| Knows about APIs? | Only enough to route (paths, hosts, headers) | Yes: endpoints, versions, clients, plans |
| Authentication | Limited (some can authenticate users) | Core feature: API keys, tokens, OAuth checks |
| Rate limiting and quotas | Not its job | Core feature |
| Request and response changes | Rare | Common: transform, validate, add headers |
| Service discovery and routing | Targets in a group | Routes to different back-end services per path |
| Health checks | Core feature | Often delegated to the load balancer behind it |
| Scales | Very high connection and request volume | Depends on policy work per request |
| Cost model | Hours plus capacity | Often per request plus data |
If you remember one thing: the load balancer answers "which server?", and the API gateway answers "may this caller do this, and which service handles it?".
What Is a Load Balancer?
A load balancer sits in front of a group of identical servers and shares requests among them. AWS's documentation describes it as distributing traffic across multiple targets in one or more availability zones, monitoring their health and routing traffic only to healthy ones. That gives two benefits: scalability (add servers to handle more traffic) and availability (if one server fails, the others carry on).
Load balancers come in two main flavours:
- Layer 4 (network) load balancers work with TCP and UDP connections. They look at IP addresses and ports and don't read the content. AWS's Network Load Balancer works at layer 4 and handles millions of requests per second. It's the right tool for raw performance or for non-HTTP protocols.
- Layer 7 (application) load balancers understand HTTP. AWS's Application Load Balancer works at the seventh layer and evaluates listener rules in priority order, routing by path, host name, headers, methods or query parameters. The default routing algorithm is round robin, with "least outstanding requests" as an alternative. It can terminate TLS (decrypt traffic so the servers don't have to), redirect, return fixed responses and authenticate users through an identity provider.
Whatever the layer, a load balancer's core features are the same: a pool of targets, health checks, a balancing algorithm and, in the cloud, automatic scaling of its own capacity. The common algorithms:
| Algorithm | How it picks a server | Good for |
|---|---|---|
| Round robin | Takes turns in order | Similar servers and similar requests |
| Least connections | Server with the fewest active connections | Requests of uneven length |
| IP hash | Same client address goes to the same server | Simple session stickiness |
| Weighted | More traffic to bigger servers | Mixed hardware |
In NGINX, for example, round robin is the default, and a pool is just an upstream block:
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com;
server 192.0.0.1 backup;
}
server {
location / {
proxy_pass http://backend;
}
}The first server gets five times the traffic of the second (default weight is 1), and the backup server is used only when the others are unavailable. Our comparison of NGINX vs Caddy shows how two popular servers handle this.
What Is an API Gateway?
What is an API gateway? It's a service that acts as the single entry point for clients calling your APIs, usually placed in front of several back-end services or microservices. Instead of every client knowing the address of every service, clients call the gateway, and the gateway applies rules and forwards the request.
The standard jobs of an API gateway:
- Routing.
/orders/*goes to the orders service,/users/*to the users service, and different API versions can go to different back ends. - Authentication and authorisation. Check API keys, JSON Web Tokens or OAuth tokens once at the edge, so each service doesn't have to.
- Rate limiting and quotas. Protect services from overload and enforce plans per client. Our guide to API rate limiting covers the algorithms.
- Request and response handling. Validate input, transform formats, add or strip headers, combine several back-end calls into one response.
- Caching. Answer repeated requests without troubling the service.
- Observability. One place for logs, metrics and traces of API traffic.
- Developer experience. Documentation, API keys and usage plans for external users.
A concrete example is AWS's API Gateway, which throttles requests with the token bucket algorithm: a bucket refills at a steady rate (the allowed requests per second) and holds a maximum number of tokens (the burst), and each request takes a token. By default it limits an account to 10,000 requests per second per Region, with burst capacity from a bucket of up to 5,000 requests, and clients that exceed it get 429 Too Many Requests responses. You can set tighter limits per stage, per method or per client through usage plans tied to API keys. These are targets rather than guaranteed ceilings, according to the documentation.
A gateway for AI model traffic is a specialised version of the same idea, covered in our guide to the LLM gateway.
API Gateway vs Load Balancer: Key Differences
The difference between API gateway and load balancer comes down to what each one understands:
| Question | Load balancer | API gateway |
|---|---|---|
| Does it care who is calling? | Not normally | Always |
| Does it enforce per-client limits? | No | Yes |
| Does it change requests? | Rarely | Often |
| Does it choose between different services? | Between copies of one service | Between different services |
| Does it protect from overload? | Spreads it out | Rejects or queues excess |
| What breaks if it fails? | Traffic stops reaching the servers | Traffic stops, and policy enforcement is lost |
| Natural owner | Platform or infrastructure team | API or platform team |
A load balancer treats every request as a unit of work to place somewhere. An API gateway treats each request as a call by a particular client to a particular operation, and applies policy. A layer 7 load balancer can look like a gateway because it can route by path and host and authenticate users, so the line blurs. A gateway, though, adds the API-specific parts: keys, plans, quotas, transformation and a developer portal.
Application Load Balancer vs API Gateway
Application load balancer vs API gateway is the comparison people search for most often, because the two overlap on path-based routing. The decision usually looks like this:
| You need | Use |
|---|---|
| Just spread HTTP traffic across identical servers, route by path or host | Application load balancer |
| API keys, per-client quotas and usage plans | API gateway |
| Request validation or transformation at the edge | API gateway |
| Lowest cost for very high request volume | Load balancer (a gateway's per-request pricing adds up) |
| Non-HTTP traffic such as raw TCP or UDP | Network (layer 4) load balancer |
| Serverless functions behind an HTTP API with built-in throttling | API gateway |
| Public API sold to third parties | API gateway |
Cost is the other reason to be careful. The two are billed differently: load balancers are typically charged by hours and capacity used, while gateways often charge per request and for data transfer, so a gateway in front of a very high-volume internal service can cost far more than a load balancer doing the same routing. Check each provider's current pricing pages rather than relying on rules of thumb.
Which Comes First?
A common question is which comes first, the load balancer or the gateway. There's no single answer, but two patterns cover most systems:
Pattern 1: load balancer in front of the gateway. Clients reach a load balancer, which spreads traffic across several gateway instances (so the gateway isn't a single point of failure). Each gateway applies policy and forwards to services, often through another load balancer in front of each service's instances. This is common when you run your own gateway software.
Pattern 2: managed gateway at the edge. A cloud API gateway is itself a scaled, redundant service, so clients call it directly, and it calls load-balanced back ends. You don't put a load balancer in front of it.
In both patterns there are usually load balancers behind the gateway, because each back-end service has several instances. The rule of thumb: the gateway deals with callers, the load balancers deal with servers.
Worked Example: A Request Through a Shop's Stack
Take an online shop with an orders service and a catalogue service, each running on three instances, and a self-run gateway on three nodes. A request to GET /orders/42 travels like this:
| Step | Component | What happens |
|---|---|---|
| 1 | Edge load balancer | Receives the connection, terminates TLS, picks one of the three gateway nodes by health and load |
| 2 | API gateway node | Checks the API key, finds the customer's plan allows 100 requests per second, takes a token from their bucket |
| 3 | API gateway node | Validates the request, matches /orders/* to the orders service |
| 4 | Internal load balancer | Chooses one of the three orders instances that is passing health checks |
| 5 | Orders service | Reads the order and returns it |
| 6 | API gateway node | Adds headers, logs the call and returns the response |
Now suppose one orders instance crashes. The internal load balancer's health check fails, it stops sending traffic there and customers notice nothing. Suppose instead a customer's script starts sending 500 requests per second: the load balancer would happily forward all of it, but the gateway's token bucket rejects the excess with 429 responses, protecting the orders service and every other customer. Each component fixed a problem the other couldn't.
Related Terms: Reverse Proxy, CDN, WAF and Service Mesh
The vocabulary overlaps, so here is how the neighbours fit:
| Term | What it does | How it relates |
|---|---|---|
| Reverse proxy | Sits in front of servers and forwards client requests | Gateways and load balancers are both specialised reverse proxies |
| CDN | Caches content near users | Sits in front of everything for static and cacheable content |
| WAF | Blocks malicious requests by rules | Often attached to a load balancer or gateway |
| Service mesh | Handles service-to-service traffic inside a cluster | Controls east-west traffic; the gateway handles north-south |
| Ingress controller | Routes external traffic into a Kubernetes cluster | Often combines load balancing and gateway-like routing |
North-south traffic (client to your system) is the gateway's world; east-west traffic (service to service) is the mesh's. Many tools, such as NGINX and Envoy, can play several of these roles, which is why the labels are fuzzy.
When Do You Need an API Gateway?
You likely don't need a gateway if you have one application on a few servers: a load balancer (or the web server itself) is enough. You likely do need one when:
- You expose public or partner APIs and need keys, plans and quotas.
- You run several back-end services and want one address and one place for authentication.
- You need per-client throttling to protect shared services.
- You want to version or retire APIs without changing clients.
- You need consistent logging, metrics and policy across many teams.
A load balancer, by contrast, is needed as soon as you have more than one copy of a service or need to survive a server failure.
Common Mistakes
- Treating them as alternatives. Most production systems use both, at different points.
- Using a gateway to scale servers. A gateway applies policy; it doesn't replace load distribution and health checks for your instances.
- Putting every service behind a gateway on the internal path too. Internal calls often need only a load balancer or mesh, and a gateway there adds latency and cost.
- Making the gateway a single point of failure. Run several nodes or use a managed service.
- Doing business logic in the gateway. It should enforce policy and route, not hold domain rules that belong in services.
- Ignoring the cost model. Per-request pricing at high volume can be a surprise.
- Skipping rate limits because "the load balancer will cope". A load balancer spreads load; it won't stop one client from consuming it.
FAQ
What is the difference between an API gateway and a load balancer?
A load balancer distributes traffic across identical servers for capacity and availability. An API gateway is the managed front door for APIs: it authenticates callers, enforces rate limits, transforms requests and routes to the right service.
Can a load balancer replace an API gateway?
Only for simple cases. A layer 7 load balancer can route by path and host and sometimes authenticate users, but it lacks API keys, per-client quotas, usage plans and request transformation. If you need those, you need a gateway.
Do I need both an API gateway and a load balancer?
Often yes. The gateway handles who is calling and which service to use, and load balancers spread each service's traffic across instances and remove failed ones. Small systems with one service may need only a load balancer.
Which comes first, the load balancer or the API gateway?
With a self-run gateway, a load balancer usually sits in front of several gateway nodes, and more load balancers sit behind the gateway. With a managed cloud gateway, clients call the gateway directly and it calls load-balanced back ends.
What is an API gateway used for?
An API gateway is used to give clients one entry point to many services, to check identity, to enforce rate limits and quotas, to validate and transform requests, to cache responses and to collect logs and metrics for API traffic.