Modernising an application step by step while it stays in use
An application that has grown over the years carries day-to-day business. Stopping it for a year to rewrite it is rarely possible and even more rarely wise. The alternative replaces it piece by piece while it keeps running — and follows its own rules.
What you will learn
The answer first
Modernise in small stages that can each be deployed on their own. For every area, first capture today’s behaviour in tests, then create a seam at which old and new code are interchangeable. The new part runs alongside the old one for a while, until it demonstrably does the same thing. Only then do you switch over — and delete the old part.
Martin Fowler named this approach after the strangler fig, a plant that slowly grows around a tree and eventually replaces it. The application stays in use the whole time, and each stage delivers value on its own.
Why the big rewrite so often takes longer
The plan “we will build it all again, just modern” sounds tidy. Fowler names the obstacles it usually runs into in practice: replacing a serious system takes a long time, and users cannot wait that long for new features. Existing behaviour is hard to specify completely, because much of it is written down nowhere. And some of that behaviour is no longer needed at all — rebuilding it one to one is wasted work.
Then there is an effect anyone who has accompanied a rewrite will recognise: while the new system is being built, the old one still has to be maintained. Every change is made twice, or it is postponed “until the new one is ready”.
Whether an application can be renewed step by step at all is settled beforehand by an inventory; how that works is described in Taking over existing PHP software. This article starts after that: the decision for the gradual route has been made, and the question now is how.
First, capture today’s behaviour
Before an area is rebuilt, it has to be clear what it does today — not what it ought to do. That is what characterisation tests are for, a term from Michael Feathers’ book “Working Effectively with Legacy Code”. You call the existing code with typical cases and edge cases and record the actual result as the expectation, even if it looks odd.
Such tests do not judge; they only report when something changes. If a result turns out to be plainly wrong, it is noted and the business side decides whether to correct it — separately from the rebuild. Doing both in one step means that afterwards nobody can tell whether a difference was intended. The distinction is described in Bug or change request.
Seams: where old and new can be separated
A seam is a point at which behaviour can be swapped without changing the surrounding code. In an application that has grown over time, seams often have to be created first. Typical candidates:
The code the transition needs — redirects, comparison layers, switches — is not waste. Fowler calls it transitional architecture and points out that the reduced risk and earlier value outweigh its cost. It simply has to be planned as temporary from the start.
Old and new side by side for a while
Once the seam is in place, the new part is built and first used in shadow mode: both parts calculate, users still receive the old result, and differences are logged. Real operation then shows whether the new implementation does the same thing, without an error affecting anyone.
// Shadow mode: answer with the old result, compare with the new one, log differences.
final class ShadowShippingCost implements ShippingCost
{
/** @var list<string> */
public array $differences = [];
public function __construct(private ShippingCost $old, private ShippingCost $new) {}
public function forOrder(int $weightGrams, string $country): int
{
$expected = $this->old->forOrder($weightGrams, $country);
$actual = $this->new->forOrder($weightGrams, $country);
if ($expected !== $actual) {
$this->differences[] = "{$weightGrams} g {$country}: old {$expected}, new {$actual}";
}
return $expected; // users still get the old answer
}
}
$cost = new ShadowShippingCost(new LegacyShippingCost(), new TariffShippingCost());
$cost->forOrder(2000, 'DE');
$cost->forOrder(5000, 'DE');
$cost->forOrder(7500, 'AT');
echo implode(PHP_EOL, $cost->differences ?: ['no differences']), PHP_EOL;ShippingCost interface (not shown in the excerpt; amounts in cents). Output of the full script: 5000 g DE: old 490, new 790 — the new version differs exactly at the 5 kg boundary. Shadow mode finds edge cases like this before a customer does.Shadow mode suits calculations and read access. Where the new part writes data, sends e-mails or calls other systems, it may only pretend in shadow mode — otherwise everything happens twice.
The switch itself is best made with a feature toggle that can be reversed without a new deployment, at first for some users or locations only. Pete Hodgson’s article on martinfowler.com describes how such toggles tend to multiply and carry an ongoing maintenance cost; some teams therefore create the task for removing a toggle as soon as they add it. The same applies here: once the new part is established, the toggle, the comparison layer and the old code are deleted.
A hypothetical example
An order management system in four stages
An order management system that has grown over several years is to be modernised. Instead of a rewrite, the team defines four stages, each of which can be deployed on its own:
- Delivery note generation is moved behind an interface and switched to a maintained PDF library. Reference files show that the content stays the same.
- Price calculation gets characterisation tests, then a new implementation in shadow mode. After two weeks without unexplained differences, it is switched over.
- The order list is built as a new page, and a router redirects its URL. The old list stays available until staff are comfortable with the new one.
- Only at the end is the order data storage changed, with a planned migration.
The application is running at every point, and each stage brings a visible benefit. Leaving data storage until last is deliberate: as long as old and new code use the same tables, the way back stays simple. How such a change is planned is covered in Planning a data migration.
In which order?
There is no universal order, but there are useful criteria:
An end point matters just as much. For each stage, agree beforehand when it is finished: when the old code has been deleted, not when the new code is running. Without that rule you end up with two applications, both of which need maintaining.
Limits and typical mistakes
If you are facing this decision, we support you in modernising PHP software that has grown over time — from the first seam to deleting the old code.
Common misconceptions
Further sources
- Strangler Fig Martin Fowler, 2024: gradual replacement instead of a rewrite
- Branch By Abstraction Martin Fowler, 2014: rebuilding behind an abstraction layer
- Feature Toggles (aka Feature Flags) Pete Hodgson: kinds of toggles and how long they live
About the author
Fabian Lücker is managing partner of PHP Schmiede KG in Rheine and has worked in software development since 2007. He is responsible for development and design, and writes here about decisions that come up in real projects.
Should your application become more modern without standing still?
Describe which areas cause the most effort today. We will suggest a first stage that can be checked on its own.