Lesson 8 of 9 · 28m
Testing and Migration Patterns Behind Flags
Use contract tests, dual-write reconciliation, backfill discipline, and path pinning to migrate safely without freezing the product.
The hard part of a migration is rarely the new code. It is the seam: old rows, in-flight jobs, retry behavior, downstream consumers, rollbacks, and the fact that production does not pause while you improve it. Feature flags help only when the migration pattern is explicit. A boolean wrapped around two incompatible implementations is not a migration strategy.
Test behavior, not branch existence
Do not write tests that merely assert flag on returns new service. Test the contract both implementations must satisfy:
@pytest.mark.parametrize("payment_path", ["legacy_gateway", "orchestrator"])
def test_capture_requires_authorized_order(api, payment_path):
order = create_order(payment_path=payment_path)
response = api.post(f"/orders/{order.id}/capture")
assert response.status_code == 409Dual-write is a reconciliation system
Writing to two stores is not automatically safer. It is safer only if divergence is detectable and repairable:
1. write authoritative legacy record
2. enqueue projection to new store
3. record migration status and checksum
4. reconcile counts and sampled fields
5. alert on divergence or lagDecide which side is authoritative during each phase. If both sides can be mutated independently, dual-write becomes dual-truth, which is worse than either implementation alone.
Pin workflow decisions
Earlier lessons covered evaluation timing; migration makes the stakes concrete. Store the implementation path on durable entities and jobs:
{
"job_id": "9d7d...", "type": "invoice.generate_pdf",
"renderer": "weasyprint-v2", "attempt": 3
}Backfills need their own controls
A backfill is production traffic with unusual shape. Rate-limit it, checkpoint it, make it idempotent, and expose progress:
python -m billing.backfill_invoice_paths --start-after inv_000000 --batch-size 500 --max-rps 25 --checkpoint-file /run/backfill/invoice-paths.jsonDo not bury backfill behavior behind the same user-facing flag. The flag controls serving path; the migration job controls data convergence. They are related but not interchangeable.
Rehearse rollback at the system boundary
Can the old implementation read rows written by the new one? Can downstream consumers handle both event versions? Can a disabled flag stop new writes while old writes drain? If the answer is no, rollback is not binary. Document it as forward-only, dual-compatible, or reverse-migration-required.
Remove the scaffolding while you still understand it
The best time to delete the legacy branch is immediately after stability criteria are met, while the people who understand the edge cases are still emotionally scarred and employed. Waiting six months turns cleanup into archaeology.
Field notes
- Never assume retries are harmless; keep legacy compatibility tests until the legacy path is deleted, not merely disabled.
Was this lesson helpful?