The strangler pattern in practice: replacing a legacy system piece by piece

Incremental replacement works when each slice moves traffic and data ownership together and the old code is deleted on schedule. Routing takes the least effort, while moving the data and seeing the migration through to the end take the most.

By Nabil Ahmed7 minute read

The lunar surface near 25°S, 100°E, lit by a low Sun with night falling across one side.
The Moon near 25°S, 100°E. Rendered by Farside from NASA Lunar Reconnaissance Orbiter elevation data.

Full rewrites of core systems tend to go the same way. A team spends two years building a replacement while the old system keeps changing underneath it, and the cutover weekend arrives with a long list of behaviors that no one knew the old system had. Sometimes the cutover fails and is rolled back. When it succeeds, the business spends a year rediscovering rules that were buried in the old code.

The strangler pattern replaces a legacy system incrementally instead. A routing layer is placed in front of the old system, functions are rebuilt one at a time in a new system, each function's traffic moves across once its replacement is ready, and the old code is removed as it falls out of use. The name comes from Martin Fowler's description of the strangler fig, a plant that grows around a host tree until the tree is no longer needed.

The pattern is widely recommended and often left half done. Most descriptions stop at the routing layer, which takes the least effort, whereas success depends on how the slices are chosen, how ownership of the data moves and whether the organization carries the work through to the end.

In brief

  • The strangler pattern replaces a legacy system one function at a time behind a routing layer, so that each step is small and reversible and delivers something to production.
  • A slice is complete only when the new system owns both the behavior and the data for that function and the old code path has been deleted.
  • Each data entity should have exactly one system writing to it at any moment, because two writers for the same record produce conflicts that no synchronization job fully repairs.
  • Running the old and new systems side by side on real traffic and comparing their outputs uncovers the undocumented rules that specifications miss.
  • A strangler migration that stops partway leaves two systems running indefinitely, so the plan has to fund and schedule the removal of the old system as well as building the new one.

Where the seams are

A slice is a function that can be moved on its own, with an entry point you can intercept and a set of data you can separate. The first design task is to find those seams in a system that was never built to have them.

Entry points take more forms than HTTP requests. A typical core system also receives batch files, reads from queues and runs scheduled jobs, and reports and other applications often read its database directly. Each is a way in that must be routed or replaced. The direct database readers are the ones most often forgotten, so list them early from database accounts and query logs, since each breaks when a table moves.

Choose the first slice for what it teaches the team, and leave the most valuable functions for later. A good first slice is real, low in risk and thin, often a read-only function such as an inquiry screen or a status API. It forces the team to build the routing layer, the deployment pipeline, the monitoring and the data feed for the new system before any write path is at risk. The second and third slices should then go where the business most needs change, because a migration that delivers nothing new for a year usually loses its funding.

The routing layer

The routing layer, often called a facade, sits in front of both systems and decides which one handles each request. For HTTP traffic it can be an API gateway or a reverse proxy, for messages it is a router on the queue or topic, and for batch files it is a splitter that sends each record to one side or the other.

The facade should stay simple, routing by path, operation or cohort through configuration changes that can be reversed within minutes, as in this sketch for a reverse proxy:

# Order status has moved; everything else still goes to the legacy system.
location /api/orders/status {
    proxy_pass http://new-order-service;
}

location / {
    proxy_pass http://legacy-oms;
}

Business logic should not build up in the facade, because once it starts transforming payloads and applying rules it becomes a third system that no team planned to own. It is, however, the right place to publish a clean, versioned contract for consumers, which is ordinary API design work.

Moving ownership of the data

Moving traffic is a routing change, whereas moving ownership of the data is where most of the migration effort goes. For every entity, decide which system is the system of record at each stage, and keep to one writer per entity at any time.

The tempting shortcut is the dual write, in which the application writes every change to both databases. It fails predictably, because the two writes are not one transaction, so if the second fails or two updates arrive in a different order, the databases disagree with no indication of which one is correct.

A safer sequence for each slice has four stages.

  1. Replicate the legacy data. The legacy system remains the writer, and its changes flow into the new system's store, commonly through change data capture, which reads the database's transaction log and emits each change as an event. Tools such as Debezium and the replication services of the major cloud providers do this, and once it runs the new system can serve reads.
  2. Verify the two stores. Reconcile them on a schedule with counts, checksums and a field-level comparison on a sample, correcting the mapping until every difference is explained.
  3. Switch the system of record. The new system becomes the writer for the entity and writes are routed to it. If legacy functions that have not moved still need the data, the flow reverses so that changes replicate from new to old.
  4. Retire the old tables. Once no legacy function reads the entity, the reverse feed is removed along with the old tables.

