My account

Moving away from a legacy system

A full rewrite from zero usually takes far longer than planned, and the old system keeps changing while you wait. The Strangler Fig way is safer. Put a facade in front of the old system, move one small part at a time to new code, and send that part's traffic to the new code. Compare old and new results before you trust the new part, keep a way back, and delete the old code when a part is done.

Not reviewedWritten with AI helpReading time: 16 minOnline shop exampleStrangler FigAnti-corruption layer

Author: bezzad

Contents

The problem: a 12-year-old shop

Our online shop runs on one big application from 12 years ago. It works, and it makes all the money. But:

  1. A small change, like a new discount type, takes three weeks and often breaks something far away.
  2. Only two people understand the order code, and one of them is leaving.
  3. The framework is old, and the hosting company will stop supporting it next year.

The team says: “Let’s stop and rewrite everything from zero. It will take 18 months.” The architect must answer: is this a good plan, and if not, what is better?

Why a big rewrite usually fails

A full rewrite looks clean, but it has known traps:

  1. The old system does not stop. For 18 months, the business still asks for changes. You build them twice, or the new system is behind on the day it is ready.
  2. Hidden rules. Twelve years of fixes are in the old code: a tax rule for one region, a special case for gift cards. Nobody wrote them down. The new system forgets them, and you find out in production.
  3. No value until the end. The business pays for 18 months and gets nothing until the switch. When the plan slips to 30 months, the project is often stopped half done.
  4. One huge, risky switch day. Everything changes at once, so when something breaks, it is hard to know what.

A rewrite can still be right for a small system with clear rules. For a big system that makes the money, there is a safer way.

The Strangler Fig idea

1. Facade first2. First part moved3. Old part is smallFacadeOld systemFacadeCatalogOldFacadeCatalogSearchCartOldThe facade decides where each request goes. A way back is one routing change.
Step 1: a facade sits in front of the old system and sends all traffic to it. Step 2: the new Catalog service is ready, so the facade sends catalog requests to it. Step 3: most parts are new, and the old system is small. In the end it is removed.

A strangler fig is a plant that grows around a tree, little by little, until it can stand alone. We do the same with software:

  1. Put a facade in front of the old system. This is a reverse proxy or an API gateway. At first it sends every request to the old system. Users see no change.
  2. Pick one part and build it new. For example, the product catalog.
  3. Send that part’s traffic to the new code. The facade sends /products/* to the new Catalog service. Everything else still goes to the old system.
  4. Repeat part by part. The old system gets smaller, the new one gets bigger.
  5. Remove the old part when no traffic reaches it. This step is often forgotten, and then you run two systems forever.

The business gets value after the first part, not after 18 months. And if a new part fails, the facade can send traffic back to the old code in seconds.

Which part first?

The first part teaches the team how to do the move, so pick it with care:

  1. A clear edge. A part with few links to the rest, like the catalog or the email sender. Not the order code, which everything touches.
  2. Real value. A part the business wants to change often, so the move pays back soon.
  3. Low risk. If it breaks, the shop can still sell. Payment is a bad first choice.
  4. Small. It should be done in weeks, not months.

Use the domain model to find edges: bounded contexts from DDD or an Event Storming workshop show where one part ends.

The anti-corruption layer

The old system has its own words and data shapes. In the old code, an order has 140 columns, and status = 7 means “paid but not packed”. If the new code uses these shapes directly, the old design leaks into the new system.

An anti-corruption layer (ACL) is a small translator between the two:

// The only place that knows the old status numbers.
public static class LegacyOrderStatus
{
    public static OrderStatus ToNew(int old) => old switch
    {
        1 => OrderStatus.Created,
        7 => OrderStatus.Paid,
        9 => OrderStatus.Shipped,
        _ => throw new UnknownLegacyStatus(old)
    };
}
  1. The new code talks in its own clean model.
  2. Only the ACL knows the old shapes and numbers.
  3. When the old system is gone, you delete the ACL, and the new model stays clean.

The hardest part: data

Moving code is easy compared to moving data. For each part, decide who writes the data at each moment:

  1. Phase 1: old writes, new reads a copy. Changes in the old database are copied to the new database, for example with change data capture (CDC) or events. The new service only reads.
  2. Phase 2: new writes, old reads a copy. You switch the writer. Now the old system gets a copy, because old reports and screens still need it.
  3. Phase 3: only new. Nothing old reads the data any more. Stop the copy and remove the old tables.

Never let both systems write the same data at the same time. Two writers means two sets of rules, and the data drifts apart.

Prove it before you trust it

UserFacadeOld systemNew servicecopyComparerlogs each differencethe user gets the old answer
In a parallel run, the facade sends a copy of the request to the new service too. The user gets the old answer. A comparer logs every difference, so the team can fix them before the switch.

How do you know the new part behaves like the old one, including the hidden rules?

  1. Parallel run (shadow traffic). The facade sends each request to both. The user still gets the old answer. A comparer logs every difference. Each difference is a bug in the new code, or a hidden rule you now know about. This works best for reads, or for writes that go to a test copy of the data.
  2. Step by step switch. When the differences are near zero, send 1% of users to the new part, then 10%, then 100%, with a feature flag or the facade’s routing.
  3. A fast way back. At every step, one setting sends traffic back to the old code.

Branch by abstraction: the same idea inside the code

Sometimes the part to replace is not behind a URL but deep inside the code, like the tax calculation. Then:

  1. Add an interface in front of the old code, for example ITaxCalculator, and make all callers use it.
  2. Write the new implementation behind the same interface.
  3. Switch with a flag, compare results, and finally delete the old implementation.

The code stays in one main branch the whole time. No long-lived branch that is hard to merge.

Measure the move

Report progress in numbers the business understands:

  1. Share of traffic served by new code.
  2. Lead time for a change in moved parts compared to old parts.
  3. Incidents caused by moved parts.
  4. Lines or modules of old code deleted.

If the last number does not grow, you are not migrating. You are just adding a second system.

Common mistakes

Mistake Result Better way
A full rewrite of a big, money-making system Years of work, no value until the end, lost hidden rules. Move part by part behind a facade.
Starting with the hardest part The team learns on the riskiest code. Start with a clear, small, valuable part.
New code uses the old data shapes The old design leaks into the new system. Put an anti-corruption layer between them.
Both systems write the same data The data drifts apart. One writer at a time, with a planned switch.
No comparison before the switch Hidden rules are found by customers. Parallel run and a step by step switch.
Never deleting old code You run and pay for two systems forever. Make “old part removed” the end of each step.

Summary in 7 lines

  1. A big rewrite of a large system is slow and risky: the old system keeps changing and hidden rules get lost.
  2. Strangler Fig: a facade in front, move one part at a time, then remove the old part.
  3. Start with a part that has a clear edge, real value, low risk and a small size.
  4. An anti-corruption layer keeps the old shapes out of the new model.
  5. For data, have one writer at a time: old writes, then new writes, then only new.
  6. Prove the new part with a parallel run and a step by step switch, with a fast way back.
  7. Measure traffic moved and old code deleted, not only new code written.

Done reading this lesson?Mark it as finished so it shows in your lessons list and report card.