Writing ·
Don’t Rewrite It: Modernising One Piece at a Time
Around 2003, part of an overnight batch job at Travis Perkins took hours.
That part was written in UniVerse, a MultiValue database (one where a single field can hold a whole list of values). The code sorted the data in UniVerse, and then wrote the result out as a CSV file.
I changed it to write the CSV first, and then sort the file with the Unix sort command, a standard tool that sorts the lines of text files. That part of the job went from hours to under two minutes.
I knew about sort because my dad taught me Unix when I was a young child, before Linux existed. It is perhaps not the most glamorous use of a childhood, but it paid off.
I didn’t replace the system, and I didn’t rewrite the job. I rewrote one part of it, using a tool that already existed, once I understood where the time was going. That’s the whole argument of this article in miniature: most systems don’t need to be replaced to get better, and the changes that help most are often smaller than anyone expects. For the business, that means lower risk and results much sooner. For the engineer, it means learning to find the right piece to change.
Why big-bang rewrites go wrong
A big-bang rewrite is the plan to build a complete replacement for a system, then switch everything over to it in one go. It’s an appealing plan, because the old system is usually painful and the new one, on paper, isn’t. It goes wrong in a few predictable ways.
- The target moves. The business still runs on the old system, so the old system keeps changing: new rules, new products, urgent fixes. Every change has to be made twice, or the new system falls further behind.
- Nobody knows everything the old system does. Its real specification is its behaviour, built up over years, and much of it is written down nowhere. You find the missing pieces when the new system gets them wrong.
- Nothing pays off until the end. You pay to run the old system and build the new one at the same time, and users see no benefit until the switch-over. That can be years away.
- The switch-over is a cliff edge. Everything changes on the same day. When something breaks, it’s hard to tell which of a thousand changes caused it, and going back can be harder than going forward.
The alternative is to change the system in place, one piece at a time, while it keeps running. Martin Fowler calls this the Strangler Fig, after a plant that grows around a host tree: new code grows alongside the old and takes over its behaviour bit by bit, until the old system can be retired.
Understand it before you change it
Before changing anything, find out where the time, money or pain actually goes. A slow system is rarely slow everywhere. Measuring usually takes days; rebuilding the wrong part can take months.
The same applies inside a rewrite. At Neilson Financial Services, I worked on the rewrite of a desktop CRM (customer relationship management) application for the web, and cut the database calls needed to render a single page from more than 250 to fewer than ten. In general, every call is a round trip to the database, and the waits add up, so how many calls a page makes often matters more than how fast any one of them is.
Sometimes the person best placed to see this is someone from outside, who has no history with the system and no part of it to defend. That’s much of what an architecture review is for.
Make it safe to change
Old systems are often hard to change because nobody is confident that a change won’t break something. Fix that first:
- Tests around the behaviour that matters. For old code, the most useful are often characterisation tests (a term from Michael Feathers, whose Working Effectively with Legacy Code is the classic book on the subject): tests that record what the system does now, right or wrong, so that you notice if a change alters it. Start with the behaviour the business depends on, not with whatever is easiest to test.
- Monitoring that shows what the system is really doing. You can’t tell whether a change helped, or did harm, without knowing how the system behaved before it.
- A delivery pipeline you can trust. That’s the automated route a change takes from a developer’s machine to production. It should be quick, repeatable, and able to undo a bad release.
None of this changes what users see. It’s what makes every later step cheap enough to try and safe enough to reverse.
More than once, my first piece of modernisation work has been the pipeline itself, at companies where releases were deployed by hand from one particular person’s laptop. That’s more common than you might think, and it’s rarely anyone’s fault: it starts as a sensible shortcut while the system is small, and nobody gets round to replacing it. But until it’s replaced, every release depends on one person and one machine, and moving faster only makes that risk bigger.
Find the seams
A seam is a place where part of a system can be separated from the rest, replaced, and switched over on its own. (The term is Feathers’ too. His definition is narrower: a place where you can change what a program does without editing the code at that point. I’m using it a little more loosely here, for the boundaries that make that possible at the scale of a whole system.) Typical seams include a batch step with a file going in and a file coming out, a single page or screen, a well-defined interface between two parts of the system, and a queue (where one part leaves messages for another to pick up).
Finding seams is a craft rather than a formula. Some are clear from the design. Others only appear when you read the code and see what really depends on what. Where there’s no seam, the first piece of work is often to create one, by putting a clear interface around the part you want to replace.
At LeasePlan, the application was built with ASP.NET Web Forms, Microsoft’s older way of building web pages in .NET, and the aim was to move it to the newer ASP.NET MVC. There was no clean seam between the two, so I made one: I built a small framework that hosted MVC inside the existing Web Forms application, before that was common practice. The old and new code ran side by side in a single running application, so the migration could proceed while the system stayed up, which effectively eliminated downtime.
Move one piece at a time
Each piece can move somewhere different: to new code, to a new platform, to the cloud or, sometimes, back out of it. The cloud isn’t always the cheaper home, and I’ve written separately about when it’s worth asking whether you need it at all.
Switching a piece over usually needs something in front of the old and new code that decides which one handles each request: a proxy, a routing rule or a feature flag (a setting that turns a code path on or off without a release). Where you can, run both for a while and compare their results before the new code’s answers count, then move the traffic across gradually: one customer, one region or a small percentage first. If the new code misbehaves, switching back is a configuration change, not an emergency release.
Each step should deliver something on its own, and be small enough to undo. That has a cost: for a while you maintain two ways of doing the same thing, and the routing in front of them. It’s usually worth paying, because every step is small enough to understand, test and reverse. When the system is small, it may not be.
Keep the data right
Code can usually be rolled back in minutes. Data is harder: once records have been converted, merged or deleted, getting back to where you were can be slow or impossible.
So treat data migrations with more care than code changes. Plan them, and rehearse them against a realistic copy of the real data, more than once, so you know how long they take and what they get wrong. Have a way back: for example, keep the old structure and keep it up to date until the new one has proved itself, and check the migrated data against the original record by record rather than trusting the totals.
That’s how I built a migration agent for an AI app-building platform, to move organisations’ legacy applications onto it. The data migration is deterministic, with no AI involved: the same input always produces the same result. Every record is verified after import, and an interrupted run can resume from the last safe point.
When a rewrite is the right answer
Sometimes it is. It depends on a few things:
- Whether the platform still has a future. If it’s unsupported, can’t be secured, or nobody can be hired to work on it, waiting has a cost of its own.
- How big the system is. A small system that can be replaced in weeks doesn’t need the machinery of a gradual migration.
- How much of the old behaviour you still want. If most of what the system does is no longer needed, there’s less to carry across, and less risk in leaving it behind.
- Whether there are any seams at all. Some systems are so tangled that every change touches everything.
Even then, try to switch over in stages, for one group of users or one part of the business at a time, so that the cliff edge becomes a series of steps.
The short version
Big-bang rewrites put all the risk at the end and all the value even later. Modernising in place spreads the risk thinly and delivers results early, from the first piece moved. And sometimes the biggest win of all comes from understanding the problem well enough to change one small piece.
If you have a system the business depends on and can no longer change with confidence, read more about legacy modernisation, or get in touch.