Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 65 ยท Building Blocks of the Web Stack

API Gateway - The Front Door of Your Services

Learn what an API gateway does: routing, authentication, rate limiting, aggregation and protocol translation, with a Python gateway simulation.

When a system grows from one application into many services (users, orders, payments, search, recommendations), clients face a problem: which of those services should they call, and at what address? A phone app that must know twenty internal hostnames, log in to each, and combine twenty responses is slow, fragile and hard to change.

An API gateway solves this by being the single front door. Clients talk to one address; the gateway authenticates the call, applies limits, routes it to the right service (or several), and returns one response. It is a reverse proxy with API-specific intelligence, as described in proxies.

flowchart LR
  M["Mobile app"] --> G["API gateway"]
  W["Web app"] --> G
  P["Partner"] --> G
  G --> U["Users service"]
  G --> O["Orders service"]
  G --> Pay["Payments service"]
  G --> S["Search service"]

Figure: every client enters through the gateway; services stay private.

What a gateway does

One door, many jobs

Routing Send each path to the right service.
Authentication Check tokens once, at the edge.
Rate limiting Stop abuse before it reaches services.
Aggregation Combine several calls into one reply.

Also: logging, metrics, caching, TLS, versioning.

1 / 2
  • Routing and service discovery: map /orders/* to the orders service, find its healthy instances, and balance across them.
  • Authentication and authorization: verify a token (for example a JWT) or API key once and pass the verified identity downstream, so every service does not repeat it.
  • Rate limiting and quotas: per user, per key or per IP (see rate limiting algorithms).
  • Request and response transformation: translate between protocols (public REST to internal gRPC), rename fields, remove internal headers.
  • Aggregation (API composition): one client request fans out to several services and the gateway merges the replies.
  • Caching of public, repeatable responses.
  • TLS termination, compression and CORS handling.
  • Observability: assign a request ID, record latency and errors, and feed tracing and dashboards.
  • Versioning and canary releases: send 5% of traffic to a new version.
  • Resilience: timeouts, retries and circuit breaking toward slow services.

Gateway vs load balancer vs reverse proxy

They overlap, which is why they are confused. A load balancer spreads traffic across identical instances of one service and cares about health and distribution. A reverse proxy is the general mechanism of forwarding on behalf of servers. An API gateway works at the API level: it understands routes, identities, quotas and API versions, and usually sits in front of many different services. In practice a gateway product often contains a load balancer, and you will usually run a plain load balancer behind or in front of it for the gateway's own availability.

Load balancer Reverse proxy API gateway
Main goal Spread load Front servers Manage API access
Understands API identity? No Rarely Yes
Typical layer L4 or L7 L7 L7
Targets Copies of one service Origin servers Many different services

Aggregation and the latency payoff

Imagine a product page needs data from three services: product info (40 ms), reviews (60 ms) and stock (30 ms). A mobile client on a 100 ms round-trip network calling each in turn pays the network three times. Through a gateway in the data center, the client pays the long round trip once and the gateway calls the services in parallel on the fast internal network.

Python Client-side calls vs gateway aggregation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Here the gateway cuts the page load from 430 ms to 161 ms. The slowest service (reviews) now sets the pace, which is also a reminder to give each downstream call a timeout and a fallback so one slow service cannot hold the whole page.

Building a toy gateway

The next simulation puts the main ideas together: authenticate, rate limit, route, aggregate in parallel, and degrade gracefully when one service fails.

Python A tiny API gateway

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the 200 reply: the recommendations service is down, so the gateway returns the profile and orders anyway and marks recs as degraded. The page loses one widget instead of failing entirely. That is graceful degradation applied at the front door.

Backend for frontend (BFF)

Different clients want different shapes of data. A phone wants a small payload with few fields; a web dashboard wants rich detail; a partner wants a stable public contract. Rather than one gateway full of if client == "mobile" branches, many teams run a gateway per client type, called backend for frontend. Each BFF is owned by the team that owns that client and shapes responses for it, while the shared services stay generic.

Trade-offs and pitfalls

  • Single point of failure and bottleneck: all traffic crosses it. Run several instances behind a load balancer, keep it stateless, and watch its latency.
  • Extra hop: a few milliseconds. Worth it for the benefits, but measure.
  • Logic creep: when teams stuff business rules into the gateway it becomes a hard-to-change monolith. Keep it to cross-cutting concerns.
  • Deployment coupling: a route change needs a gateway deploy. Prefer configuration that can be updated without redeploying code.
  • Security: it concentrates trust. Services should still verify the identity the gateway passes on and not accept traffic from anywhere else.

Gateways matter most in microservice architectures; a single monolith behind a reverse proxy rarely needs one.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main purpose of an API gateway?

    The gateway fronts many services and handles cross-cutting concerns so clients and services stay simpler.
  2. 2. Why does gateway aggregation reduce latency for a mobile client?

    The long client-to-datacenter trip is paid once instead of once per service.
  3. 3. What should NOT normally live inside the gateway?

    Business logic belongs in services; a fat gateway becomes a bottleneck and a hidden monolith.
  4. 4. What does the BFF pattern mean?

    Backend for frontend shapes API responses for one client type, owned by that client's team.

Summary

  • An API gateway is the single front door for clients of a multi-service system.
  • It handles routing, authentication, rate limiting, aggregation, protocol translation, caching and observability.
  • Aggregation moves slow client round trips into fast internal calls and enables graceful degradation.
  • Use a BFF per client type when needs differ; keep business logic out of the gateway.
  • Make the gateway redundant, stateless and thin, or it becomes the bottleneck.

Next: Web and Application Servers