An API gateway sits between clients and your backend services. It receives all inbound requests, applies cross-cutting concerns (authentication, rate limiting, logging, transformation), and routes requests to the appropriate upstream service. For simple monolithic applications, a gateway may seem like overkill. For anything beyond a single service talking to a single client, it's the structural component that makes the whole system manageable.
Core Gateway Responsibilities
A well-designed API gateway handles these concerns centrally so individual services don't have to:
- Authentication and authorization — validate JWTs, API keys, or OAuth tokens before requests reach services
- Rate limiting and throttling — protect backend services from traffic spikes, abuse, and DDoS
- Request routing — map incoming routes to the appropriate upstream service
- Load balancing — distribute requests across multiple instances of a service
- Request/response transformation — normalize headers, reshape payloads, add metadata
- Logging and observability — capture request metadata, latency, and error rates in one place
- TLS termination — handle HTTPS at the gateway, communicate with backends over internal network
Authentication Patterns at the Gateway
JWT Validation
The most common pattern for user-authenticated APIs. The client obtains a JWT from an auth service (Supabase Auth, Auth0, custom), includes it in the Authorization: Bearer {token} header, and the gateway validates the signature and expiry before forwarding the request.
The gateway should not re-verify the JWT on every call if performance is critical — use a local JWKS cache with periodic refresh rather than hitting the auth server on every request. AWS API Gateway, Kong, and NGINX all support JWT validation with configurable JWKS endpoints.
API Key Authentication
For machine-to-machine and third-party API access. API keys are simple (no token expiry, no OAuth flows) but require careful management: rotation policies, per-key rate limits, scope restrictions. The gateway validates the key against a store (Redis hash, database lookup) and injects the resolved identity into request headers for downstream services.
mTLS for Service-to-Service
For internal service mesh communication, mutual TLS (where both client and server present certificates) provides strong identity without API keys. This is the standard in production Kubernetes environments. Service meshes like Istio handle mTLS transparently.
Rate Limiting Patterns
Fixed Window
Simple: allow N requests per time window (e.g., 1000 requests per minute). Easy to implement, predictable. Weakness: allows 2x the limit at window boundaries — N requests at the end of one window plus N at the start of the next.
Sliding Window
Tracks requests over a rolling window rather than fixed buckets. Eliminates the boundary problem. Slightly more complex, requires storing timestamps per client. Redis sorted sets are the standard implementation.
Token Bucket
Allows burst traffic up to a maximum bucket size, with a steady refill rate. Clients can accumulate unused capacity and use it in short bursts, making it better for legitimate clients who have spiky usage patterns. AWS API Gateway uses token bucket natively.
Apply rate limits at multiple levels: per-IP for anonymous traffic, per-API-key for authenticated clients, per-route for expensive endpoints, per-account for multi-tenant systems.
Routing and Versioning
Path-Based Routing
Route to different services based on the URL path: /api/users/* goes to the user service, /api/orders/* goes to the order service. Simple and transparent to clients.
Header-Based Routing
Route based on request headers — useful for A/B testing, canary deployments, and routing beta users to new service versions without changing URLs.
API Versioning
Three common approaches:
- URL versioning:
/v1/users,/v2/users— most explicit, easiest to cache, common choice - Header versioning:
Accept: application/vnd.myapi.v2+json— keeps URLs clean but more complex to implement and cache - Query parameter:
/users?version=2— simple but pollutes the query string and breaks caching
URL versioning is the pragmatic default for most APIs. Keep old versions alive until you've confirmed clients have migrated — use analytics to track version adoption before deprecation.
Gateway Implementations to Know
AWS API Gateway
Fully managed, serverless-native, integrates tightly with Lambda. Best fit for AWS-native architectures. HTTP APIs (v2) are lower latency and cheaper than REST APIs (v1) for most use cases.
Kong
Open-source, plugin-based, runs anywhere. Extensive plugin ecosystem for auth, rate limiting, logging, transformations. Self-hosted or managed (Kong Konnect). Good for multi-cloud or on-premise requirements.
NGINX / Traefik
Configuration-based reverse proxies that can serve gateway functions. Lower level than dedicated gateways — you implement rate limiting, auth, and routing via config or Lua plugins. Common in Kubernetes ingress setups.
Cloudflare Workers as Gateway
For edge-deployed logic, Cloudflare Workers can act as an API gateway layer — auth validation, rate limiting, geo-routing, and caching at the CDN edge before requests even reach your origin. Reduces latency and moves compute closer to users.
Observability at the Gateway
The gateway is the ideal place to capture:
- Request rate, error rate, latency (P50, P95, P99) per route
- Rate limit hits per client/key
- Authentication failures (useful for detecting credential stuffing)
- Upstream service health and timeout rates
Export to Datadog, Prometheus, or CloudWatch. Set alerts on error rate spikes and P99 latency before users report problems.
FAQ
Do I need an API gateway for a small application?
Not necessarily. A single-service application with one client doesn't need a gateway. Add one when you have multiple services, multiple client types, or when cross-cutting concerns (auth, rate limiting) are being duplicated across services.
Should the gateway handle business logic?
No. Gateways handle infrastructure concerns. Business logic belongs in services. A gateway that makes business decisions becomes a hidden dependency that's difficult to test and reason about.
How do I handle gateway downtime?
Deploy at least two gateway instances behind a load balancer. Managed solutions (AWS API Gateway, Cloudflare Workers) handle availability for you. For self-hosted solutions, health checks and automatic failover are non-negotiable.
What's the difference between an API gateway and a service mesh?
A gateway handles north-south traffic (external clients to services). A service mesh handles east-west traffic (service-to-service). They're complementary — many production systems use both: a gateway at the perimeter and a mesh for internal communication.
Building an API that needs to scale?
Open Door Digital designs backend architectures with the right patterns for your traffic, team, and roadmap — not template solutions.
Discuss Your Backend Architecture