The symptom and the explanations#
The example is illustrative. A fictional pricing service computes an average unit price for each order. Between 00:00 and 12:00 UTC it raised ZeroDivisionError on six orders. An engineer found an empty basket in one of the failing requests, added a guard for empty baskets, and shipped it at 12:00. There were no errors after 12:00, and the ticket was marked fixed.
- The fix works. Bad orders kept arriving after 12:00, and the guard handled them.
- The trigger stopped. The bad orders came from one partner's bulk importer, which was paused at 12:00 for the partner's own maintenance. The new build saw no bad orders, so it had nothing to fail on.
A third possibility hides behind the second: the fix handles only part of the trigger. Six failures might have had two different shapes, and the engineer looked at one. While the trigger is absent, this explanation and the first look the same. Choose the observation where explanations disagree
What each explanation predicts#
| Observation | The fix works | The trigger stopped |
|---|---|---|
| Errors after 12:00 | none | none |
| Triggering orders after 12:00 | some, none failed | none |
| Each captured failing order replayed on the old build | fails | fails |
| The same orders replayed on the new build | all pass | all pass if the fix is complete; some fail if it is partial |
The error graph is the first row, and it separates nothing. The second row separates the first two explanations, but only if you can recognize a triggering order in the logs, which means knowing the trigger precisely. The replay rows do not depend on whether the trigger came back. They also test the fix itself: a partial fix fails the replay of the shape it missed.
The old-build replay is the control. If a captured order passes on the old build, the replay is not reproducing the failure, and something else the failing request had, such as concurrency, state or configuration, is missing. The bug will not reproduce in staging
The lab: two worlds with a quiet afternoon#
The script builds a day of order traffic. The importer sends two kinds of bad order: an empty basket and a basket whose quantities are all zero. Both divide by zero in the original code. price_v2 guards the empty basket, which is the fix the engineer wrote. price_v3 guards a zero total quantity, which covers both. In the fix-works world, price_v3 ships at 12:00 and the importer keeps sending. In the trigger-stopped world, price_v2 ships at 12:00 and the importer pauses at the same moment. It needs Python 3.8 or later.
import random, sys
def price_v1(order): # the release that failed
items = order["items"]
return round(sum(i["price"] * i["qty"] for i in items) / sum(i["qty"] for i in items), 2)
def price_v2(order): # fix A: guard the empty basket
return 0.0 if not order["items"] else price_v1(order)
def price_v3(order): # fix B: guard the zero total quantity
return 0.0 if sum(i["qty"] for i in order["items"]) == 0 else price_v1(order)
WORLDS = { # build shipped at 12:00, does the importer keep sending?
"fix-works": (price_v3, True),
"trigger-stopped": (price_v2, False),
}
def traffic(importer_after_noon, rng):
for hour in range(24):
for _ in range(200):
yield hour, {"items": [{"price": 9.5, "qty": rng.randint(1, 3)}]}
if hour < 12 or importer_after_noon: # one partner's bulk importer
if rng.random() < 0.5:
yield hour, {"items": []}
if rng.random() < 0.3:
yield hour, {"items": [{"price": 4.0, "qty": 0}]}
def fails(fn, order):
try:
fn(order)
return False
except ZeroDivisionError:
return True
if len(sys.argv) < 2 or sys.argv[1] not in ("fix-works", "trigger-stopped"):
sys.exit("usage: python3 replay.py fix-works|trigger-stopped [seed]")
world = sys.argv[1]
shipped, importer = WORLDS[world]
log = list(traffic(importer, random.Random(int(sys.argv[2]) if len(sys.argv) > 2 else 3)))
before = [o for h, o in log if h < 12]
after = [o for h, o in log if h >= 12]
print(f"world={world}")
print(f" errors before 12:00 (v1) {sum(fails(price_v1, o) for o in before)}")
print(f" errors after 12:00 (shipped) {sum(fails(shipped, o) for o in after)}")
print(f" orders with zero quantity after {sum(sum(i['qty'] for i in o['items']) == 0 for o in after)}")
print(f" replay of the morning's failing orders against v1 and {shipped.__name__}:")
for o in {str(o): o for o in before if fails(price_v1, o)}.values():
old = "FAIL" if fails(price_v1, o) else "ok"
new = "FAIL" if fails(shipped, o) else "ok"
print(f" {str(o):40s} v1 {old:5s} {shipped.__name__} {new}")
Save it as replay.py. Before running it, write down for each world the number of errors after 12:00, the number of triggering orders after 12:00, and the replay result for each failing shape on the shipped build. The prediction is that both worlds show zero errors after 12:00, that only fix-works shows triggering orders after 12:00, and that the replay fails price_v2 on the zero-quantity order.
python3 replay.py fix-works
python3 replay.py trigger-stopped
Python 3.11 printed:
world=fix-works
errors before 12:00 (v1) 6
errors after 12:00 (shipped) 0
orders with zero quantity after 7
replay of the morning's failing orders against v1 and price_v3:
{'items': []} v1 FAIL price_v3 ok
{'items': [{'price': 4.0, 'qty': 0}]} v1 FAIL price_v3 ok
world=trigger-stopped
errors before 12:00 (v1) 6
errors after 12:00 (shipped) 0
orders with zero quantity after 0
replay of the morning's failing orders against v1 and price_v2:
{'items': []} v1 FAIL price_v2 ok
{'items': [{'price': 4.0, 'qty': 0}]} v1 FAIL price_v2 FAIL
The two error graphs are identical: six errors in the morning, none in the afternoon. In fix-works, seven triggering orders arrived after 12:00 and none failed, and the replay passed every captured shape. In trigger-stopped, no triggering order arrived, so the afternoon tested nothing, and the replay showed that the shipped fix still fails on one of the two shapes. When the importer resumes, the errors come back at a lower rate, and someone will call it a new incident.
The prediction would have failed if the replay had passed price_v2 on the zero-quantity order, or if either world had shown errors after 12:00. Pass a second argument to change the seed, for example python3 replay.py trigger-stopped 9; the counts change and the replay table does not, because it depends on which shapes of input failed in the morning, not on how many arrived.
Read it on a real system#
Start from the failing requests. Their request IDs or trace IDs lead to the inputs, and the inputs give you the trigger as a predicate you can count: here, an order whose quantities sum to zero, from the partner's client. Find the failing request in the server logs
Count requests that match the predicate before and after the fix. The input has to be in the logs, or at least a field that stands in for it, such as the client ID of the importer. CloudWatch Logs Insights filters on fields with comparisons and like, and stats count(*) by bin(1h) gives the count per hour. filter · stats
filter route = "/orders/price" and client_id = "partner-import" and qty_total = 0
| stats count(*) as triggering by bin(1h)
On Google Cloud, the same filter goes in a Logging query on jsonPayload fields with gcloud logging read. Logging query language · gcloud logging read
For the replay, check out the old and new commits side by side. git worktree add creates a separate working tree at a path with the given commit checked out, so both builds can run at once. git worktree
git worktree add --detach ../pricing-old OLD_COMMIT
git worktree add --detach ../pricing-new NEW_COMMIT
Run each build locally or in a staging environment, and send every captured failing input to both. Send them to a copy you control, never to production: a replayed write is a real write, and the captured inputs may hold customer data that must stay where it was collected. Keep one input per distinct shape, as the lab does, and make each one a regression test once the replay shows old-fails and new-passes.
Where the check misleads#
A replay reproduces only what you captured. If the failing requests were logged without their bodies, the replay set is a guess at the trigger, and a fix can pass the guess while the real input still fails. Prefer the exact captured inputs, with personal data replaced in ways that keep the failing property.
Some failures need more than the input: two requests at once, a cache in a particular state, a configuration value production has and staging lacks. The old build then passes the replay, which at least tells you the replay is incomplete.
Counting triggering inputs after the fix needs a precise trigger. A predicate that is too broad counts harmless requests as tests; one that is too narrow misses the shape the fix did not handle. When the trigger is clear and keeps arriving, the count since the fix also tells you how long to keep watching. How long to watch after a fix
Questions#
How do I prove a bug fix worked in production?
Show two things: inputs that used to fail kept arriving after the fix and did not fail, and each captured failing input fails on the old build and passes on the new one in a test environment. Zero errors since the fix is consistent with the trigger having stopped.
Why replay on the old build if I already know it failed?
Because a replay that passes on the old build is not reproducing the failure. Only an old-build failure shows that the replay contains the trigger, and so that a new-build pass means something.
The failing input came from a customer. Can I replay it?
Replay it in a test environment, not against production, and handle the data as the original request was handled. If the input contains personal data, replace those values with ones that keep the failing property, such as the same length, encoding or zero quantity, and check that the old build still fails on the edited input.