# Shadow Traffic: Mirror Live Requests to Gen 2 | gen2.dev

> Shadow traffic sends a copy of live requests to Gen 2 and discards or compares the responses. How to mirror requests, handle writes and filter noise.

Source: https://gen2.dev/guide/shadow-traffic/

---

Field Guide

# Shadow traffic: show Gen 2 real requests before it answers any

Send Gen 2 a copy of every live request, throw its answers away or compare them, and make sure it cannot touch anything real while it learns.

![Slide: live requests are mirrored. Gen 1 answers the user, Gen 2 gets a copy, its answer is discarded and it causes no real side effects.](https://gen2.dev/img/shadow-traffic.png)

A test suite sends Gen 2 the requests somebody thought of. Production sends it everything else: the malformed ones, the enormous ones, the ones from a client nobody has updated in years. Shadow traffic is the way to show Gen 2 that second set before any user depends on its answers.

The idea is simple. Every live request still goes to Gen 1, and Gen 1's response still goes back to the user. A copy of the request also goes to Gen 2. Whatever Gen 2 sends back is thrown away, or kept and compared. The Istio documentation puts it in one line: "Mirroring sends a copy of live traffic to a mirrored service."

## How it differs from a parallel run and a dark launch

The three terms overlap, and people use them loosely. The useful distinction is where the copy is made and what is done with the result.

Technique

Where the copy is made

What happens to Gen 2's answer

Shadow traffic

Usually in the network, outside both applications

Discarded, or compared by a separate tool

[Parallel run](https://gen2.dev/guide/parallel-run/)

Anywhere, including batch inputs and nightly files

Compared, and every difference explained

Dark launch

In the application, triggered by real user activity

Not shown to the user

Martin Fowler describes a dark launch as calling new back-end behavior from existing users "without the users being able to tell it's being called." It is a decision made in application code. Shadow traffic is usually a decision made in the infrastructure: neither Gen 1 nor the caller knows it is happening.

A parallel run is defined by its purpose, which is proving that the answers match. Shadow traffic is one way to feed a parallel run. It is also useful with no comparison at all, as a way to find out whether Gen 2 stays up, how fast it is, and what it logs as an error.

## Where the copy is made

### At a proxy or load balancer

nginx has a module for it. The documentation says that it "implements mirroring of an original request by creating background mirror subrequests," and that "Responses to mirror subrequests are ignored." The module appeared in version 1.13.4. The example from the documentation is short:

```nginx
location / {
    mirror /mirror;
    proxy_pass http://backend;
}

location = /mirror {
    internal;
    proxy_pass http://test_backend$request_uri;
}
```

Here `backend` is Gen 1 and `test_backend` is Gen 2. The request body is mirrored by default, and the `mirror_request_body` directive turns that off.

### In a service mesh

Envoy calls the feature shadowing. Its documentation says: "The router is capable of shadowing traffic from one cluster to another." Envoy does not wait for the shadow cluster to respond before it returns the response from the primary one. A `runtime_fraction` setting limits the share of requests that are mirrored. If it is not set, every request is.

Istio offers the same thing under the name mirroring. Its documentation says: "The mirrored traffic happens out of band of the critical request path for the primary service." The mirrored requests are described as "fire and forget," which means the responses are discarded. A `mirrorPercentage` field mirrors a fraction of the traffic. If the field is absent, all of it is mirrored.

Both add a marker. The Host or Authority header of a mirrored request has `-shadow` appended, so Gen 2 can tell a copy from the real thing. Envoy can also apply header mutations to each mirrored request.

### At the packet level

Amazon VPC Traffic Mirroring copies network traffic from an elastic network interface and sends it to another target. The AWS documentation describes its purpose as feeding "out-of-band security and monitoring appliances" for content inspection, threat monitoring and troubleshooting. That is a different job. It copies packets, not requests, and it is aimed at inspection tools, not at a second copy of the application. Check which of the two a product means before planning around the word "mirroring."

### In a comparing proxy

Twitter's Diffy sits in front of the services under test. Its README says: "Diffy behaves as a proxy and multicasts whatever requests it receives to each of the running instances." It then compares the responses. Diffy does not capture traffic itself; it accepts requests from whatever source it is given. The repository is archived and no longer maintained.

### In application code

GitHub's Scientist library makes the copy inside the application. The existing code and the new code both run, the existing result is returned, and the two are compared. This is the most precise option, because the comparison point can be a single function. It is also the only one that requires changing Gen 1.

## Writes and side effects

A mirrored GET is harmless. A mirrored POST is a second order, a second charge or a second email, unless something stops it. Mirroring at the network level makes this easy to miss, because nobody wrote code that says "do it twice."

The tools take the problem seriously. Diffy's README says: "For safety reasons `POST`, `PUT`, `DELETE` are ignored by default." A flag has to be set to allow them. The Scientist README warns that the library "is only safe for wrapping methods that aren't changing data," and the GitHub announcement says: "we only use Scientist on read operations."

There are four defenses. Most teams need more than one.

-   **Mirror only the safe requests.** Start with reads. This is the Diffy default and the Scientist rule.
-   **Give Gen 2 its own state.** A separate database, queue and cache, so a mirrored write lands somewhere that does not matter. Gen 2's data then has to start in step with Gen 1's, which is the subject of [dual write and data migration](https://gen2.dev/guide/dual-write/).
-   **Stub everything that leaves the building.** Email, text messages, payment providers, partner APIs, webhooks. The shadow environment should have no working credentials for any of them. A missing credential is a stronger guarantee than a configuration flag.
-   **Make Gen 2 recognize a copy.** The `-shadow` suffix exists for this. Gen 2 can refuse to act on a request that carries it.

Then test the defenses. Send a mirrored request that would trigger an email and confirm that no email leaves.

## What to compare, and what to ignore

With plain mirroring the responses are discarded, so the first things to watch are Gen 2's own numbers: error rate, latency, resource use, and the exceptions in its logs. That alone finds crashes, timeouts and inputs nobody planned for.

Comparing answers takes more work. Something has to hold both responses side by side: a comparing proxy, code in the application, or logs from both systems joined on a request identifier.

Compare the status code and the fields that carry meaning. Expect noise. Timestamps, generated identifiers and random values differ on every call and mean nothing. Diffy's answer is a second copy of the old code. It runs a primary and a secondary instance of the known-good version and a candidate with the new code. The README defines noise as what is "observed between the primary and secondary instances," and differences in the candidate are judged against that baseline.

One kind of difference is specific to shadow traffic. If Gen 2 has its own data store and mirrored writes are blocked or stubbed, its data drifts away from Gen 1's. After a while the two systems are answering the same question about different data, and the mismatches say nothing about the code. Refresh Gen 2's data on a schedule, or compare only requests whose answer does not depend on recent writes.

## Load and cost

Mirroring everything doubles the request volume that the fleet as a whole has to serve. Gen 2 needs enough capacity to take it, and so does anything Gen 2 calls. If Gen 2 reads from a database or a service that Gen 1 also uses, the shadow load lands on production.

Mirroring is not free on the Gen 1 side either. In nginx, mirroring the request body means the body is read before the mirror subrequests are created, and the documentation notes that unbuffered proxying of the request body is disabled in that case. The GitHub announcement is direct about the in-application version: "You should also be mindful that you take a performance hit using Scientist in production."

So ramp up. Use the percentage controls: `runtime_fraction` in Envoy, `mirrorPercentage` in Istio, or an experiment enabled for a share of calls in Scientist. Start small, watch both systems, and increase.

Full mirroring is also the cheapest load test available. It shows whether Gen 2 can carry production volume with the real mix of requests, which is the question a dark launch asks.

## When it fits, and when it does not

Shadow traffic fits when:

-   The work arrives as requests and most of them are reads.
-   There is a proxy, load balancer or mesh that every request already passes through. This is the same router a [strangler fig](https://gen2.dev/guide/strangler-fig/) needs.
-   Gen 2 is meant to give the same answers as Gen 1.
-   The real mix of production requests is hard to reproduce in a test.

It is a poor fit when:

-   Most requests change data and Gen 2 cannot be given its own state.
-   The work is batch, files or scheduled jobs. A classic [parallel run](https://gen2.dev/guide/parallel-run/) on the same inputs suits that better.
-   Requests carry personal or regulated data and the shadow environment is not held to the same standard as production.
-   Gen 2 is supposed to behave differently, so there is nothing to compare against.

## Where it goes wrong

**A side effect escapes.** One endpoint that sends a receipt was not stubbed, and customers get two. This is the failure that ends shadow programs. Remove the credentials, do not rely on a flag.

**The shadow path slows the real one.** The mirror shares a connection pool, a thread pool or a database with production. When Gen 2 struggles, Gen 1 does too. Give the shadow its own resources and its own timeouts.

**Nobody reads the output.** Mirroring is switched on, Gen 2 logs errors for a month, and no one is assigned to look. A dashboard is not a process.

**The data drifts.** Mismatches pile up because the stores have diverged, the report becomes unreadable, and people stop trusting it.

**Passing is mistaken for proof.** Shadow traffic shows that Gen 2 handles requests. It does not exercise the writes that were blocked, the emails that were stubbed, or the month-end job. Those need their own tests.

**It never ends.** The mirror and the second fleet are a standing cost. Set the exit condition first, as on any parallel run.

## Checklist

-   Gen 1's response is the only one a user ever receives.
-   A slow or failed Gen 2 cannot delay or break a live request.
-   Writes are excluded, or Gen 2 writes only to its own state.
-   The shadow environment has no working credentials for email, payments or partners.
-   Mirrored requests are marked, and Gen 2 can recognize the mark.
-   The share of traffic mirrored is a setting, and it started small.
-   Someone owns the errors and the mismatches, and reviews them on a schedule.
-   The exit condition and the removal of the mirror are both in the plan.

Shadow traffic is how Gen 2 meets production without production meeting Gen 2. The comparison discipline is covered in [parallel run](https://gen2.dev/guide/parallel-run/), the cost of keeping a second fleet alive in [running two systems](https://gen2.dev/guide/running-two-systems/), and the step it prepares for in [the cutover](https://gen2.dev/guide/cutover/).

## Sources

1.  [Istio documentation - Mirroring](https://istio.io/latest/docs/tasks/traffic-management/mirroring/) - the definition, out-of-band delivery, discarded responses, the -shadow suffix and mirrorPercentage
2.  [Envoy documentation - route components (RequestMirrorPolicy)](https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/route/v3/route_components.proto) - shadowing does not wait for the shadow cluster; runtime\_fraction; header mutations on mirrored requests
3.  [nginx documentation - Module ngx\_http\_mirror\_module](https://nginx.org/en/docs/http/ngx_http_mirror_module.html) - the mirror and mirror\_request\_body directives and the example configuration
4.  [Amazon VPC documentation - What is Traffic Mirroring?](https://docs.aws.amazon.com/vpc/latest/mirroring/what-is-traffic-mirroring.html) - packet-level mirroring for security and monitoring appliances, and how it is charged
5.  [twitter-archive/diffy - README](https://github.com/twitter-archive/diffy) - a comparing proxy, the three instances, and POST, PUT and DELETE ignored by default
6.  [Abel Avram - InfoQ news report on Twitter open sourcing Diffy (September 18, 2015)](https://www.infoq.com/news/2015/09/twitter-diffy)
7.  [github/scientist - README](https://github.com/github/scientist) - comparison inside application code, and the warning about code that changes data
8.  [Jesse Toth - Scientist: Measure Twice, Cut Once (GitHub Blog, February 3, 2016)](https://github.blog/developer-skills/application-development/scientist/) - read operations only, the performance cost, and enabling an experiment for a percentage of calls
9.  [Martin Fowler - Dark Launching (April 29, 2020)](https://martinfowler.com/bliki/DarkLaunching.html)

Last updated November 28, 2026.

## Field Guide

-   [Rewrite vs refactor](https://gen2.dev/guide/rewrite-vs-refactor/)
-   [The 7 Rs](https://gen2.dev/guide/seven-rs/)
-   [Characterization tests](https://gen2.dev/guide/characterization-tests/)
-   [Estimating a rewrite](https://gen2.dev/guide/estimating-a-rewrite/)
-   [Strangler fig pattern](https://gen2.dev/guide/strangler-fig/)
-   [Monolith to microservices](https://gen2.dev/guide/monolith-to-microservices/)
-   [Lift and shift](https://gen2.dev/guide/lift-and-shift/)
-   [Second-system effect](https://gen2.dev/guide/second-system-effect/)
-   [Branch by abstraction](https://gen2.dev/guide/branch-by-abstraction/)
-   [Anti-corruption layer](https://gen2.dev/guide/anti-corruption-layer/)
-   [Parallel run](https://gen2.dev/guide/parallel-run/)
-   [Shadow traffic](https://gen2.dev/guide/shadow-traffic/)
-   [Dual write and data migration](https://gen2.dev/guide/dual-write/)
-   [Expand and contract](https://gen2.dev/guide/expand-and-contract/)
-   [Data migration tools](https://gen2.dev/guide/data-migration-tools/)
-   [The cutover](https://gen2.dev/guide/cutover/)
-   [Feature flags and dark launches](https://gen2.dev/guide/feature-flags-and-dark-launches/)
-   [Big-bang cutover](https://gen2.dev/guide/big-bang-cutover/)
-   [API versioning](https://gen2.dev/guide/api-versioning/)
-   [Running two systems](https://gen2.dev/guide/running-two-systems/)
-   [Killing Gen 1](https://gen2.dev/guide/killing-gen-1/)
-   [Rewriting a mobile app](https://gen2.dev/guide/rewriting-a-mobile-app/)
-   [Selling a rewrite](https://gen2.dev/guide/selling-a-rewrite/)
-   [AI-written code](https://gen2.dev/guide/ai-written-code/)

## Should we rewrite it?

Sixteen questions about your system. A recommendation, and the reasoning behind it.

[Start the questions](https://gen2.dev/should-we-rewrite/)
