Implementing Request Routing
1. Configuring Static Routing
| Property | Value |
|---|---|
| Match | Fixed path/host |
| Target | Single upstream |
| Performance | O(1) lookup |
| Use case | Stable, low-change endpoints |
Example: Static route
routes:
- paths: ["/health"]
service: health-svc
- paths: ["/metrics"]
service: metrics-svc
2. Using Path-Based Routing
Example: Path prefix → microservices
routes:
- paths: ["/users"] service: users-svc
- paths: ["/orders"] service: orders-svc
- paths: ["/billing"] service: billing-svc
| Pattern | Example | Best For |
|---|---|---|
| Prefix | /api/v1/* | Domain split |
| Exact | /login | Auth endpoints |
| Regex | ~ ^/u/\d+ | Dynamic IDs |
3. Using Header-Based Routing
Example: Route by tenant header
routes:
- paths: ["/api"]
headers:
X-Tenant-Tier: ["premium"]
service: premium-cluster
- paths: ["/api"]
service: standard-cluster
4. Using Query Parameter Routing
| Param | Match | Target |
|---|---|---|
?beta=true | Exact | Beta backend |
?region=eu | Exact | EU cluster |
?debug=1 | Present | Debug pool |
5. Implementing Host-Based Routing
Example: Multi-tenant by subdomain
routes:
- hosts: ["acme.app.com"] service: acme-svc
- hosts: ["globex.app.com"] service: globex-svc
- hosts: ["*.app.com"] service: default-svc
6. Using Content-Type Routing
| Content-Type | Route To |
|---|---|
application/json | REST backend |
application/grpc | gRPC backend |
application/soap+xml | Legacy SOAP service |
multipart/form-data | Upload service |
7. Implementing Dynamic Routing
Example: Lua plugin dynamic route (Kong)
local jwt = require "kong.plugins.jwt.jwt_parser"
local token = kong.request.get_header("authorization"):sub(8)
local payload = jwt:new(token):get_payload()
local shard = payload.tenant_shard or "default"
kong.service.set_target("shard-" .. shard .. ".internal", 8080)
| Source | Example |
|---|---|
| JWT claim | Tenant ID → shard |
| Header | X-User-Region → DC |
| Body field | RPC method name → backend |
8. Configuring Weight-Based Routing
Example: 90/10 split for gradual rollout
upstreams:
- name: checkout-pool
targets:
- target: checkout-stable:8080
weight: 90
- target: checkout-new:8080
weight: 10
9. Setting Up Canary Routing
| Phase | Weight | Duration |
|---|---|---|
| Internal | 0% (header only) | 1 day |
| Canary 1% | 1% | 1 hour |
| Canary 10% | 10% | 2 hours |
| Canary 50% | 50% | 4 hours |
| Full rollout | 100% | — |
10. Implementing Geographic Routing
Example: GeoIP-based routing
geoip2 /etc/geoip2/GeoLite2-Country.mmdb {
$geoip_country_code source=$remote_addr country iso_code;
}
map $geoip_country_code $backend {
default us-cluster.internal;
~^(DE|FR|IT|ES) eu-cluster.internal;
~^(JP|KR|SG) apac-cluster.internal;
}
location / { proxy_pass http://$backend; }