5 min read
ā¢Question 25 of 28hardWhat is an API Gateway?
Understanding API Gateway pattern.
What You'll Learn
- What an API Gateway is
- Key features
- Popular solutions
What is an API Gateway?
An API Gateway is a single entry point for all client requests that routes to appropriate microservices.
Key Features
- Routing - Direct requests to services
- Authentication - Centralized auth
- Rate limiting - Protect services
- Load balancing - Distribute traffic
- Caching - Reduce backend load
- Monitoring - Centralized logging
Architecture
code.jsJavaScript
Client ā API Gateway ā Service A
ā Service B
ā Service CPopular Solutions
- Kong - Open source, Lua plugins
- AWS API Gateway - Managed, serverless
- NGINX - High performance
- Express Gateway - Node.js based
Simple Implementation
code.jsJavaScript
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
// Authentication middleware
app.use(authenticate);
// Rate limiting
app.use(rateLimit({ windowMs: 60000, max: 100 }));
// Route to services
app.use('/api/users', createProxyMiddleware({
target: 'http://user-service:3001',
changeOrigin: true,
}));
app.use('/api/orders', createProxyMiddleware({
target: 'http://order-service:3002',
changeOrigin: true,
}));
app.use('/api/products', createProxyMiddleware({
target: 'http://product-service:3003',
changeOrigin: true,
}));
app.listen(3000);Benefits
- Single entry point simplifies client
- Centralized cross-cutting concerns
- Can transform/aggregate responses
- Easier to manage and monitor