Sub topic 14
Rolling Out Gradually and Removing Old Code
This chapter assumes you can use Git branches, run a test suite, and read basic Python. It also assumes you have already split a change into small, verified steps. Merging a verified change is not the last step. The new code still has to run safely in production, and the old code has to go away.
Why roll out in stages
Tests catch the differences you thought of. Production traffic contains inputs you did not think of. A gradual rollout limits the damage when the new code misbehaves, and it lets you switch back without a redeploy.
A common sequence has four stages:
- Add the new implementation next to the old one, with the old one still in use.
- Run both and compare results, but keep returning the old result (shadow mode).
- Switch to the new implementation, first for a small share of traffic and then for all of it.
- Delete the old implementation and the switch.
A switch with a shadow mode
The example below reuses calculate_tax from the earlier refactoring. An environment variable selects the mode. In shadow mode both versions run, a mismatch is logged, and the old result is returned.
import logging
import os
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("rollout")
TAX_RATE = 0.2
def old_tax(subtotal):
return round(subtotal * 0.2, 2)
def new_tax(subtotal):
return round(subtotal * TAX_RATE, 2)
def calculate_tax(subtotal):
mode = os.environ.get("TAX_IMPL", "old")
if mode == "new":
return new_tax(subtotal)
if mode == "shadow":
old, new = old_tax(subtotal), new_tax(subtotal)
if old != new:
logger.warning("tax mismatch for %r: old=%r new=%r", subtotal, old, new)
return old
return old_tax(subtotal)
if __name__ == "__main__":
print(calculate_tax(100.0))Run it with TAX_IMPL=shadow python pricing.py. Silence in the logs over a realistic period is your evidence to move on. If your system has a feature-flag service, use it in place of the environment variable. The flag service can change values without a restart and can target a percentage of users.
What the agent does well here
Ask the agent to add the switch, the shadow comparison, and tests for each mode. These are small, well-defined edits that you can verify. Keep the request scoped, as in earlier chapters. Do not ask it to flip the flag in a deployed environment. That action is yours, because it changes live behavior.
Removing the old code
Cleanup is where migrations tend to stall, because nothing forces it. Schedule it as its own task once the new path has run at 100% for an agreed period.
Give the agent an exact list of what to remove: the old function, the flag, the mode branches, and any tests that exist only for the old path. Then check for leftovers yourself:
git grep -n -e "TAX_IMPL" -e "old_tax" -e "new_tax"Any match outside the files you expected is a lead. It might be a doc, a deploy script, or a second caller that the agent missed. After the removal, run the full test suite, the type checker, and the linter. Unused imports and unreachable branches are common leftovers, and the linter finds them.
Review this diff the same way as any other. A cleanup diff should be almost entirely deletions. Added lines in a deletion commit need an explanation.
Common mistakes
- Removing the flag but leaving its configuration in deploy files or dashboards.
- Deleting old tests that still describe behavior the new code must keep.
- Skipping the shadow stage because the tests pass.
- Leaving "temporary" flags in place for months, so that the code carries two paths indefinitely.
Exercise
A teammate asks the agent to switch all traffic to the new implementation and delete the old implementation and the flag in one commit. Give two reasons to split this into separate steps, and name the order.
Show answer
Reasons: (1) after the combined commit, rolling back requires a redeploy, because the old code is gone; (2) the cleanup diff mixes a behavior change with deletions, which makes the review harder. Order: first switch traffic and watch metrics, then, after a stable period, remove the old code and the flag in a deletion-only commit.