Video in progress
When you split a service out of a monolith, how do tests guarantee that behavior hasn't changed?
Before the split I write tests at the monolith's boundary that pin down behavior (golden master, API-level tests), then I run the new service in shadow mode and compare the old and new responses on prod requests. Only when the differences are near zero do I move traffic over step by step.
📖 Answer
In the strangler fig approach, the risk is the new service working "almost the same". Small differences (rounding, date format, an empty list instead of null) aren't visible in unit tests, but they break consumers.
The steps:
-
Pin down the boundary with tests. I test the monolith's entry points for the function being extracted (REST endpoints, an internal interface) at the API level. These same tests then also run against the new service: one test set, two implementations. The internal unit tests aren't moved, because they're tied to the old structure.
-
Golden master. With a set of real requests taken from prod (anonymized), the old implementation's output is saved and compared with the new one.
-
Shadow traffic. In prod the request goes to the monolith, and an asynchronous copy also goes to the new service. Responses are normalized (timestamps, IDs) and the differences go to a metric and a sampled log. For write operations, shadow needs care: the new service writes to a separate database or only computes "what it would have done".
-
Step-by-step cutover. Through a feature flag or gateway routing, 1% → 10% → 100%, watching the error rate and the differences at each stage; the way back is a single flag.
-
Data consistency. If the data is moved too, a reconciliation job between the two sides: row counts, control totals, sample comparison.
Trade-off: shadow and reconciliation cost infrastructure and time. For a low-risk function (an internal report), 1-2 stages are enough; for the part that touches money or permissions, I do all of them.
Sign up to mark studied questions and save your progress.