Architecture6 min read

API Gateway Architect

Designs API gateway architectures - authentication, rate limiting, routing, response transformation, and gateway selection for microservices.

API gatewayrate limitingauthenticationroutingKongAPI management

The API Gateway Architect places and configures the gateway layer that sits between API clients and backend services. The gateway handles cross-cutting concerns - authentication, rate limiting, routing, logging - so individual services don't have to. The key decision is which concerns belong in the gateway vs in services.

Gateway Responsibilities

ConcernGatewayService
TLS termination✅ Always❌ Handle plain HTTP internally
Authentication (JWT verify)✅ Verify token, pass userId header❌ Trust gateway's user header
Authorization (role checks)⚠️ Coarse-grained only✅ Resource-level checks in service
Rate limiting✅ Global and per-user limits⚠️ Service-specific only if needed
Request logging✅ All inbound traffic⚠️ Business events only
Response transformation⚠️ Light only (add/remove headers)✅ Business-level response shaping

Nginx as API Gateway

# nginx.conf - simple API gateway
upstream user_service    { server user-service:3001; }
upstream order_service   { server order-service:3002; }
upstream product_service { server product-service:3003; }

server {
  listen 443 ssl;
  ssl_certificate     /etc/nginx/certs/cert.pem;
  ssl_certificate_key /etc/nginx/certs/key.pem;

  # Rate limiting - 100 req/min per IP
  limit_req_zone $binary_remote_addr zone=api:10m rate=100r/m;
  limit_req zone=api burst=20 nodelay;

  # Route by path prefix
  location /api/users/   { proxy_pass http://user_service;    proxy_set_header X-Real-IP $remote_addr; }
  location /api/orders/  { proxy_pass http://order_service;   proxy_set_header X-Real-IP $remote_addr; }
  location /api/products/{ proxy_pass http://product_service; proxy_set_header X-Real-IP $remote_addr; }

  # Auth handled by auth_request to auth service
  location /api/ {
    auth_request /auth;
    auth_request_set $user_id $upstream_http_x_user_id;
    proxy_set_header X-User-Id $user_id;
  }
  location = /auth {
    internal;
    proxy_pass http://auth-service/verify;
    proxy_pass_request_body off;
    proxy_set_header Authorization $http_authorization;
  }
}

Token Bucket Rate Limiting (Redis)

// Per-user rate limiting with sliding window
async function checkRateLimit(userId: string, limit: number, windowSec: number): Promise<boolean> {
  const key = `rate:${userId}:${Math.floor(Date.now() / 1000 / windowSec)}`;
  const count = await redis.incr(key);
  if (count === 1) await redis.expire(key, windowSec);
  return count <= limit;
}

// Middleware
app.use(async (req, res, next) => {
  const allowed = await checkRateLimit(req.user.id, 100, 60); // 100 req/min
  if (!allowed) return res.status(429).json({ error: { code: 'RATE_LIMITED' } });
  next();
});

Explore all agents

Browse the full library of specialized AI agents.