Video in progress
How do you migrate a large monolith to microservices step by step using the strangler fig approach?
A routing layer (gateway/proxy) is placed in front of the monolith, new features and extracted parts are written as new services, and traffic is moved to them route by route; the monolith gradually "withers". The hardest part isn't code but data: splitting the shared database and syncing during the transition.
📖 Answer
The "let's rewrite everything from scratch" approach often drags on for years, and the old system keeps changing in the meantime. Strangler fig means replacing the monolith piece by piece while keeping it running.
-
Choosing the boundary. For the first service, pick a part that's important to the business, loosely coupled to other parts and frequently changed. For example, notifications or the catalog. Starting from the most tangled core, like payments, is a mistake.
-
Routing. A gateway in front of the monolith:
routes:
- id: catalog-new
uri: http://catalog-service
predicates: [ Path=/api/catalog/** ]
- id: legacy
uri: http://monolith
predicates: [ Path=/** ]
You start with traffic by percentage or with internal users, then switch over fully. Rolling back is changing one route.
- Data. The new service needs its own database, but the monolith still uses the old tables. The stages:
- first the new service reads from the monolith's DB (temporarily);
- then its own DB, with data synced through CDC or events;
- the write source moves to the new service, and the monolith works through its API or events;
- finally, the old tables and code in the monolith are deleted.
- An anti-corruption layer. A translation layer so the new service doesn't carry over the monolith's old model (strange status codes, a 40-column table).
At every stage, compare the results of the old and new paths (shadow traffic or a "parallel run") — especially for anything involving calculations.
Risks:
- A distributed monolith — if services call each other synchronously on every request, you have added network problems on top of all the monolith's problems;
- An endless transition period — being stuck at "80% migrated" and keeping two systems alive is the most expensive state. Deleting the old code in the monolith has to be part of the plan, with a deadline.
Sign up to mark studied questions and save your progress.