Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 37 ยท Communication

API Gateway and Backend for Frontend

Put one front door in front of your microservices: gateway routing, aggregation, authentication offloading and rate limiting, plus the Backend for Frontend pattern.

A shop built from ten microservices should not make a phone app know ten addresses, ten authentication schemes and ten error formats. An API gateway is a single entry point that sits in front of the services. Clients talk to the gateway; the gateway decides which service gets each request.

Besides hiding the internal layout, the gateway is the natural place for jobs every request needs: checking who the caller is, limiting abuse, adding correlation ids and sometimes combining several service calls into one answer. This lesson shows what a gateway does, what it should not do, and the related Backend for Frontend (BFF) pattern. You will run a working gateway stand-in with real HTTP on the loopback interface.

What a gateway does

Routing Send each path to the right service.
Aggregation Combine several replies into one.
Auth offloading Check identity once, at the door.
Rate limiting Stop one client from flooding.
1 / 2

Why a front door helps

Without a gateway, every client must:

  • Know the address and protocol of every service, and update when they move.
  • Make many calls to build one screen, which is slow on mobile networks.
  • Authenticate separately with each service.
  • Cope with services that use different error formats and rate rules.

With a gateway, the clients know one address, the internal layout can change freely, and cross-cutting rules live in one place. The cost is another component that must be fast, highly available and scaled with your traffic, because every request passes through it.

flowchart LR
    W["Web app"] --> G["API gateway"]
    M["Mobile app"] --> G
    G -->|"/users/*"| U["Users service"]
    G -->|"/orders/*"| O["Orders service"]
    G -->|"/catalog/*"| C["Catalog service"]

Clients see one address. The gateway maps URL prefixes to internal services.

Routing

The most basic job is a route table: path prefix to service address. Real gateways (NGINX, Envoy, Kong, cloud gateways) read this from configuration. Below is a real NGINX configuration for two services, shown read-only. It also forwards a correlation header and passes the client's real address on.

NGINX
upstream users_service  { server users:8080; }
upstream orders_service { server orders:8080; }

server {
  listen 80;

  location /users/ {
    proxy_pass http://users_service/;
    proxy_set_header X-Request-Id $request_id;
    proxy_set_header X-Forwarded-For $remote_addr;
  }

  location /orders/ {
    proxy_pass http://orders_service/;
    proxy_set_header X-Request-Id $request_id;
    proxy_set_header X-Forwarded-For $remote_addr;
  }
}

The same idea with Kong, a gateway configured with declarative YAML, including a rate-limiting plugin:

YAML
_format_version: "3.0"
services:
  - name: orders
    url: http://orders:8080
    routes:
      - name: orders-route
        paths: ["/orders"]
    plugins:
      - name: rate-limiting
        config:
          minute: 60
          policy: local

Which router you choose is a deployment detail. What matters in the design is what the gateway does with each request.

Authentication offloading

If every service checked passwords and tokens itself, each would need the same code, keys and bugs fixed in ten places. With authentication offloading, the gateway checks the credentials once (for example validates a token's signature and expiry), rejects bad requests early, and forwards the trusted identity to services in a header.

Offloading does not mean services may trust anything. The internal network can be reached by other things than your gateway, so services should still verify who is calling them. Authentication and Authorization Between Services covers that in depth.

Rate limiting at the door

One misbehaving client, or an attacker, should not be able to use up all your capacity. The gateway can count requests per client and answer 429 Too Many Requests beyond a limit. The algorithms (token bucket and friends) get a full lesson in Rate Limiting, Load Shedding and Backpressure.

A gateway you can run

The stand-in starts two small services (users and orders) and a gateway, all real HTTP servers on loopback ports inside one Node program. The gateway routes by prefix, rejects calls without a valid token, limits each token to three requests, and offers one aggregated endpoint, /dashboard, that calls both services at once and merges the results. The token check is a simple lookup in a table, not real JWT validation; production code would verify a signed token.

Node.js Routing, auth, rate limiting and 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 node:readline or process.stdin), it gets the next line from this box.

Look at what each caller sees: only the gateway address, one error format at the edge, and a single /dashboard call in place of two. Ada's fourth request is refused with 429 before any service wakes up, so abuse costs the services nothing.

The stand-in has real shortcomings you should recognize: the rate-limit counter never resets (a real one uses a time window), there is no timeout on the internal calls (Synchronous Communication with HTTP APIs) and a failed service would turn into an unhandled error instead of a clean 502.

Backend for Frontend

A single general-purpose gateway tends to become a compromise: the mobile app wants small payloads, the web app wants rich data, a partner API wants stable, documented behavior. Sam Newman popularized a fix, the Backend for Frontend pattern: one gateway-like backend per kind of client, each owned by the team that builds that client.

Each BFF shapes data for its own client: it calls the same internal services, but picks different fields, merges differently and can change quickly with its front end. The mobile team no longer waits for a shared gateway to be changed.

JS Two BFFs shaping the same data

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

No output.

The trade-off is duplication: several BFFs share some logic, so keep real business rules in the services and let BFFs only translate and combine.

Gateway products and trade-offs

You rarely write a gateway from scratch. Managed options include Amazon API Gateway, Azure API Management and Google Apigee; open-source options include Kong, Apache APISIX, Envoy-based gateways and NGINX; Kubernetes deployments often use an ingress controller or the Gateway API. They provide routing, TLS termination, authentication, rate limiting and throttling, request transformation, caching and analytics out of the box.

The trade-offs are real. A gateway adds latency (one more network hop and processing step) and is a potential bottleneck if it is under-provisioned, so run several instances. It abstracts the backend, which is the point, but it also hides failures unless it reports metrics. Use TLS everywhere including between gateway and services, avoid excessive aggregation, and keep business logic out of it.

Pitfalls

  • A smart gateway. Putting business rules in it means every change touches the gateway and its team becomes a bottleneck.
  • A single point of failure. Run several instances behind a load balancer (Service Discovery and Load Balancing).
  • Over-aggregation. If one endpoint calls fifteen services, one slow service slows the whole screen. Use timeouts and return partial results when possible.
  • Skipping internal security. The gateway is a door, not a wall around the whole building.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main benefit of an API gateway for clients?

    The gateway hides the internal layout, so services can move or split without changing clients.
  2. 2. What does authentication offloading mean?

    Checking at the door avoids repeating the same code in every service, though services should still protect themselves.
  3. 3. A client exceeds its limit at the gateway. Which status code is appropriate?

    429 Too Many Requests tells the client to slow down.
  4. 4. What is the Backend for Frontend pattern?

    Each BFF is owned by its client's team and tailors data for that client.
  5. 5. Which is a warning sign for a gateway?

    Business rules belong in services. A gateway that holds them becomes a bottleneck and a hidden monolith.

Summary

  • An API gateway is the single entry point that routes requests to services and hides the internal layout.
  • It is a good home for cross-cutting jobs: authentication, rate limiting, correlation ids and light aggregation.
  • Keep business rules out of the gateway, and run it in several instances because every request passes through it.
  • The Backend for Frontend pattern gives each client type its own tailored backend, owned by that client's team.
  • Services should still verify callers; the gateway is not the only security layer.

Next: Service Discovery and Load Balancing.