Cloud Api Gateway Patterns

Production engineering guide for cloud api gateway patterns covering patterns, implementation strategies, and operational best practices.

This page describes common architectural patterns for managing API traffic through cloud-native gateways. These patterns apply when designing systems that route, authenticate, transform, or monitor HTTP requests across services. The focus is on the structural and behavioral approaches to API gateway configuration, not on specific implementations.

Rate Limiting and Throttling

Rate limiting controls how many requests a client can make within a time window. A typical pattern uses a sliding window algorithm with tokens.

GET /api/v1/users/123
Authorization: Bearer token123

The gateway checks the request against a quota. Common configuration keys include:

When exceeded, the gateway returns HTTP 429 Too Many Requests with headers:

Retry-After: 60
X-RateLimit-Limit: 1000
X-RateLimit-Remaining: 0
X-RateLimit-Reset: 1678886400

Failure mode: Without proper burst handling, a client may see intermittent failures during short bursts. The gateway must distinguish between per-user and global limits.

Authentication and Authorization

Authentication typically happens at the gateway level using tokens or certificates. A common pattern validates bearer tokens against an external service.

Example request:

GET /api/v1/orders
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

The gateway verifies the token signature and extracts user identity. Common keys in configuration:

Authorization decisions often happen after authentication, using claims from tokens or external policies.

Failure mode: If the token validation service is unavailable, the gateway should either reject all requests or allow a grace period with cached rules.

Request and Response Transformation

Gateways commonly transform requests before forwarding to backends and modify responses on return. A typical transformation involves header manipulation.

Example configuration:

transformations:
  request:
    add_headers:
      X-Forwarded-For: "$remote_addr"
      X-Request-ID: "$request_id"
  response:
    remove_headers:
      - Server
      - X-Powered-By

The gateway can also rewrite paths or modify body content using JSON patches or XPath.

Failure mode: Header transformations may silently drop values that exceed size limits. The gateway should log such truncations.

Path Routing and Load Balancing

Routing is the core function of an API gateway, matching incoming requests to backend services based on path or method.

Example rule:

match:
  path: "/api/v1/users/{id}"
  method: "GET"
backend:
  service: "user-service"
  port: 8080

The gateway can apply weighted routing for canary deployments:

backends:
  - service: "user-service-v1"
    weight: 90
  - service: "user-service-v2"
    weight: 10

Failure mode: If a backend becomes unavailable, the gateway must return appropriate errors without exposing internal service names. A missing default route causes silent failures.

Circuit Breaking

Circuit breakers prevent cascading failures by temporarily halting requests to unhealthy backends.

Configuration keys:

When a backend exceeds failure thresholds, the circuit opens and returns HTTP 503 Service Unavailable for subsequent requests.

Failure mode: The circuit does not reset immediately after recovery. It requires a full timeout period before attempting to reconnect to the backend.

Request Timeout Management

Gateways set timeouts to prevent long-running requests from blocking resources.

Example configuration:

timeout:
  connect_timeout_seconds: 5
  read_timeout_seconds: 30
  write_timeout_seconds: 30

If a backend does not respond within these limits, the gateway returns HTTP 504 Gateway Timeout.

Failure mode: If timeouts are too short, legitimate slow operations may fail. If too long, the gateway may run out of connections or memory.

Health Checks and Readiness

Gateways can check backend health before routing requests. A typical pattern involves periodic probes to a /health endpoint.

Configuration:

health_check:
  path: "/health"
  interval_seconds: 30
  timeout_seconds: 5

Backends marked as unhealthy are temporarily excluded from routing.

Failure mode: Health check failures may be misinterpreted as application errors. The gateway should distinguish between transient and permanent backend issues.

Error Handling and Response Codes

Gateways often normalize error responses from backends to a consistent format.

Example transformation:

error_handling:
  default_error_code: 500
  map_status_codes:
    401: 403
    403: 403
    500: 502

The gateway can also inject custom headers or body content on error responses.

Failure mode: If the backend returns a malformed response, the gateway may fail to transform it properly. A missing error mapping causes generic HTTP 500 responses.

Request Body Validation

Gateways can validate request payloads before forwarding to backends using JSON Schema or similar validation mechanisms.

Example configuration:

validation:
  schema:
    type: "object"
    properties:
      name:
        type: "string"
        minLength: 1
    required: ["name"]

If validation fails, the gateway returns HTTP 400 Bad Request with details.

Failure mode: Validation errors may not include field-specific messages. The gateway should log which fields failed validation.

Logging and Observability

Gateways typically capture request metadata for monitoring and debugging.

Example logging configuration:

logging:
  format: "json"
  fields:
    - request_id
    - remote_addr
    - method
    - path
    - status_code
    - response_time_ms

The gateway may also emit metrics to monitoring systems like Prometheus or Datadog.

Failure mode: If logging fails, the gateway continues operating but loses visibility into request flow. Logs are often written synchronously, blocking requests during disk issues.

Cross-Origin Resource Sharing (CORS)

Gateways handle CORS preflight requests and add appropriate headers for cross-domain access.

Example configuration:

cors:
  allowed_origins: ["https://app.example.com"]
  allowed_methods: ["GET", "POST", "PUT", "DELETE"]
  allowed_headers: ["Content-Type", "Authorization"]
  max_age_seconds: 86400

The gateway adds headers like Access-Control-Allow-Origin and Access-Control-Allow-Methods.

Failure mode: If CORS configuration is incomplete, browsers may block requests with preflight errors. The gateway should not add CORS headers when the origin is not explicitly allowed.

Header Forwarding and Filtering

Gateways can control which headers pass through to backend services.

Example configuration:

forward_headers:
  include:
    - X-Request-ID
    - X-Forwarded-For
  exclude:
    - Authorization
    - X-API-Key

The gateway may also inject new headers for internal use.

Failure mode: If header filtering is too restrictive, the backend may lose required context. Headers exceeding size limits are silently truncated or dropped.

Request Rewriting and Path Transformation

Gateways can rewrite paths before forwarding to backends.

Example rule:

rewrite:
  from: "/api/v1/users/{id}"
  to: "/users/{id}"

This allows maintaining a public API version while internal services use different paths.

Failure mode: Rewrite rules may conflict with each other. The gateway should apply rules in a consistent order and log rewrites for debugging.

Response Compression

Gateways can compress response bodies using gzip or similar algorithms.

Configuration:

compression:
  enabled: true
  min_size_bytes: 1024
  content_types:
    - "application/json"
    - "text/html"

The gateway adds Content-Encoding: gzip to compressed responses.

Failure mode: If compression fails, the gateway should fall back to uncompressed responses. Compression may cause increased CPU usage during high load.

Session Affinity

Gateways can maintain sticky sessions for stateful applications.

Configuration:

session_affinity:
  enabled: true
  cookie_name: "session_id"
  ttl_seconds: 3600

The gateway sets a cookie to ensure subsequent requests from the same client go to the same backend instance.

Failure mode: If session data is lost, clients may experience inconsistent behavior. The gateway should handle missing session cookies gracefully.

This page was rewritten on 16 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.