Microservice Patterns
Strangler Fig
Replace a monolith piece by piece: put a routing facade in front of it, move one capability at a time to a new service, shift traffic gradually — and keep rollback one switch away.
What you will learn
- Plan a migration as a sequence of small, reversible steps.
- Use a routing facade to shift traffic per route and per percentage.
- Limit the blast radius of a buggy new service with canary releases.
- Handle data during a migration (shared database, sync, ownership transfer).
Before you start
Overview
A strangler fig grows around an old tree, one branch at a time, until it can stand on its own and the old tree is gone. Rebuilding a large system works best the same way: grow the new one around the old one, function by function, while the business keeps running.
A routing facade (API gateway or reverse proxy) sits in front of the monolith. For each capability you rebuild, you add a route that sends some or all of its traffic to the new service. The monolith shrinks until nothing routes to it anymore.
Why not rewrite everything at once?
- A big-bang rewrite delivers no value until the very end — often years later.
- The old system keeps changing while the rewrite chases it.
- Cut-over day concentrates all risk in one moment with no easy way back.
Run it
A: a buggy Orders v2 gets 10% of /orders — only a sliver of users notice; drag the slider to 100% to see a big-bang failure. B: a healthy v2 takes over in steps while /catalog stays on the monolith. C: a bug appears at 50%; flipping the route back stops the errors immediately.
A migration step by step
- Put the facade in front of the monolith with routes that all point to it (no behaviour change).
- Pick a capability with clear boundaries and real value (e.g. order history).
- Build the new service; keep it reading the same data at first, or sync data via events / CDC.
- Shadow traffic or compare results, then canary 1% → 10% → 50% → 100% while watching errors and latency.
- Move data ownership to the new service; remove the code path from the monolith.
- Repeat — and decommission the monolith when nothing routes to it.
Implementation: weighted routes
spring:
cloud:
gateway:
server:
webflux:
routes:
- id: orders-v2
uri: lb://orders-v2
predicates:
- Path=/orders/**
- Weight=orders, 10 # 10% of /orders
- id: orders-legacy
uri: lb://monolith
predicates:
- Path=/orders/**
- Weight=orders, 90
- id: everything-else
uri: lb://monolith
predicates:
- Path=/**Advanced Random percentages give each request a different backend; for flows with state (multi-step checkout) route sticky by user or tenant id instead, so one customer stays on one side. The hardest part is data: during the transition either both sides share the old database (simple, but coupled), or the new service owns a copy synced through events/CDC with one clear owner per field, never two writers.
Trade-offs
| Benefits | Costs | |
|---|---|---|
| Risk | Small, reversible steps; bugs reach a fraction of users. | Two systems run in parallel for a long time. |
| Delivery | Value ships with every migrated capability. | The facade and data sync are extra work that is thrown away at the end. |
| Consistency | Behaviour can be compared side by side. | Shared or synced data during the transition is a source of subtle bugs. |
Production considerations
- Compare error rate and latency per backend for the same route during every traffic shift.
- Automate rollback (route weights in config, not code) and rehearse it.
- Track what remains in the monolith; a migration that stalls at 80% doubles operating costs forever.