Video in progress
Which part of a monolith do you extract into a separate service first, and how do you determine the boundary?
First I take a part that is independent from a business point of view and loosely coupled, but changes often or needs to scale separately. I set the boundary by bounded context and data ownership, not by technical layer, and then migrate gradually with a strangler fig.
📖 Answer
I measure first, then cut. To see the coupling between modules inside the monolith, I map which parts of the code read and write each table. If four different modules write to one table, that is not a boundary, that is a knot.
Signs of a good first candidate:
- it has its own data and others rarely touch it (notifications, reports, catalog search);
- its connection to the monolith can be asynchronous;
- its rate of change or load profile differs from the rest;
- if it fails, the damage to the business is small.
A bad first candidate is a central domain like "order" or "user". They are connected to everything, and on the first step you end up with a distributed monolith.
Migration with a strangler fig:
- A gateway or routing layer is put in front of the monolith.
- The new service is written, first in "shadow" mode: we copy traffic and compare the results.
- Endpoints are routed to the new service one by one, behind a feature flag so you can switch back.
- Data ownership is moved: first the monolith reads through the new service's API, then the old tables are dropped.
The thing most often overlooked is data. Separating code is easy, separating tables is hard. If the new service still writes straight into the monolith's DB, you have separated only the deployment unit, not the boundary.
And the question may be "do we need microservices at all?". If the team is small and the problem is deploy speed, a well-modularized monolith (like Spring Modulith) is often the cheaper solution.
Sign up to mark studied questions and save your progress.