Strangler fig: splitting a monolith
Move a monolith to services one route at a time, keeping a way back at every step.
Open this lesson in the learning hubKey points
- Never rewrite in one go. Put a facade in front of the monolith first, usually your gateway, so routing becomes something you control.
- Choose the first slice by pain and independence: read-heavy, low-risk, and owning as little shared data as possible.
- Run both sides at once. Send a small share of traffic to the new service, compare the answers, then raise the share.
- The data is the hard half. Copy the tables, keep them in sync for a while, then flip the writes and let the monolith read the service.
- Delete the old path once the route is at 100%. The migration is not done while the dead code still compiles.
Example
# The facade owns the migration. Each route moves on its own schedule.
spring:
cloud:
gateway:
routes:
- id: catalogue-migrated # step 3: fully moved, old code deleted
uri: http://catalogue:8080
predicates:
- Path=/catalogue/**
- id: orders-canary # step 2: 5% to the new service
uri: http://orders:8080
predicates:
- Path=/orders/**
- Weight=orders, 5
- id: orders-monolith # the other 95%, and the way back
uri: http://monolith:8080
predicates:
- Path=/orders/**
- Weight=orders, 95
- id: everything-else # step 1: nothing moved yet
uri: http://monolith:8080
predicates:
- Path=/**
Strangle the monolith route by route, and keep a way back until the old code is gone.
This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Microservices course, and every lesson in it is listed on the Microservices contents page.