API Gateway

One entry point in front of many backend services that handles shared concerns for all of them.

What is it?

In a microservices architecture (a system built as many small, independent services instead of one big app), a client may need data from several services to render one screen. Each service may need the same things done first: check authentication, log the request, enforce a rate limit.

An API gateway sits in front of all of them and handles those shared concerns once, in one place. Then it forwards each request to the right backend service.

Explain like I'm 10

It's like a hotel concierge desk. Guests don't wander the building looking for housekeeping, room service, or the gym. They go to one desk, which checks who they are and then passes the request on or handles it right there.

Examples

A gateway handling shared concerns before routing

// Simplified API gateway middleware
async function handleRequest(req) {
  if (!isAuthenticated(req)) return { status: 401 };
  if (isRateLimited(req.userId)) return { status: 429 };

  logRequest(req);

  if (req.path.startsWith("/users")) return userService.handle(req);
  if (req.path.startsWith("/orders")) return orderService.handle(req);
}

How it works

Every client request goes to the gateway first, never straight to a backend service. The gateway applies shared logic (authentication, rate limiting, logging, sometimes combining data from several services into one response), routes the request to the right backend, and relays the response back.

Why does it exist?

Without a gateway, every microservice would reimplement the same authentication, rate limiting, and logging. That's duplicated work and an easy way for inconsistencies and security gaps to creep in. A gateway keeps those concerns in one place.

When to use it

Add one when clients talk to several backend services and shared concerns (auth, rate limiting, logging) would otherwise be duplicated in each.

When not to use it

For a single backend service or a simple monolith, a gateway is an extra layer with no real benefit yet. It pays off once there are several services to put behind it.

Common mistakes

  • Piling so much business logic into the gateway that it becomes its own tangled monolith, defeating the point of splitting services.

  • Treating the gateway as optional. Every request flows through it, so it's a critical single point of failure that needs its own redundancy.

  • Forgetting the gateway adds an extra network hop and a little latency to every request.

Practice exercises

  1. Easy:

    Explain in your own words what problem an API gateway solves for a system with many microservices.

  2. Medium:

    List three concerns that make sense to handle in an API gateway rather than in each backend service.

  3. Hard:

    Explain why an API gateway itself must be highly available, given that every request passes through it.

Interview questions

What is an API gateway?

A single entry point in front of several backend services. It handles shared concerns like authentication and rate limiting, then routes requests to the right service.

Why not just handle authentication separately in each microservice?

That duplicates the same logic in every service and raises the risk of inconsistencies or security gaps. A gateway keeps it in one well-tested place.

What's a risk of putting too much logic in an API gateway?

It can become an overly complex monolith of its own, undermining the benefit of separate, independently maintained services behind it.