The strangler fig: replacing a system one path at a time
Replace a system while it runs
Section titled “Replace a system while it runs”A ten-year-old system serves the whole product, and nobody wants to touch it, and nobody can switch it off. The obvious plan is to build a new one that does the same and switch over on a date. Martin Fowler describes how that plan goes: it sounds simple, and he has seen it go down in flames most of the time, because replacing a serious system takes long, users cannot wait for new features in the meantime, and the details of the existing behaviour are hard to find out, much of it behaviour nobody wants.
He took the name from the strangler figs he saw in Queensland in 2001: vines that grow in the nook of a tree, draw on it, reach the ground and the canopy, and can end as self-sufficient plants while the host tree dies. His preferred alternative is a gradual modernisation that starts with small additions built on top of the legacy system, and moves bits of behaviour into the new code over time. The pattern is simple to state.
Mental model. A road is rebuilt one lane at a time while traffic keeps moving. The signs at the junction send each car to the old lane or the new one. The drivers use the same road all along, and the crew can put a sign back if a new lane is not ready.
A facade that routes
Section titled “A facade that routes”Put a routing facade, a proxy, in front of the legacy system. At first it forwards everything to the legacy system. Then build a replacement for one piece, such as /invoices, and change one routing rule so that this path goes to the new service. Every path the rule table does not name yet still goes to the legacy system. The Azure Architecture Center describes the same sequence: the facade intercepts requests, initially routes most of them to the legacy system, and with each iteration shifts more of them to the new one. When nothing depends on the legacy system any more, it is decommissioned, and the facade is removed or kept as an adapter for legacy clients. The clients keep the same address throughout and never know that a migration is going on.
Try it: two paths and a share
Section titled “Try it: two paths and a share”The flow has two variants, a request on a path that has moved and one on a path that has not. Watch the facade in the middle.
The strangler fig: a facade moves paths to the new service
The facade’s routing table sends /invoices to the new service. The client sees the same address; only the facade knows that the answer now comes from somewhere else.
- The facade routes the request15 msClient to Routing facade: GET /invoices/42. Arrives at 15 ms.GET /invoices/42
- A system answers17 msRouting facade to New service: Forward to the new service (route: /invoices). Arrives at 17 ms.Processing: 40 msForward to the new service (route: /invoices)
- 59 msNew service to Routing facade: Response. Arrives at 59 ms.Response
- The client gets the response74 msRouting facade to Client: Response. Arrives at 74 ms.Response
Time until the client has the response: 74 ms40 ms of processing
Model notes
Assumptions
- Each message takes half the round-trip time of the link it crosses. Messages are never lost, never queue and never overlap, and each leaves when the one before it has arrived, once its receiver has finished any processing.
- A routing facade sits in front of both systems and every request goes through it. Migrating a path means changing one routing rule in the facade; the share you set is the share of requests whose path has already moved.
Simplifications
- Both services are assumed to answer the same request with the same response. Real migrations spend most of their effort on that: data that must be copied or synchronised, and behaviour that differs in the details.
- The facade’s own work, caching and authentication are not drawn. Rolling a path back means moving its routing rule back to the legacy system, which turns that path into the legacy case at once.
Parameter values are illustrative. They are not benchmarks of any real product.
With the defaults, A path already moved to the new service takes 74 ms. The client’s request reaches the facade, the facade forwards it to the new service, which takes 40 ms for New service processing time, and the response comes back through the facade. A path still on the legacy system takes 184 ms: the same four messages, but the legacy system takes 150 ms for Legacy system processing time. Of the 34 ms the messages take on each path, 30 are the round trip between the client and the facade, and 4 are the facade’s round trip to the system behind it, which is the price of having a facade at all. The 110 ms between the two paths is the difference in the systems’ processing times, 150 minus 40, and it does not depend on the network: with Round trip, client to facade raised to 100 ms the two paths take 144 ms and 254 ms, still 110 apart.
The control Share of requests whose path has moved to the new service is at 20% by default, and the line Average at this share shows 162 ms. It is 0.2 × 74 + 0.8 × 184, which is 14.8 + 147.2. Drag the share to 50% and the average is 129 ms; at 100% it is 74 ms, the moved path’s time; at 0% it is 184 ms. The average falls in proportion to the share, which is why a migration shows its benefit gradually, and why it hides a detail: each request is either 74 ms or 184 ms, and none takes 129 ms. A user on a legacy path sees nothing of the progress, however far the average has fallen.
Where the seams are
Section titled “Where the seams are”The hard question is what to move first, and in what pieces. Fowler’s answer is to find the seams, the places where the system can be split, and to pick pieces that are small and independent, so that a mistake is cheap. A first path is usually one with a clear boundary, little shared data and low business risk, so that the team learns on something that can be rolled back. The Azure page adds that new applications should be built so that they can easily be intercepted and replaced in future migrations, with clear lines between the parts.
Inside the code, branch by abstraction applies the same idea to a dependency that is not at the edge. Fowler’s description is: create an abstraction layer between the clients and the supplier you want to replace, move the clients to call the supplier through it, build the new supplier behind the same abstraction, switch the clients over a piece at a time, then delete the old supplier and perhaps the abstraction. The system builds and runs correctly at every step, so releases continue. The two are complementary: the facade routes at the boundary of the system, and branch by abstraction routes inside it, where the code is not reachable by a proxy. Fowler also mentions a variation that checks that both implementations return the same results to requests, a way to find differences before users do.
The hard part is data
Section titled “The hard part is data”The routing rule is one line. The data is where migrations take their time. The new /invoices service needs the invoices, and the legacy system still reads and writes them. The Azure page says to consider how to handle services and data stores that both systems might use, and to make sure both can reach them at the same time. There are three common ways, each with a cost:
- The new service calls the legacy system for what it has not moved yet, or the other way round, through an anti-corruption layer that translates between the two models, so that the new design is not bent by the old one.
- The data is copied and kept in sync, for example by capturing changes to the old tables, so that both systems have a copy. The copy can lag, and writes in two places can conflict. The Azure page mentions change data capture, reading the transaction log, as one way to reach a legacy database without changing the application.
- One system owns the table at any time, and ownership moves with the path. It is the cleanest and the hardest, because it needs the whole path’s data to be separable.
The flow assumes none of this: it takes the two services to answer the same request with the same response. Real migrations spend most of their effort exactly there, on data that has to be copied or synchronised and behaviour that differs in the details.
What happens if the new service is wrong?
Section titled “What happens if the new service is wrong?”Move the routing rule back. In the flow, a path that goes back to the legacy system becomes the legacy case at once: 74 ms becomes 184 ms, and the new service stops receiving requests. That is the pattern’s biggest strength, and it is quick for the same reason that blue/green is: the old thing is still running.
It is not safe in one case, the one the rollback lesson warned about. If the new service has written data that the legacy system cannot read, moving the rule back brings back a system that fails on the new data. A migration that moves writes needs the data to stay readable by both, or a plan to go forward. Also note that the flow’s share is the share of paths that have moved, not a share of one path’s traffic. A gradual rollout inside one path, 5% of requests first, needs the facade to split by request and not by route, and is what a canary does.
When the pattern does not fit
Section titled “When the pattern does not fit”The Azure page lists cases where it may not suit: when requests to the back end cannot be intercepted; when you cannot access the legacy system’s source code, since disabling migrated features and redirecting internal calls may need changes to it; when the system is small and replacing it all is simple; and when the original solution has to be decommissioned fully and quickly. Two more follow from the costs. A migration that stops half-way leaves two systems and a facade to run for ever, so the team has to commit to finishing. And the facade is itself a system on every request path: the Azure page says to make sure it does not become a single point of failure or a performance bottleneck. A facade should do routing, and little else.
The trade-off
Section titled “The trade-off”| Step of the framework | For the strangler fig |
|---|---|
| Decision | Put a routing facade in front of a legacy system and move its paths to new services one at a time. |
| Why | A rewrite and a switch-over on a date is the risky plan: it is slow, the old behaviour is poorly known, and users cannot wait. |
| Benefits | Small steps with value at each one. Clients keep one address. A step is undone by moving a routing rule. The legacy system keeps serving what has not moved. |
| Costs | A facade to run. Transitional code for the data and for calls between the two systems. Two systems to operate, for longer than anyone expects. |
| Failure modes | A new service that differs in the details. Data written by one system and unreadable by the other. A facade that fails or slows every request. A migration that never finishes. |
| Scaling limits | The facade handles all the traffic, so it needs capacity and redundancy of its own. The synchronisation of data costs more as the volume of writes grows. |
| Alternative | A rewrite with a cut-over date, for a small system; or leaving the legacy system alone and building new features beside it. |
Check your understanding
Section titled “Check your understanding”Check your understanding: the strangler fig
This quiz needs JavaScript to check your answers.
- The strangler fig replaces a system gradually: a routing facade in front, and one path moved to the new service at a time, with the same address for clients.
- In the flow a request takes 74 ms on a moved path and 184 ms on a legacy path. The 110 ms gap is the difference in processing time, and the facade’s own hop is 4 ms of the 34 ms on the wire.
- The average at a share is the weighted mean: 162 ms at 20%, 129 ms at 50%, 74 ms at 100%. Each request is still either fast or slow.
- Branch by abstraction applies the idea inside the code: an interface in front of the old implementation, the new one behind it, a switch.
- The hard part is the data both systems share. Going back is a routing change, unless the new system wrote data the old one cannot read.
- The pattern does not suit a system whose requests cannot be intercepted, a small system that is simple to replace, or a migration that must finish quickly. The facade must not become the bottleneck.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.