tadata
Back to home

API Gateway Patterns: Routing, Security & the BFF Approach

#architecture#api#microservices#cloud#security

An API gateway is the front door of your microservice architecture. It handles cross-cutting concerns so that individual services do not have to. But gateway design decisions have long-lasting consequences on performance, coupling, and team autonomy.

Gateway Responsibilities Taxonomy

API Gateway Responsibilities
├── Traffic Management
│   ├── Request routing
│   ├── Load balancing
│   ├── Rate limiting / throttling
│   ├── Circuit breaking
│   └── Canary / blue-green routing
├── Security
│   ├── Authentication (JWT validation, OAuth2)
│   ├── Authorization (scope/role checking)
│   ├── TLS termination
│   ├── CORS management
│   └── IP whitelisting / WAF integration
├── Transformation
│   ├── Request/response mapping
│   ├── Protocol translation (REST↔gRPC)
│   ├── Header injection
│   └── Response aggregation
├── Observability
│   ├── Access logging
│   ├── Distributed tracing (trace ID injection)
│   ├── Metrics collection
│   └── Health check endpoints
└── Developer Experience
    ├── API documentation (OpenAPI serving)
    ├── Developer portal
    ├── API key management
    └── Usage analytics

Tool Comparison

FeatureKongEnvoyAWS API GatewayApigee (Google)Traefik
TypeFull gatewayProxy / data planeManaged serviceManaged platformEdge router
DeploymentSelf-hosted or cloudSidecar or edgeAWS-managedGCP-managedSelf-hosted or cloud
Protocol supportHTTP, gRPC, WebSocketHTTP/2, gRPC, TCPHTTP, WebSocket, RESTHTTP, gRPCHTTP, TCP, gRPC
Plugin ecosystemExtensive (Lua, Go)Filters (C++, Wasm)Lambda authorizersPolicies (extensive)Middlewares (Go)
Service mesh roleVia KumaIstio data planeN/AN/AVia Traefik Mesh
Best forAPI management at scaleHigh-performance proxyAWS-native workloadsEnterprise API programKubernetes ingress
Pricing modelOpen source + enterpriseOpen sourcePer request + dataPer API callOpen source + enterprise
Learning curveMediumHighLowMedium-HighLow-Medium

Backend-for-Frontend (BFF) Pattern

┌──────────┐  ┌──────────┐  ┌──────────┐
│  Mobile  │  │   Web    │  │  Partner │
│   App    │  │   SPA    │  │   API    │
└────┬─────┘  └────┬─────┘  └────┬─────┘
     │             │             │
┌────▼─────┐  ┌────▼─────┐  ┌────▼─────┐
│ Mobile   │  │  Web     │  │ Partner  │
│ BFF      │  │  BFF     │  │ BFF      │
│          │  │          │  │          │
│ - Compact│  │ - Rich   │  │ - Stable │
│   payload│  │   payload│  │   versioned│
│ - Offline│  │ - SSR    │  │   contract│
│   support│  │   support│  │ - Rate   │
│ - Push   │  │ - WebSock│  │   limited │
└────┬─────┘  └────┬─────┘  └────┬─────┘
     │             │             │
     └─────────────┼─────────────┘
                   │
          ┌────────▼────────┐
          │  Internal API   │
          │  Gateway / Mesh │
          └────────┬────────┘
                   │
     ┌─────────┬───┴───┬─────────┐
     │         │       │         │
  ┌──▼──┐  ┌──▼──┐ ┌──▼──┐  ┌──▼──┐
  │Svc A│  │Svc B│ │Svc C│  │Svc D│
  └─────┘  └─────┘ └─────┘  └─────┘

API Gateway vs Service Mesh

DimensionAPI GatewayService Mesh
PositionEdge (north-south traffic)Internal (east-west traffic)
Primary roleExternal API managementService-to-service communication
AuthenticationExternal identity (JWT, API key)mTLS between services
Rate limitingPer external clientPer service / per route
ObservabilityAPI-level metrics, access logsDistributed traces, L7 metrics
ProtocolHTTP/REST focusAny protocol (L4/L7)
DeploymentCentralized (1-few instances)Distributed (sidecar per pod)
Managed byPlatform / API teamPlatform team
ToolsKong, AWS APIGW, ApigeeIstio, Linkerd, Consul Connect
OverlapYes -- both can do routing, TLS, observability

Key insight: They are complementary, not competing. Use a gateway for external traffic and a mesh for internal traffic. Avoid duplicating policies across both layers.

Anti-Patterns

The God Gateway -- Putting business logic in the gateway (field transformations, complex orchestration). The gateway should route and enforce policies, not contain domain logic.

Shared gateway across teams -- When one team's gateway change breaks another team's API. Give teams ownership of their own gateway configuration or use a self-service model.

No gateway at all -- Exposing microservices directly means every service reimplements auth, rate limiting, and CORS. Cross-cutting concerns belong in infrastructure.

Resources