← System Design
Core Concepts

What is an API Gateway?

Watch: "API Gateways in System Design Interviews w/ Ex-Meta Staff Engineer" — Hello Interview

An API gateway is a single entry point for all client requests to a set of backend services. It sits in front of your microservices and handles the cross-cutting work every API needs (authentication, rate limiting, routing, logging), so each service doesn't have to. Examples: Kong, AWS API Gateway, Apigee, Envoy-based gateways, Nginx configured as a gateway.

In simple words

The main gate of an office campus: the guard checks your ID, makes sure you're allowed in, limits how many visitors enter per minute, and tells you which building to go to. The buildings themselves don't each need their own guard.

API gateway in front of microservicesMobileWebPartnersAPI gatewayauth · rate limitrouting · logsUser serviceOrder servicePayment service/users/orders/payments
One entry point handles the cross-cutting work; services behind it stay focused on business logic.

Why It Exists

With microservices, a mobile app might otherwise need to know the addresses of the user service, order service, payment service and more, and each of those would reimplement auth and throttling. The gateway gives clients one stable address and one place for shared policy.

How It Works

mobile / web / partners
         |
   +-------------+   1. terminate TLS
   | API gateway |   2. authenticate (JWT / API key / OAuth)
   +-------------+   3. rate-limit per user or key
     |    |    |     4. route by path/version, maybe transform
     v    v    v     5. log, trace, collect metrics
  users orders payments   (internal services)

When to Use It (and When Not)

Key Pointers

Follow-up Questions

API gateway vs load balancer?
A load balancer spreads traffic across instances of a service and checks health. A gateway understands the API: who the caller is, what they're allowed, which service handles the path, how many calls they have left. Gateways usually sit behind or include load balancing.
Where do you enforce rate limits: gateway or service?
Coarse per-client limits at the gateway (cheap rejection before work is done); fine-grained, business-specific limits (e.g. "5 OTPs per phone per hour") in the service that understands them.
What happens if the gateway goes down?
Everything behind it is unreachable, so run multiple stateless instances across zones behind a load balancer, keep its per-request work small, and store rate-limit counters in a shared fast store.

Related