The Strangler Fig pattern is easy to describe: put a facade in front of the old system, move one piece of functionality at a time behind it into new services, and retire the old system when nothing is left. Diagrams of it are everywhere. What the diagrams leave out is how you know, at each step, that clients still get exactly what they got before.
This case works through one full step with running code: a small shop built as a monolith, a gateway in front of it, and the product catalog moved into a service of its own. The code is on GitHub: chudovo/application-modernization, case 06.
At a glance
| Before | After | |
|---|---|---|
| Clients talk to | the monolith | a gateway (YARP) |
| Product catalog | monolith, table Products | catalog service with its own database |
| Orders and stock | monolith | monolith, same address for clients |
| Orders need product data | read the Products table directly | ask an interface that calls the catalog service |
| Switching the catalog over | not possible | one routing change, no restart |
| Contract checks passing | 19 of 19 | 19 of 19 in both routing states |
The order of work
- Write a contract against the monolith as it is.
- Put the gateway in front, routing everything to the monolith.
- Cut a seam inside the monolith where other code reaches into the part that will leave.
- Build the new service and run the same contract against it through the gateway.
- Copy the data and switch the route.
Each step is small and each can be released on its own. The contract runs after every one of them.
Step 1: the contract
The contract is 19 checks written against HTTP only: status codes, headers, and response bodies compared as text. It knows nothing about what is behind the address. That is what lets the same file run against the monolith, against the gateway with everything still in the monolith, and against the gateway with the catalog served by the new service.
[Fact]
public async Task New_product_is_created_with_a_location_the_client_can_follow()
{
var response = await Http.PostAsync("/api/products", Json("""{"sku":"RUG-11","name":"Wool rug","price":210.00}"""));
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
Assert.Equal("""{"sku":"RUG-11","name":"Wool rug","price":210.00,"isActive":true}""", await Body(response));
// Clients follow this header as it is, so it must point at the address they talk to.
Assert.Equal(new Uri(Http.BaseAddress!, "/api/products/RUG-11"), response.Headers.Location);
}
Comparing bodies as text is deliberately strict. Clients of a ten-year-old system depend on details nobody wrote down: a price printed with two decimals, an error message shown to the user as it comes.
Step 2: the gateway, with nothing extracted
The gateway is YARP, the reverse proxy library from Microsoft, with two routes. The catalog paths have an owner that can be changed while the gateway runs; everything else goes to the monolith:
private RouteConfig[] Routes() =>
[
// The extracted slice: everything under /api/products.
new RouteConfig
{
RouteId = "catalog",
ClusterId = CatalogOwner == Owner.CatalogService ? "catalog-service" : "monolith",
Match = new RouteMatch { Path = "/api/products/{**rest}" },
},
// Everything else still belongs to the monolith.
new RouteConfig { RouteId = "everything-else", ClusterId = "monolith", Match = new RouteMatch { Path = "/{**rest}" } },
];
/// Moves the catalog paths to the other system. Takes effect for the next request, no restart.
public void RouteCatalogTo(Owner owner)
{
CatalogOwner = owner;
Provider.Update(Routes(), Clusters());
}
At this point both routes lead to the monolith and nothing is supposed to change. The contract disagreed: 2 of 19 checks failed.
Expected: http://127.0.0.1:39703/api/products/RUG-11 (the gateway, which the client talked to)
Actual: http://127.0.0.1:36103/api/products/RUG-11 (the monolith behind it)
When the monolith creates a product or an order, it returns the address of the new resource, built from the address it was called at. Behind a gateway that is its internal address, which clients cannot reach. The proxy sends the original host along in headers, but the application has to be told to use them:
// Behind the gateway the request arrives at an internal address. The gateway says which address the client used,
// and links such as the Location header are built from that. Name the proxy you trust; do not trust everyone.
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedHost | ForwardedHeaders.XForwardedProto,
});
So even the step that changes nothing changes something. Better to learn that before any service has moved.
Step 3: the seam
Inside the monolith, the order code read the catalog table whenever it needed a name or a price. That direct access is what makes a monolith hard to split, and it has to go before the catalog can:
// Before: the order code reads the catalog table itself. Nothing in a monolith stops it.
var product = await db.Products.FindAsync(line.Sku);
// After: it asks an interface. Configuration decides who answers.
public interface ICatalog
{
Task<CatalogProduct?> FindAsync(string sku);
}
CatalogProduct? product;
try { product = await catalog.FindAsync(line.Sku); }
catch (CatalogUnavailableException)
{
// What used to be a method call can now fail on its own.
return StatusCode(503, new { error = "Orders cannot be placed right now: the catalog is unavailable." });
}
At first the interface is answered from the local table, and the monolith behaves as before. The seam also shows how wide the dependency really is. Here the order code needs four fields of a product and nothing else.
Step 4: the new service, and what the contract caught
The catalog service was first written the way one writes a new ASP.NET Core service today, without looking at the monolith’s responses. Run through the gateway, 10 of 19 contract checks failed:
| The contract says | The first version of the new service did |
|---|---|
| Prices have two decimals: 89.50 | 89.5 |
| Every error has a body: {“error”:”…”} | empty bodies for 401, 404 and 409, a validation document for 400 |
| Location is an absolute address | /api/products/RUG-11 |
| Search ignores case | a case-sensitive search |
None of these is a bug in the new service taken alone. Each is a default that differs from what the old system did, and each takes one line to fix once it is known. Without the contract, clients would have found them in production, one at a time.
Step 5: copy the data and switch
The new service gets its own database. A small copy job reads every product through the monolith’s own API and stores it; it can be run again, and the second run picks up changes made in between. Then two switches are flipped: the gateway routes the catalog paths to the new service, and the monolith’s order code asks the service instead of its own table.
What extraction adds that the monolith never had
The contract proves that nothing changed for clients. Extraction also creates situations that did not exist before, and those need tests of their own.
A dependency that can be down. With the catalog service stopped, orders can still be read and stock queried. Placing an order returns 503 with a clear message and reserves no stock. The catalog paths return 502 from the gateway. Before the split, none of these states was possible.
Routing back is immediate. Data is not. The pattern is often sold with the promise that you can always switch back. The route, yes:
[Fact]
public async Task Routing_back_to_the_monolith_is_immediate_but_data_written_since_the_cutover_stays_behind()
{
await _shop.Client.PostAsync("/api/products", Json(NewRug));
await _shop.Client.PutAsync("/api/products/CHAIR-01/price", Json("""{"price":95.00}"""));
_shop.Routing.RouteCatalogTo(Owner.Monolith);
// The very next request is answered by the monolith again, without a restart.
Assert.Equal(HttpStatusCode.OK, (await _shop.Client.GetAsync("/api/products/DESK-02")).StatusCode);
// But the monolith's table stopped at the cutover: the new product and the new price are not there.
Assert.Equal(HttpStatusCode.NotFound, (await _shop.Client.GetAsync("/api/products/RUG-11")).StatusCode);
}
A product created after the cutover exists only in the new service, and the monolith still has the old prices. There is a second trap: the gateway route and the monolith’s setting are separate switches. After routing back alone, clients see the monolith’s catalog at 89.50 while a new order is still priced by the service at 95.00. The test asserts that too. A rollback plan has to say how data flows back and in which order the switches change.
What is verified, and what is not
| Check | Result |
|---|---|
| Contract against the monolith | 19 / 19 |
| Contract through the gateway, everything routed to the monolith | 19 / 19 |
| Contract through the gateway, catalog served by the new service | 19 / 19 |
| Extraction tests: routing, data copy, outage, routing back and forward | 5 / 5 |
- Load and latency were not measured. Every catalog lookup during an order is now a network call. There is no cache, retry or circuit breaker here.
- Writes during the cutover are not handled. A product changed in the monolith between the last copy and the switch is lost. A real cutover needs a short write freeze or change capture.
- No deployment. The pieces run in one test process on local ports, with in-memory databases. No containers, no service discovery, no encryption between services.
- Only the easy slice. Stock and orders share a transaction and stay together. Splitting them is a harder problem, covered by other patterns.
Run it yourself
Only the .NET 10 SDK is needed. The tests start every piece on a free local port:
dotnet test before/tests/Shop.Contract # the contract against the monolith
dotnet test after/tests/Shop.Contract # the contract through the gateway, twice, plus the extraction tests
Why the catalog went first
It is read far more than it is written, one other part of the system depends on it, and it shares no transaction with anything. That is the profile of a good first candidate. How to find such a slice in a real system, what to do about the shared database, and when not to split at all is covered in the monolith to microservices migration guide.
If you need a team for a migration like this, see our comparison of top .NET modernization companies.