Comprehensive API gateway patterns skill covering Kong, routing, rate limiting, authentication, load balancing, traffic management, and production gateway architecture
Production-grade API gateway patterns using Kong and industry best practices for microservices architectures.
This skill provides comprehensive guidance on implementing, configuring, and operating API gateways in production environments. It covers routing strategies, authentication mechanisms, rate limiting, load balancing, caching, security, and observability patterns.
Docker:
docker run -d --name kong \
-e "KONG_DATABASE=off" \
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
-e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
-p 8000:8000 \
-p 8443:8443 \
-p 8001:8001 \
kong:latest
Docker Compose:
version: '3.8'
services:
kong:
image: kong:latest
environment:
KONG_DATABASE: "off"
KONG_PROXY_ACCESS_LOG: /dev/stdout
KONG_ADMIN_ACCESS_LOG: /dev/stdout
KONG_PROXY_ERROR_LOG: /dev/stderr
KONG_ADMIN_ERROR_LOG: /dev/stderr
KONG_ADMIN_LISTEN: 0.0.0.0:8001
KONG_DECLARATIVE_CONFIG: /etc/kong/kong.yaml
ports:
- "8000:8000" # Proxy
- "8443:8443" # Proxy SSL
- "8001:8001" # Admin API
volumes:
- ./kong.yaml:/etc/kong/kong.yaml
Kubernetes:
kubectl create -f https://bit.ly/k4k8s
Create a simple service and route:
# kong.yaml
_format_version: "3.0"
services:
- name: example-service
url: http://httpbin.org
routes:
- name: example-route
service: example-service
paths:
- /httpbin
Apply configuration:
# Using Admin API
curl -i -X POST http://localhost:8001/services \
--data name=example-service \
--data url=http://httpbin.org
curl -i -X POST http://localhost:8001/services/example-service/routes \
--data 'paths[]=/httpbin' \
--data name=example-route
# Using decK (declarative)
deck sync -s kong.yaml
Test the gateway:
curl http://localhost:8000/httpbin/get
Add API key authentication:
plugins:
- name: key-auth
route: example-route
config:
key_names:
- apikey
consumers:
- username: test-user
keyauth_credentials:
- consumer: test-user
key: my-secret-api-key
Apply and test:
deck sync -s kong.yaml
# Request without key (401)
curl http://localhost:8000/httpbin/get
# Request with key (200)
curl -H "apikey: my-secret-api-key" http://localhost:8000/httpbin/get
Protect your API with rate limits:
plugins:
- name: rate-limiting
route: example-route
config:
minute: 5
hour: 100
policy: local
Apply and test:
deck sync -s kong.yaml
# First 5 requests succeed
for i in {1..6}; do
curl -H "apikey: my-secret-api-key" http://localhost:8000/httpbin/get
done
# 6th request returns 429 Too Many Requests
Route requests to multiple backend services:
services:
- name: users-service
url: http://users-api:8001
- name: orders-service
url: http://orders-api:8002
- name: products-service
url: http://products-api:8003
routes:
- name: users-route
service: users-service
paths:
- /api/users
strip_path: true
- name: orders-route
service: orders-service
paths:
- /api/orders
strip_path: true
- name: products-route
service: products-service
paths:
- /api/products
strip_path: true
plugins:
- name: jwt
# Global authentication
- name: rate-limiting
# Global rate limiting
- name: prometheus
# Metrics collection
Support multiple API versions:
# Version 1 (stable)
services:
- name: api-v1
url: http://api-v1:8001
routes:
- name: api-v1-route
service: api-v1
paths:
- /v1
strip_path: true
# Version 2 (latest)
services:
- name: api-v2
url: http://api-v2:8002
routes:
- name: api-v2-route
service: api-v2
paths:
- /v2
strip_path: true
# Version 2 via header
routes:
- name: api-v2-header
service: api-v2
paths:
- /api
headers:
X-API-Version:
- "2"
Distribute load across multiple instances:
upstreams:
- name: api-upstream
algorithm: round-robin
healthchecks:
active:
type: http
http_path: /health
healthy:
interval: 5
successes: 2
unhealthy:
interval: 5
http_failures: 3
timeouts: 3
targets:
- upstream: api-upstream
target: api-1:8001
weight: 100
- upstream: api-upstream
target: api-2:8001
weight: 100
- upstream: api-upstream
target: api-3:8001
weight: 100
services:
- name: api-service
host: api-upstream
port: 80
protocol: http
routes:
- name: api-route
service: api-service
paths:
- /api
Complete security stack for public APIs:
services:
- name: public-api
url: https://api.internal:8001
protocol: https
routes:
- name: public-api-route
service: public-api
paths:
- /api
protocols:
- https
plugins:
# Authentication
- name: oauth2
route: public-api-route
config:
scopes:
- read
- write
enable_authorization_code: true
# Rate limiting (tiered)
- name: rate-limiting
route: public-api-route
config:
minute: 60
hour: 1000
# CORS
- name: cors
route: public-api-route
config:
origins:
- https://app.example.com
methods:
- GET
- POST
- PUT
- DELETE
credentials: true
# IP restriction (admin endpoints)
- name: ip-restriction
route: admin-route
config:
allow:
- 10.0.0.0/8
# Bot detection
- name: bot-detection
route: public-api-route
# Request validation
- name: request-validator
route: public-api-route
# Logging
- name: http-log
route: public-api-route
config:
http_endpoint: http://logstash:5000
# Monitoring
- name: prometheus
route: public-api-route
Gradually shift traffic to new version:
upstreams:
- name: api-upstream
algorithm: round-robin
targets:
# Stable version (90%)
- upstream: api-upstream
target: api-v1:8001
weight: 900
# Canary version (10%)
- upstream: api-upstream
target: api-v2:8002
weight: 100
# Monitor errors, latency in v2
# Gradually increase v2 weight: 100 → 300 → 500 → 900 → 1000
# Decrease v1 weight: 900 → 700 → 500 → 100 → 0
Isolate tenants with custom policies:
# Tenant routing
routes:
- name: tenant-a
hosts:
- tenant-a.api.example.com
service: tenant-a-service
- name: tenant-b
hosts:
- tenant-b.api.example.com
service: tenant-b-service
# Per-tenant rate limiting
consumers:
- username: tenant-a
custom_id: tenant-a-001
plugins:
- name: rate-limiting
consumer: tenant-a
config:
minute: 1000 # Premium tier
consumers:
- username: tenant-b
custom_id: tenant-b-002
plugins:
- name: rate-limiting
consumer: tenant-b
config:
minute: 100 # Free tier
# Add tenant context to requests
plugins:
- name: request-transformer
config:
add:
headers:
- "X-Tenant-ID:$(consumer_custom_id)"
Optimized for mobile apps:
services:
- name: mobile-api
url: http://mobile-backend:8001
routes:
- name: mobile-v1
service: mobile-api
paths:
- /mobile/v1
strip_path: true
plugins:
# Aggressive caching for mobile
- name: proxy-cache
route: mobile-v1
config:
strategy: redis
cache_ttl: 3600
vary_headers:
- X-App-Version
# Device-based rate limiting
- name: rate-limiting
route: mobile-v1
config:
limit_by: header
header_name: X-Device-ID
minute: 100
# Response compression
- name: response-transformer
route: mobile-v1
config:
add:
headers:
- "Content-Encoding:gzip"
# Request size limiting (protect uploads)
- name: request-size-limiting
route: mobile-upload
config:
allowed_payload_size: 10 # MB
Track requests across microservices:
plugins:
# Zipkin tracing
- name: zipkin
config:
http_endpoint: http://zipkin:9411/api/v2/spans
sample_ratio: 0.1 # Trace 10%
include_credential: true
# Or OpenTelemetry
- name: opentelemetry
config:
endpoint: http://jaeger:14268/api/traces
resource_attributes:
service.name: api-gateway
service.version: 1.0.0
batch_span_processor:
max_queue_size: 2048
# Propagate trace context
- name: request-transformer
config:
add:
headers:
- "X-Request-ID:$(uuid)"
- "X-Trace-ID:$(traceid)"
Gateway returns 502/503:
Authentication failing:
Rate limiting not working:
High latency:
Cache not working:
Enable Debug Logging:
# Set log level to debug
export KONG_LOG_LEVEL=debug
kong restart
Check Admin API:
# Verify service configuration
curl http://localhost:8001/services/my-service
# Check route configuration
curl http://localhost:8001/routes/my-route
# List enabled plugins
curl http://localhost:8001/plugins
Test Upstream Directly:
# Bypass gateway to test upstream
curl http://upstream-host:8001/endpoint
Review Logs:
# Proxy logs
tail -f /usr/local/kong/logs/access.log
# Error logs
tail -f /usr/local/kong/logs/error.log
Clients → Kong Gateway → Backend Services
Clients → DNS/GeoDNS → Regional Kong Clusters → Regional Services
Control Plane (Config Management)
↓
Data Plane Nodes (Traffic Handling)
↓
Backend Services
Clients → Kong (North-South) → Istio/Linkerd (East-West) → Services
Version: 1.0.0 Last Updated: October 2025 Maintainer: API Gateway Patterns Team
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer