MF Marcus Feld

Legacy · 12 min read

Rewriting the system nobody wanted to touch

Nine years of accumulated decisions, one engineer who understood it, and an eighteen-month rewrite proposal on the table. What we did instead, and what it cost.

The system was nine years old, written in PHP 5.6, and processed the company's entire order book. It had 412 routes, a 6,000-line file called functions.php, and exactly one person — Rita, the operations lead, not even a developer by title — who could predict what it would do. Everyone else worked around it.

The engineering team had a rewrite proposal, and it was a good proposal. Modern framework, proper tests, a migration plan, a two-phase cutover. Eighteen months, four engineers, one very confident architecture diagram. The board had approved the budget in principle. I was asked to review it, which is a polite way of asking someone to find the reason not to do it.

I found three, and none of them were about technology.

The spec is the code

The first problem: nobody could write down what the system did. The documentation described a workflow that had been changed in 2019 without the doc being updated. The ticketing system had 4,100 closed tickets referencing behaviour that no longer existed. The tests covered 11% of the code, mostly the parts that were easy to test and never broke.

This is the structural problem with rewriting a long-lived system. A rewrite assumes that requirements exist somewhere other than the running code. In a system this old, the code is the specification — including the three undocumented special cases for one wholesale customer, the invoice rounding that differs by region, and the reason the export runs at 4 a.m. rather than on demand.

A rewrite is not a port. It is a reverse-engineering project wearing a port's clothes.

Every one of those undocumented behaviours will be discovered during the rewrite, at the worst possible time, by a user who has relied on it for six years and never mentioned it because it worked. The estimate accounts for none of them. This is why rewrites are late by a factor, not a percentage.

The second problem: you run two systems

During an eighteen-month rewrite, the old system keeps receiving new requirements. Someone in sales promises a customer a feature. Regulatory reporting changes. The rewrite team must implement every new feature twice, or the new system launches without them, and then launches with a backlog of parity work.

One client of mine budgeted nine months for a rewrite. It took thirty-one. Not because the engineers were slow — they were good. Because the business kept moving, and every quarter of movement added a quarter of parity work. The old system became a competitor to the new one, and it was winning, because it had customers.

Third: the person who knows is not on the team

Rita was not going to be seconded to a rewrite. She ran operations. And she was the only one who understood the failure modes. A rewrite team that cannot ask Rita why the nightly job sometimes runs twice will reimplement the bug.

What we did instead

We kept the system and made it observable. That sounds like a downgrade, and for the first month it felt like one. The sequence was deliberate.

1. Instrument before touching

Two weeks: request logging with timing per route, a database query log with slow-query capture, and a weekly report of which routes were actually called. This produced the single most useful document of the whole engagement — a list of 412 routes ranked by traffic. 63 of them accounted for 99.2% of requests. 118 had not been called in ninety days.

The rewrite proposal had scoped all 412. Two-thirds of the work was for routes nobody used.

2. Cut by domain, migrate by slice

We grouped the hot routes into four domains — invoicing, shipping, customer records, reporting — and chose the one with the cleanest boundary and the least write traffic: invoice read queries. Read-only means a mistake is a wrong page, not a wrong payment.

The old system stayed authoritative. The new service was deployed behind a route shim, one path at a time:

# nginx: route by slice, not by page. Old paths keep working.
location /api/invoices/search { proxy_pass http://invoices-v2; }
location /api/invoices/{ proxy_pass http://legacy; }
location /api/            { proxy_pass http://legacy; }

For six weeks we ran shadow reads: the new service received the same query, its result was logged and compared, and the old response was returned to the user. We found eleven discrepancies in the first week — nine were bugs in the new code, two were behaviours in the old code that nobody knew about and that we then had to decide whether to preserve. Both were preserved. One is still documented as "regional rounding," which is the polite name for a 2017 workaround.

3. Move the person, not just the code

We put Rita on the migration team for two days a week. Not as a developer — as the oracle. Every ambiguous behaviour got a decision, written down in a file we called DECISIONS.md, which became the actual specification the rewrite never had. It ended at 340 entries. It is now the most-read document in the company.

Where it landed

Fourteen months in, 62% of traffic was on the new services. Three low-traffic domains were still on the old system, deliberately: one was scheduled for replacement by a vendor product, and two were going to be retired with a customer contract that ended the following year. That is a successful migration, not an incomplete one. The goal was never to delete the old code. It was to stop being afraid of it.

The numbers, since I dislike stories without them:

MeasureBeforeAfter 14 months
Sev-1 incidents4 per month1 per quarter
Deploy frequencyEvery 3 weeksDaily (new services)
Time to diagnose an issue~2 days~40 minutes
Engineers able to change billing15
Total spendRoughly 40% of the rewrite budget, over a longer period

Diagnosis time is the number I care about most. Four incidents a month is survivable. Not knowing why they happen is what makes a team leave.

When a rewrite is the right answer

I have recommended a full rewrite twice. The conditions were specific and all three held.

  • The runtime is unsupported. Not old — unsupported. No security patches, no vendor, no path forward. That is a deadline, not a preference.
  • The business model changed, not just the code. If the product is now something structurally different, preserving behaviour is preserving the wrong thing.
  • The team is large enough to run both systems. Two teams, not one team split in half. A rewrite with a single team is not a rewrite, it is a pause.

In this case none of the three held. The runtime was old but supported. The business was stable. The team was three engineers. A rewrite would have consumed every one of them for eighteen months and shipped nothing to a customer in that time.

Most rewrites are not engineering decisions. They are an emotional response to technical debt — a wish to be free of a system that makes people feel stupid. That feeling is real and worth addressing. The rewrite is usually the most expensive possible way to address it, and the least likely to work.

The system is still there, by the way. Three domains, low traffic, gently maintained. Rita retired last spring. Her DECISIONS.md is now maintained by two engineers who never met her, which is, I think, the actual definition of a successful handover.


MF

Marcus FeldIndependent software architect in Berlin. I review systems, embed with engineering teams, and write down what breaks. More about my work.