Strangler Fig Pattern
Incrementally extract services from a monolith while routing selected traffic to the new system.
What you will be able to do
- Explain why big-bang rewrites of a monolith are risky
- Describe the strangler fig pattern: a facade in front, one capability moved at a time
- Route requests by path to either the monolith or a new service
- Roll a moved route out gradually, and roll it back by switching the route
- Choose a good first slice, and handle the data the slice owns
The idea, in plain English
Most microservices start life as a monolith. Rewriting the whole thing at once - the big-bang rewrite - means months of parallel work, a frozen old system, and one risky switch-over day.
The strangler fig pattern moves the system a piece at a time instead. A facade (a router or API gateway) sits in front of the monolith. One capability is rebuilt as a new service, and the facade sends just its requests to it. Then the next, and the next, until the monolith has nothing left to do.
Every step is small, runs in production, and can be undone by pointing the route back. The name comes from the strangler fig tree, which grows around a host tree until it stands on its own.
Worked example: Moving an online shop off its monolith one route at a time, behind a single front door.
An everyday example: renovating a shop that stays open
A shop owner wants a new building, but cannot close for six months:
- A new front door goes up first. Every customer comes in through it - for now, it just leads into the old shop.
- A new counter is built for one thing - say, payments. The front door sends payment customers to the new counter; everyone else still goes to the old shop.
- If the new counter has a problem, the door sends payment customers back to the old one. Nobody is turned away.
- Counter by counter, the old shop empties. When it is empty, it is knocked down.
- The shop was open every single day.
The new front doorThe facade: a router or API gateway in front of everything.A new counter for paymentsOne capability rebuilt as a new service.The door sends payment customers thereThe facade routes just that path to the new service.Sending them back if it failsRollback: point the route at the monolith again.Knocking down the empty shopRetiring the monolith when nothing routes to it.The facade decides where each request goes
- Step 1: put the facade in front of the monolith, routing everything to it. Nothing changes for users - but now you control the traffic.
- Step 2: rebuild one capability as a service and add one route: /catalog goes to catalog-service.
- Step 3: move the next one. The monolith handles less with every stage.
- Rollback is a routing change, not a redeploy: remove the route and the monolith takes the traffic back.
- The model below runs four stages over five requests.
def route(path, routes):
for prefix, target in routes:
if path.startswith(prefix):
return target
return "monolith"
requests = ["/users/7", "/orders/812", "/catalog/pens", "/payments/55", "/reports/daily"]
stages = {
"Stage 1 - facade only": [],
"Stage 2 - catalog moved": [("/catalog", "catalog-service")],
"Stage 3 - orders moved": [("/catalog", "catalog-service"), ("/orders", "order-service")],
"Stage 4 - rollback orders": [("/catalog", "catalog-service")],
}
for stage, routes in stages.items():
targets = [route(p, routes) for p in requests]
left = targets.count("monolith")
print(f"{stage:<28} monolith handles {left} of {len(requests)}")
for p, t in zip(requests, targets):
if t != "monolith":
print(f" {p:<14} -> {t}")
# Stage 1 - facade only monolith handles 5 of 5
# Stage 2 - catalog moved monolith handles 4 of 5
# /catalog/pens -> catalog-service
# Stage 3 - orders moved monolith handles 3 of 5
# /orders/812 -> order-service
# /catalog/pens -> catalog-service
# Stage 4 - rollback orders monolith handles 4 of 5
# /catalog/pens -> catalog-serviceMoving a route gradually
- You do not have to switch a route all at once. Send a small percentage of its traffic to the new service first.
- Watch error rates and response times. If they hold, raise the percentage: 5%, 25%, 100%.
- If they do not, drop it back to 0% - most users never noticed.
- This is the same idea as a canary release (Lesson 41), applied to a migration.
- For risky slices, you can also send a COPY of traffic to the new service and compare its answers without using them (Shadow Deployment, Lesson 43).
import random
def route_orders(percent_new, rng):
return "order-service" if rng.random() * 100 < percent_new else "monolith"
rng = random.Random(42) # fixed seed, so the run is repeatable
for percent in (0, 5, 25, 100):
sent = [route_orders(percent, rng) for _ in range(1000)]
print(f"{percent:>3}% rollout: {sent.count('order-service'):>4} of 1000 requests to order-service")
# 0% rollout: 0 of 1000 requests to order-service
# 5% rollout: 44 of 1000 requests to order-service
# 25% rollout: 254 of 1000 requests to order-service
# 100% rollout: 1000 of 1000 requests to order-serviceChoosing the first slice - and its data
- Pick a first slice that is low-risk, clearly bounded and has few dependencies - often something read-heavy, like the catalogue.
- Avoid starting with the most tangled, most critical part. Learn the routing, deployment and monitoring on something forgiving.
- Code is the easy half. The slice’s DATA must move too: the new service needs its own database (Lesson 16).
- A common path: copy the data and keep it in sync (Change Data Capture, Lesson 21), switch reads to the new service, then switch writes - and only then let the new service own it.
- Until the data has moved, the monolith and the service are still coupled through it.
Strangler fig, step by step
1. FacadePut a router in front; route everything to the monolith.
gateway -> monolith
2. ExtractRebuild one capability as a service.
catalog-service
3. RouteSend that path to the service - gradually if needed.
/catalog -> catalog-service
4. Move the dataSync, switch reads, switch writes, hand over ownership.
CDC, then cut over
5. DeleteRemove the old code from the monolith.
monolith gets smaller
RollbackPoint the route back at the monolith.
remove the route
What usually goes wrong
Rebuilding everything before switching anything means months without feedback and one huge, risky cut-over.
If the new service still reads the monolith’s tables, a schema change still needs both. Move the data as part of the slice.
If the moved feature stays in the monolith "just in case", both copies get maintained and drift apart. Delete it once the route is stable.
The first slice is where you learn routing, deploys and monitoring. Do that on something forgiving.
A migration that stalls at 60% leaves you running two architectures forever. Plan the order, and track what is left.
Practice
Write these yourself before opening anything. Getting them wrong first is most of how this sticks.
A monolith serves /catalog, /cart, /orders, /payments and /admin/reports. Choose an extraction order and give a one-line reason for each.
Show hintHide hint
Start forgiving and well bounded; leave the most tangled for last.
Show solutionHide solution
1. /catalog read-heavy, few dependencies - a safe place to learn
2. /cart small, customer-facing, depends only on catalog
3. /orders core flow; by now routing and monitoring are proven
4. /payments high-risk; move with a gradual rollout and shadow traffic
5. /admin/reports reads from everything - easier once the data has movedAfter moving /orders to order-service at 100%, error rates jump. What do you do first, and why is it quick?
Show solutionHide solution
Point /orders back at the monolith in the facade (or drop the rollout to 0%).
It is a routing change, not a redeploy - the monolith still has the orders
code, so traffic recovers at once. Then investigate the new service offline.Key points
- The strangler fig pattern migrates a monolith one capability at a time, instead of a big-bang rewrite.
- A facade in front routes each request to the monolith or a new service.
- Each step runs in production and is undone by changing a route.
- Move a route gradually with a percentage rollout, and watch the metrics.
- Start with a low-risk, well-bounded slice.
- Move the slice’s data as well as its code - otherwise the two stay coupled.
- Delete the old code once a route is stable, and finish the migration.
Quick check before you move on
Interview questions
How would you migrate a monolith to microservices?
Incrementally, with the strangler fig pattern: put a facade or gateway in front, extract one well-bounded capability at a time into its own service with its own data, route its traffic gradually while watching metrics, keep rollback as a routing change, delete the old code, and repeat until the monolith can be retired.
How do you handle the database during a strangler migration?
Give the new service its own store. Copy and sync the data (often with change data capture), move reads first, then writes, then make the new service the owner and stop the monolith writing to those tables.
What are the risks of the strangler fig pattern?
Running two systems for a long time, routing complexity, data kept in sync across both during the move, and migrations that stall halfway. Mitigate with a planned order, data ownership moved per slice, and deleting old code as you go.
Quiz
- 1.
Why is a big-bang rewrite riskier than the strangler fig pattern?
- 2.
What must move along with a slice’s code?
- 3.
When is the migration finished?
Comments
Sign in to leave a comment. Your name and photo come from Google; nothing else is shared.
Loading comments...