Replication in either direction delivers events at least once, so the receiving side must be able to apply the same event twice without harm, which is the subject of idempotency and a firm requirement here. The feeds themselves are integration work with the usual requirements for retries, ordering and monitoring, which fall under systems integration, and publishing changes as events carries costs of its own, described in event-driven architecture: what it gives you and what it costs.

Proving a slice before it takes traffic

A legacy system's real specification is its behavior, since its documents describe what it was meant to do years ago, and the reliable way to learn what it does today is to run the new code alongside it.

In a parallel run, sometimes called shadowing, each real request goes to the legacy system, which still produces the answer the user sees, while a copy goes to the new system, whose answer is recorded and compared. Differences are logged and reviewed daily, and each turns out to be either a defect in the new code or a rule that was never written down, both of which are worth finding before cutover. For write operations the shadow copy has to run against a store that has no side effects, so that no emails are sent, no payments are taken and no messages go to partners.

When the rate of differences is low and every remaining difference is understood, move real traffic by cohort, starting with internal users, then one branch, region or customer group, then everyone else. Keep the route back open until each cohort has gone through a full business cycle, including a month end if the function touches finance.

A distributor's order management system

A mid-size distributor runs order management on a fifteen-year-old monolith with one large database. Orders arrive from a sales desk application, from electronic data interchange files sent by large customers and from a web portal. Pricing rules sit in stored procedures, and the warehouse system and finance reports read the monolith's tables directly.

The team puts a gateway in front of the portal and sales desk traffic and chooses order status inquiry as the first slice. Change data capture feeds the order and shipment tables into a new store, which the new status service reads, and the legacy system remains the only writer. Within weeks the new code serves status requests, and the pipeline and monitoring are in place along with the reconciliation jobs.

Pricing is the second slice, chosen because sales wants customer-specific price lists that the old system cannot support. The new pricing service runs in shadow against every quote for a month, and the comparison shows that the stored procedure rounds line totals before applying a volume discount while the new service rounds afterwards. Most orders come out the same, but on large orders the results differ by small amounts, enough to trigger invoice disputes. The rule had never been documented, and the team agrees with finance to keep the legacy behavior and writes it into the new service's tests.

Order capture, the third slice, is where the writer switches. New orders are created in the new system and replicated back to the legacy tables, since the warehouse system still reads them there. The team moves the web portal first and the sales desk second, followed by the customers who send files, one at a time. The legacy order tables can go only once the warehouse integration moves onto the new API.

Had the distributor done a single cutover, the rounding difference would have surfaced as a wave of disputed invoices with no easy way back.

Why strangler migrations stall

A poorly run strangler migration rarely ends in outright failure. More often it settles into a permanent halfway state where the valuable slices have moved and the less interesting remainder has stayed, so the organization runs two systems and two sets of skills, plus a synchronization layer, indefinitely.

Three practices prevent this. The first is to define done for each slice as "legacy code and tables deleted" and to track that in place of a count of new services. The second is to keep a visible inventory of everything that still depends on the legacy system, with an owner for each item. The third is to decide at the outset what happens to the long tail, since functions that are rarely used are often better retired or handled manually than rebuilt.

The pattern also has limits, chiefly that it needs seams. If the legacy system is a packaged product whose entry points cannot be intercepted, or its functions are so entangled in one data model that no entity can be separated, the incremental path may cost more than a planned replacement. Which systems deserve this treatment at all is a portfolio question for technology modernization, and some systems are better kept as they are, as we argue in your legacy system is an asset with a maintenance problem. The engineering of a single replacement, slice by slice, is application modernization.

What to do next

Before committing to an incremental replacement, answer these in writing.

  1. List every way into the legacy system, including user interfaces, APIs, files, queues, scheduled jobs and direct database readers.
  2. Name a first slice that is read-only, real and thin, and state what it will prove about routing, deployment and data replication.
  3. For the next three slices, state which system writes each entity before and after the move, and how changes reach the other side.
  4. Decide how old and new behavior will be compared on live traffic, and who reviews the differences each day.
  5. Define the cohorts for cutover and the conditions for routing back.
  6. Put the removal of legacy code, tables and feeds into the plan with dates and a budget, and decide now what happens to the long tail.

If the third question has no answer for a slice, that slice is not ready, however good the new code looks.

Related services

Start a conversation.

A short paragraph is enough. A principal reads every message and replies within one working day.

Or email hello@farside.works. We use what you send only to reply. Privacy