Core Building Blocks

API Gateways: Traffic Management, Security, and Observability

Learn how API gateways act as a single entry point for clients, handling traffic management, security, rate limiting, and observability for microservices.

20 min readapi gatewaymicroservicestraffic managementsecurityobservabilitysystem design

Why API Gateways Matter

In a microservices architecture, clients should not communicate directly with individual services. An API gateway acts as a single entry point, providing cross-cutting concerns like authentication, rate limiting, and routing, while keeping services focused on their core business logic.

Analogy: Think of an API gateway as a hotel concierge. Instead of guests knocking on every door to find the right service, the concierge directs them to the right room, handles check-in (authentication), and ensures they follow hotel policies (rate limits, security).


Core Responsibilities of an API Gateway

1. Traffic Management

  • Request routing: Based on URL path, headers, or other criteria
  • Protocol translation: e.g., HTTP/WebSocket to internal gRPC
  • Load balancing: Distributing requests across service instances
  • Request/response transformation: Modifying headers, payloads, or URLs

2. Security

  • Authentication: Validating JWTs, API keys, or OAuth tokens
  • Authorization: Checking permissions for specific endpoints
  • SSL/TLS termination: Offloading encryption from services
  • IP allowlisting/blocklisting: Controlling access by IP address
  • Bot detection: Identifying and blocking malicious traffic

3. Observability

  • Logging: Recording request/response details for debugging
  • Metrics: Collecting latency, error rates, and throughput
  • Tracing: Propagating trace IDs for distributed tracing
  • Monitoring: Alerting on anomalies like error spikes or latency increases

4. Resilience Patterns

  • Rate limiting: Protecting services from overload
  • Circuit breaking: Failing fast when a service is unhealthy
  • Retry logic: Automatically retrying failed requests (with backoff)
  • Timeouts: Preventing clients from waiting indefinitely

5. Additional Features

  • API aggregation: Combining responses from multiple services
  • Caching: Storing frequent responses to reduce backend load
  • Request validation: Ensuring requests conform to schemas before forwarding
  • Response compression: Reducing payload size with gzip or Brotli
💡

Not all gateways are equal: Some focus more on security (e.g., Apigee), others on performance (e.g., Kong, Envoy), and others on developer experience (e.g., AWS API Gateway). Choose based on your primary needs.


API Gateway Patterns

Edge Gateway

Deployed at the network edge to handle external traffic. Typically provides SSL termination, WAF capabilities, and global load balancing.

Use case: Public-facing APIs for mobile apps, web clients, or third-party developers.

Service Mesh Sidecar

While not a traditional API gateway, service meshes like Istio use sidecar proxies to handle inter-service communication with similar features (mTLS, retries, observability).

Use case: Internal service-to-service communication where you want uniform policies without changing service code.

Gateway per Service

Each service has its own dedicated gateway for fine-grained control.

Use case: When services have vastly different requirements (e.g., one needs strict rate limiting, another needs complex transformation).

Shared Gateway

A single gateway handles traffic for multiple services.

Use case: Most common pattern for microservices, reducing operational overhead.


Key Implementation Considerations

Performance

  • Latency: Gateways add hop latency; keep it minimal (< 1ms ideally)
  • Throughput: Ensure the gateway can handle your peak RPS
  • Scalability: Horizontal scaling is essential for handling growth

Security

  • Defense in depth: Gateway is one layer; services should also validate auth
  • Secret management: Securely store API keys, certificates, and tokens
  • Regular updates: Keep gateway software patched against vulnerabilities

Operational Complexity

  • Configuration: Use declarative configs (YAML, JSON) or APIs for automation
  • Monitoring: Gateway metrics are critical for system health
  • Debugging: Ability to trace requests through the gateway
⚠️

Gateway as a bottleneck: If not scaled properly, the gateway can become a single point of failure and performance bottleneck. Always design for horizontal scaling and have a rollback plan.


Cloud-Native Managed Services

  • AWS API Gateway: Fully managed, integrates with Lambda, Cognito, CloudWatch
  • Azure API Management: Rich feature set, developer portal, analytics
  • Google Cloud Endpoints: OpenAPI-based, integrates with Cloud Run

Open-Source Self-Managed

  • Kong: Plugin-based architecture, high performance, multi-protocol
  • Envoy Proxy: High-performance proxy, often used with Istio service mesh
  • Traefik: Modern, cloud-native, automatic service discovery (Docker, K8s)
  • HAProxy: Reliable, high-performance TCP/HTTP load balancer with advanced features
  • NGINX Plus: Commercial version of NGINX with advanced API gateway features

Developer-Focused

  • AWS API Gateway (HTTP APIs): Simpler, lower-cost option for basic APIs
  • Netlify Edge Functions: Run code at the edge for lightweight gateway logic
  • Cloudflare Workers: Programmable network for edge-based API management

Choosing a gateway: Start with your cloud provider's managed service if you're already in that ecosystem. For multi-cloud or on-prem, consider Kong or Envoy. If you need extreme performance, look at Envoy or HAProxy.


API Gateway vs Other Patterns

API Gateway vs Service Mesh

  • API Gateway: Primarily for north-south traffic (client to service)
  • Service Mesh: Primarily for east-west traffic (service to service)
  • Overlap: Some features like mTLS and retries appear in both

API Gateway vs Reverse Proxy

  • Reverse Proxy: General term for servers like NGINX that sit behind firewalls
  • API Gateway: A reverse proxy with additional features tailored for APIs (rate limiting, auth, etc.)

API Gateway vs Load Balancer

  • Load Balancer: Distributes traffic across servers (L4 or L7)
  • API Gateway: Does load balancing plus much more (security, transformation, etc.)
  • Relationship: Many API gateways use load balancing as one of their features

What to Remember for Interviews

  1. Core functions: Know the main responsibilities: traffic management, security, observability, resilience.
  2. Patterns: Understand edge gateway vs shared gateway vs gateway per service.
  3. Features: Be familiar with rate limiting, auth, SSL termination, retries, circuit breaking.
  4. Trade-offs: Recognize that gateways add latency and complexity but provide critical cross-cutting concerns.
  5. Technology awareness: Know the differences between managed services (AWS API GW) and self-managed (Kong, Envoy).

Practice: Draw a diagram of a microservices architecture with clients, API gateway, and several services (user, order, payment). Show how the gateway handles authentication, routes requests, applies rate limiting, and collects metrics.