API Gateway

1 single front door · Part of Architecture

Key idea

Practice

7 questions · about 4 min

Explained step by step

API Gateway

An API Gateway is a tool that sits between the client and the set of backend services. It's the single entry point into the system: it hides how it's built inside and gives each client an API tailored to it. It also handles authentication, monitoring, load balancing, caching, request limiting and logging. It's like a hotel front desk: you don't go looking for housekeeping or the chef, you ask the front desk for everything and they handle it.

Microservices usually offer fine-grained APIs: each does something very small. But a client, like an order screen, needs data from several services at once. Without a gateway, the app would have to call 5 services on its own. The gateway gives 1 single entry, gathers the answers and adds management features. It's like a waiter: instead of you going to the kitchen, the bar and the till, you ask them for everything.

A good API Gateway usually has: authentication and authorization (who you are and what you can do), security, retries and circuit breaking, logging and tracing (following a request across every service), API composition (merging several answers into 1), API versions, routing and lists of allowed or blocked IPs. It's like airport security: it checks your ticket and luggage and sends you to the right gate.

The API Gateway has advantages: it hides the internal structure, gives a central view of the whole API, simplifies client code and adds monitoring, analytics and tracing in one place. If tomorrow you split 1 service into 3, the client changes nothing. It's like a company's customer service number: even if 5 areas are reorganized inside, you keep calling the same number.

Single point of failure

The API Gateway also has risks: it can be a single point of failure, it can affect performance (it adds 1 hop to every request), it can become a bottleneck if not scaled properly and configuring it can be hard. That's why several copies usually run behind a load balancer. It's like a stadium's only door: if it jams, nobody gets in, and if it's narrow, a line forms.

Backend For Frontend

In the Backend For Frontend (BFF) pattern, each frontend has its own backend: one for the phone app, another for the web. That way you don't have to twist 1 single backend to serve different interfaces. The BFF fetches data from the right services, formats it as its frontend needs and sends it. Sam Newman described it. GraphQL works very well as a BFF. It's like a tailor: instead of 1 size for everyone, each customer gets clothes made to measure.

A BFF is worth it when: maintaining 1 general backend for everyone takes a lot of work, you want to optimize the backend for one specific client, or you're already patching a general backend so it serves several interfaces. If you have 1 single website, a BFF is an extra piece. It's like having 2 kitchens in a restaurant: worth it if you have dine-in and delivery with very different menus, not if you serve 1 single menu.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

1 single front door