Sub topic 6
Shadow deployment: testing on live traffic with no user impact
Assume you can serve a model behind an HTTP or RPC endpoint and understand basic latency, error-rate, and prediction metrics. Shadow deployment, also called traffic mirroring, runs a candidate model on copies of real production requests while users continue receiving responses from the stable model.
Shadow deployment answers this question:
> How does the candidate behave on realistic production inputs and load?
It does not answer whether users prefer the candidate. Because the candidate's response is discarded, it cannot directly measure a business outcome such as purchases or clicks.
How shadow deployment works
The serving path sends the original request to the stable model. It returns that response to the user. At the same time, it sends a copy to the candidate model in a background task. The candidate result is logged for later comparison.
The candidate must not delay or change the stable response. Use a timeout, separate resource limits, and failure handling. Otherwise, a slow candidate can make the production service slow even though users do not see its predictions.
| Property | Stable model | Candidate model |
|---|---|---|
| Response returned to the user | Yes | No |
| Production traffic | Original request | Mirrored copy |
| Failure affects the user | Normally yes | Should not |
| Main evidence | Current service behavior | Compatibility, performance, and prediction differences |
A mirrored request can contain personal or confidential data. Apply the same access controls, retention rules, and encryption used for production data. Remove unnecessary fields before logging. Do not log raw requests or predictions unless there is a clear operational need.
Logging paired predictions
Log enough information to compare both models later:
- A request or correlation ID
- Model version for each prediction
- Input schema version
- Prediction or ranked output
- Prediction timestamp and latency
- Whether the candidate timed out or failed
- A later outcome, when a ground-truth label becomes available
The following example returns the stable result immediately. It runs the candidate with a 500-millisecond limit and records only the comparison.
import asyncio
import logging
logging.basicConfig(level=logging.INFO)
log = logging.getLogger("shadow")
background_tasks = set()
async def shadow(request, stable_response, candidate):
try:
candidate_response = await asyncio.wait_for(
candidate.predict(request), timeout=0.5
)
log.info(
"shadow_pair stable=%r candidate=%r",
stable_response,
candidate_response,
)
except Exception:
log.warning("candidate_failed", exc_info=True)
async def handle(request, stable, candidate):
stable_response = await stable.predict(request)
task = asyncio.create_task(shadow(request, stable_response, candidate))
background_tasks.add(task)
task.add_done_callback(background_tasks.discard)
return stable_responseIn a real service, also track candidate timeout rate, error rate, CPU and memory use, and queue depth. Compare candidate latency with the stable model, but do not treat a prediction difference as a defect by itself. A new model is expected to make some different predictions. Investigate differences by segment, input type, and eventual label.
When shadow deployment is useful
Use shadowing before a canary when the main risks are unexpected inputs, incompatible schemas, resource usage, or serving failures. It is especially useful for a model that has not yet handled live traffic.
Shadowing has limits:
- Candidate predictions may not receive labels if they are not shown to users.
- The candidate may not exercise downstream behavior such as ranking clicks or accepting a payment.
- Mirroring all traffic can double compute cost.
- Requests with side effects must never be replayed blindly. Mirror a read-only representation instead.
Exercise
The candidate model has a 2% timeout rate in shadow mode, while the stable model has a 0.1% timeout rate. Should it receive user traffic immediately?
Show answer
No. Investigate capacity, input handling, and timeout causes first. Shadow mode has shown a production-serving risk without exposing users to it.