#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
5 min read
•Question 25 of 28hard

What 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 C

Popular 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