Communication Between Services

Lesson 6 of 10 · 18 min

x
60%

The API Gateway Pattern

External clients should not need to know about every internal microservice. An API Gateway sits at the edge as a single entry point — routing /users to the User Service, /orders to the Order Service, and /payments to the Payment Service. It centralizes cross-cutting concerns: authentication, rate limiting, request logging, SSL termination, and response aggregation. Without a gateway, clients hold URLs for five services, each with its own auth mechanism and error format. With a gateway, clients talk to one host and one API contract. The gateway can also implement the Backend for Frontend (BFF) pattern — exposing different aggregated endpoints for mobile vs web clients.

Before
Client talks to every service directly
const user   = await fetch('https://users.internal/api/me');
const orders = await fetch('https://orders.internal/api/mine');
const cart   = await fetch('https://cart.internal/api/items');
// Three hosts, three auth tokens, three failure modes
After
Client talks to one gateway
const dashboard = await fetch(
  'https://api.example.com/v1/dashboard',
  { headers: { Authorization: `Bearer ${token}` } }
);
// Gateway aggregates user + orders + cart server-side

Key Takeaway

One front door for clients — route, authenticate, and aggregate behind the gateway so services stay internal.

PreviousNext Lesson