API Gateway in System Design: What It Is and How It Works
API Gateway is a server that acts as a single entry point for client requests to multiple backend services. It handles tasks like routing, authentication, and rate limiting, simplifying communication between clients and microservices.How It Works
Imagine a busy train station where many trains (backend services) arrive and depart. Instead of passengers (clients) figuring out which train to take, there is a central information desk (API Gateway) that guides them to the right train. The API Gateway receives all requests from clients and decides which backend service should handle each request.
It also performs extra helpful tasks like checking if the passenger has a ticket (authentication), making sure the station is not overcrowded (rate limiting), and translating languages if needed (protocol translation). This way, clients only talk to one place, and the gateway manages the complexity behind the scenes.
Example
import express from 'express'; import fetch from 'node-fetch'; const app = express(); const PORT = 3000; // Backend service URLs const serviceA = 'http://localhost:4000'; const serviceB = 'http://localhost:5000'; app.use(express.json()); app.all('/serviceA/*', async (req, res) => { const url = serviceA + req.originalUrl.replace('/serviceA', ''); try { const response = await fetch(url, { method: req.method, headers: req.headers, body: req.method !== 'GET' && req.method !== 'HEAD' ? JSON.stringify(req.body) : undefined }); const data = await response.text(); res.status(response.status).send(data); } catch (error) { res.status(500).send('Service A unavailable'); } }); app.all('/serviceB/*', async (req, res) => { const url = serviceB + req.originalUrl.replace('/serviceB', ''); try { const response = await fetch(url, { method: req.method, headers: req.headers, body: req.method !== 'GET' && req.method !== 'HEAD' ? JSON.stringify(req.body) : undefined }); const data = await response.text(); res.status(response.status).send(data); } catch (error) { res.status(500).send('Service B unavailable'); } }); app.listen(PORT, () => { console.log(`API Gateway running on http://localhost:${PORT}`); });
When to Use
Use an API Gateway when your system has many backend services and you want to simplify client communication. It helps by providing a single point to manage security, logging, and traffic control.
Real-world examples include large web applications, mobile apps, and microservices architectures where clients should not directly call multiple services. It also helps when you want to add features like caching or request transformation without changing backend services.
Key Points
- An API Gateway acts as a single entry point for multiple backend services.
- It handles routing, authentication, rate limiting, and more.
- It simplifies client interactions by hiding backend complexity.
- Commonly used in microservices and distributed systems.
- Helps improve security, scalability, and maintainability.