hardSenior Backend EngineerTechnology
Design an API Gateway — what responsibilities does it have and how does it route requests?
Posted 18/04/2026
by Mehedy Hasan Ador
Question Details
At a microservices company:
> "We have 20 microservices. Each has its own authentication, rate limiting, and logging. We're duplicating code across services. How does an API Gateway solve this?"
> "We have 20 microservices. Each has its own authentication, rate limiting, and logging. We're duplicating code across services. How does an API Gateway solve this?"
Suggested Solution
API Gateway Responsibilities
Client → API Gateway → Service A (users)
→ Service B (orders)
→ Service C (payments)
Cross-cutting Concerns (handled once at gateway)
Implementation (Kong / Nginx / Custom)
nginx.conf
upstream userservice { server users:3001; }
upstream orderservice { server orders:3002; }
upstream paymentservice { server payments:3003; }
server {
listen 443 ssl;
# Auth check (all routes)
authrequest /auth;
authrequestset $userid $upstreamhttpxuserid;
# Rate limiting
limitreqzone $userid zone=api:10m rate=100r/s;
# Routing
location /api/users/ {
proxypass http://userservice;
proxysetheader X-User-Id $userid;
}
location /api/orders/ {
limitreq zone=api burst=20;
proxypass http://orderservice;
}
location /api/payments/ {
limitreq zone=api burst=5;
proxypass http://paymentservice;
}
location = /auth {
internal;
proxypass http://auth_service/verify;
}
}
GraphQL Federation (Alternative)
// Single GraphQL endpoint → routes to multiple services
const gateway = new ApolloGateway({
serviceList: [
{ name: "users", url: "http://users:3001/graphql" },
{ name: "orders", url: "http://orders:3002/graphql" },
{ name: "payments", url: "http://payments:3003/graphql" },
],
});
// Client sends one query, gateway fans out to relevant services