Resilience & Service PatternsAPI Gateway PatternsMedium⏱️ ~2 min

Gateway Routing and Traffic Shaping Patterns

Path Based Routing

The simplest routing pattern. Requests to /api/users/* route to the user service; /api/orders/* routes to the order service. Configuration is straightforward but rigid. Adding a new service requires gateway configuration changes and redeployment.

Header Based Routing

Route based on request headers like X-API-Version: 2 or X-Tenant-ID: acme. Enables API versioning without path changes and multi tenant routing to isolated backends. The gateway inspects headers before routing decisions, adding 0.1-0.5ms processing time.

Canary and Blue Green Routing

Split traffic between service versions for safe deployments. Canary sends 5% of traffic to the new version while 95% goes to stable. Blue green maintains two full environments and switches all traffic instantly. The gateway manages traffic weights and can roll back in seconds by changing routing rules without redeploying services.

Rate Limiting Patterns

Control request rates at the gateway before requests reach backends. Fixed window: Allow 100 requests per minute, reset at minute boundary. Simple but allows bursts at window edges. Sliding window: Smooth rate limiting without edge bursts. Token bucket: Allow bursts up to bucket size while maintaining average rate. Apply limits per user, per IP, per API key, or globally.

⚠️ Key Trade-off: Sophisticated routing adds gateway complexity and latency. Path based routing adds near zero latency; content based routing requiring request body inspection can add 5-10ms.

Traffic Shaping

Beyond rate limiting, gateways can shape traffic patterns. Request queuing: Buffer bursts instead of rejecting. Priority queues: Process premium tier requests before free tier. Circuit breaking: Stop sending requests to failing backends. These patterns protect backend services from being overwhelmed during traffic spikes.

💡 Key Takeaways
Path based routing is simplest but requires gateway redeployment for changes; header based enables versioning and multi tenancy
Canary (5%/95% split) and blue green (instant switch) deployments are managed by gateway routing rules, enabling rollback in seconds
Rate limiting patterns: fixed window (simple, edge bursts), sliding window (smooth), token bucket (allows controlled bursts)
📌 Interview Tips
1When discussing deployments, explain how canary routing at the gateway enables safe rollouts with instant rollback
2Mention rate limiting tiers: per user, per IP, per API key, and global limits for different protection layers
3Note that content based routing (inspecting body) adds 5-10ms latency vs near zero for path based
← Back to API Gateway Patterns Overview