Architecture
Shadow Traffic Testing: Try New Code on Real Requests Safely
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيStaging environments and synthetic load tests never quite match production. Real users send odd headers, rare query combinations and traffic patterns nobody thought to script. Shadow traffic testing, also called traffic mirroring or a dark launch, closes that gap: you copy live requests to a new version of your service, let it process them, and throw its responses away. Users only ever see responses from the current version, while you watch how the new one behaves under real load. This article explains how it works, where it fits, and how to set it up with Nginx on a VPS.
How shadow traffic works
- A request arrives at your proxy or gateway.
- The proxy sends it to the primary (current) service and returns that response to the user, as usual.
- In parallel, the proxy sends a copy to the shadow (candidate) service.
- The shadow's response is discarded, but you record its status codes, latency, errors and resource use, and optionally compare its output with the primary's.
Because the user path does not depend on the shadow, a crash or slowdown in the new version should not affect anyone, provided the mirror is configured carefully.
What it is good for
- Performance validation: does the new version handle real traffic with similar CPU, memory and latency?
- Rewrites and migrations: moving from one language, framework or database to another and checking that outputs match.
- Catching rare errors: requests that never appear in your test suite.
- Capacity planning: seeing how a new instance size behaves under the exact traffic shape you serve.
Shadow vs canary vs blue-green
| Approach | Do users see the new version? | Main risk | Best for |
|---|---|---|---|
| Shadow traffic | No | Side effects of duplicated requests | Validating behavior and performance |
| Canary release | Yes, a small percentage | Some users hit bugs | Gradual rollout with real feedback |
| Blue-green | Yes, all at switch time | Bad switch affects everyone until rollback | Fast cutover and rollback |
Shadowing usually comes first: mirror to gain confidence, then release as a canary, then complete the rollout.
The big danger: side effects
A mirrored request is a real request. If the shadow service writes to the same database, charges a card, sends an email or pushes a notification, you just did it twice. Rules to follow:
- Mirror read-only traffic first:
GETandHEADrequests are the safe starting point. - Give the shadow its own data stores: a separate database copy or a restored snapshot, never production's write path.
- Stub external side effects: payment gateways, email, SMS and webhooks should point to sandboxes or no-op stubs in the shadow environment.
- Mark shadow requests with a header so downstream services and logs can recognize them.
- Watch privacy: mirrored traffic contains real user data, so the shadow environment needs the same access controls and retention rules as production.
Mirroring with Nginx
Nginx's built-in mirror module can copy requests to a second upstream:
upstream app_primary { server 127.0.0.1:8080; }
upstream app_shadow { server 10.0.0.20:8080; }
server {
listen 443 ssl http2;
server_name app.example.com;
# ssl_certificate lines here
location / {
mirror /_shadow;
mirror_request_body on;
proxy_pass http://app_primary;
}
location = /_shadow {
internal;
if ($request_method !~ ^(GET|HEAD)$) { return 204; }
proxy_pass http://app_shadow$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Shadow-Request 1;
proxy_connect_timeout 1s;
proxy_read_timeout 2s;
}
}Important details:
- The
internallocation cannot be called from outside. - The method check keeps writes from reaching the shadow. Remove it only when the shadow is fully isolated.
- Keep shadow timeouts short. Nginx processes mirror subrequests alongside the main request, and a slow or unreachable shadow can delay the handling of that connection. Short timeouts and a shadow placed close to the proxy limit the impact.
- The shadow must have enough capacity for the mirrored volume, or it will produce misleading slowdowns. You can mirror a fraction of traffic with
split_clientsto choose which requests are mirrored.
Test with sudo nginx -t, reload, and check the shadow's access log to confirm copies arrive.
For service meshes, Envoy and Istio support request mirroring with a percentage setting, and some API gateways offer the same feature.
Comparing results
Collecting metrics from the shadow is the easy part. For deeper validation, log a request ID with both responses (for example $request_id passed as a header) and compare status codes and response bodies offline. Expect noise from timestamps, random IDs and ordering, and normalize those fields before diffing. Pair this with chaos engineering experiments on the shadow to see how the candidate handles failures with realistic traffic.
Where to run the shadow
Run the shadow on a separate VPS of the same size you plan to use in production so CPU and memory numbers are meaningful, and in the same region as the primary to keep mirror latency low. A snapshot of the primary is a quick way to build the shadow server, and you can delete it once the test window ends; compare sizes on /pricing.
FAQ
Does shadow traffic slow down my real users?
It should not if configured well, since users only receive the primary's response. In practice the proxy does extra work, and a slow shadow can delay connection handling, so keep shadow timeouts short and monitor primary latency during the test.
Can I mirror POST requests?
Only when the shadow is fully isolated: its own database, and stubbed payments, email and other external calls. Otherwise mirrored writes will create duplicate side effects.
How long should a shadow test run?
Long enough to cover your normal traffic cycle, often at least a full day including peak hours, so the candidate sees both quiet periods and peak load.