Traffic Mirroring
Traffic mirroring (also known as shadow traffic) allows the API Gateway to duplicate incoming requests and send copies to a secondary service without affecting the original request-response flow. This is useful for testing new service versions, validating behavior under production traffic, or populating caches in a new environment.
Mirror Configuration
MirrorConfig Properties
| Property | Type | Default | Description |
|---|---|---|---|
mirrorHost | String | required | Hostname of the mirror target |
mirrorPort | int | required | Port of the mirror target |
percentage | int | 100 | Percentage of traffic to mirror (0-100) |
synchronous | boolean | false | Whether to wait for the mirror response |
logResponses | boolean | true | Whether to log mirror target responses |
Configure Traffic Mirroring
Endpoint: POST /api/v1/gateway/routes/:routeName/mirror
curl -X POST http://localhost:8080/api/v1/gateway/routes/ai-service-route/mirror \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d '{
"mirrorHost": "ai-service-v2-staging.internal",
"mirrorPort": 8000,
"percentage": 50,
"synchronous": false,
"logResponses": true
}'This mirrors 50% of traffic hitting the ai-service-route to the staging instance of the v2 service.
How It Works
When configureTrafficMirroring is called, the service enables the traffic-mirror Kong plugin on the specified route with the provided configuration:
- The original request is processed normally and the response is returned to the client
- A copy of the request is sent to the mirror host asynchronously (by default)
- If
synchronousis set totrue, the gateway waits for the mirror response before returning - If
logResponsesis enabled, mirror responses are logged for comparison
Traffic Flow
Client Request
|
v
Kong Route (ai-service-route)
|
+---> Primary Backend (ai-service:8000) --> Response to Client
|
+---> Mirror Backend (ai-service-v2:8000) --> Response logged/discarded
(async, no impact on client)Use Cases
Pre-Production Validation
Mirror production traffic to a staging environment to validate that a new version handles real traffic correctly before promotion:
{
"mirrorHost": "ai-service-staging.internal",
"mirrorPort": 8000,
"percentage": 100,
"synchronous": false,
"logResponses": true
}Performance Benchmarking
Mirror a subset of traffic to a performance testing instance to measure how a new version handles load:
{
"mirrorHost": "ai-service-perf.internal",
"mirrorPort": 8000,
"percentage": 25,
"synchronous": false,
"logResponses": false
}Cache Warming
Mirror traffic to a new instance to warm its caches before it starts receiving direct traffic:
{
"mirrorHost": "ai-service-new-replica.internal",
"mirrorPort": 8000,
"percentage": 100,
"synchronous": false,
"logResponses": false
}Disabling Mirroring
To disable traffic mirroring, remove the mirror plugin from the route by disabling the associated plugin. Use the plugin management endpoints described in the Plugins page.
When synchronous is set to true, the mirror response time is added to the overall latency experienced by the client. Use synchronous mode only when response comparison is required and latency impact is acceptable.