Topic 156 / Advanced
Strangler fig and anti-corruption layer
Chapter 9.5 · Data Patterns and Evolution
What and why
Replacing a legacy system in one cutover is a well-known way to fail: all the risk lands on one release, and the new system must match years of accumulated legacy behavior before it can be trusted. The strangler fig pattern migrates incrementally instead: a facade routes each piece of functionality to the new system as it's ready, and to the legacy system for everything not yet migrated, until the legacy system is "strangled" down to nothing and can be retired. The anti-corruption layer (ACL) protects the new system's domain model while this is happening, translating between the legacy system's model and the new one so legacy concepts never leak in. Builds on: → 56 (bounded contexts and domain boundaries — the ACL is the translation at that boundary).
Diagram
Requests ──► [Facade / router]
│
┌─────────┴─────────┐
▼ (not yet migrated) ▼ (migrated)
[Legacy system] [Anti-corruption layer] ──► [New system]
(translates legacy (clean domain
model ↔ new model) model, →54)
Over time, more traffic routes right, less routes left,
until the legacy system has no traffic left to serve.
How it works
- Put a facade (often an API gateway, → 41, or a simple routing layer) in front of both the legacy system and the new system.
- Pick one piece of functionality — a bounded context (→ 56) — to migrate first; build it in the new system.
- Route that functionality's traffic through the facade to the new system; everything else still goes to the legacy system.
- Where the new system must still talk to the legacy system (shared data it doesn't own yet), put an anti-corruption layer between them — it translates the legacy system's model and vocabulary into the new system's domain model (→ 54), so legacy quirks never leak into the new code.
- Repeat: migrate the next piece, extend the facade's routing, keep the ACL covering whatever legacy dependency remains.
- When every piece has been routed away from the legacy system, it serves no traffic and can be decommissioned — the "strangling" is complete.
- At every step, run the new path and, where risk warrants, compare its output against the legacy path before fully cutting over, rather than trusting it on day one.
Practical example
(illustrative). A long-lived billing system is replaced piece by
piece: first, "generate invoice" is rebuilt and routed to the new system, while "process
refund" still goes to legacy. The new invoice service, which still needs each customer's
legacy account identifier, goes through an ACL that translates the legacy system's single
denormalized cust_no field into the new system's clean Customer aggregate (→ 55) — the
new domain model never has to know cust_no exists. Later, every billing function has been
migrated and the legacy system is switched off with no big-bang cutover ever having
happened.
When to use it
- Use when: replacing a legacy system that is too large or too risky to cut over in one release, and functionality can be split into independently migratable pieces.
- Avoid when: the legacy system is small enough to rewrite and validate in one controlled effort — the facade, routing, and ACL add coordination cost that a small rewrite doesn't need.
- Failure scenarios: the facade routing logic itself becomes a second legacy system no one wants to touch; skipping the ACL and letting the new domain model absorb legacy concepts directly recreates the legacy model's problems inside the "clean" rewrite; migrating pieces with no plan to ever retire the legacy system, so it lingers indefinitely as a permanent dependency.
- Simpler alternative: for a small, well-understood system, a direct rewrite with a single cutover, validated by a parallel-run period and a rollback plan, is simpler than building and maintaining a long-lived facade and ACL.
Practice
(Pattern kind — implement minimal; 25 min, two small processes). Build a
tiny "legacy" service (a key-value store with an odd field name, e.g., cust_no) and a
tiny "new" service with a clean model (customerId). Build a facade that routes a GET /customer/:id request to the new service for one customer ID and to the legacy service,
through an ACL that translates cust_no into customerId in the response, for all others.
Confirm callers see the same clean shape regardless of which system actually answered.
Done when the facade's routing can move one more ID from "legacy" to "new" without any caller-visible change in response shape.
Key takeaway
Migrate incrementally behind a routing facade, and protect the new domain model with an anti-corruption layer until the legacy system has no traffic left. Done when you can: design a migration plan that moves one bounded context at a time with no big-bang cutover. Used next in → 157 (architecture evolution — strangler fig as the standing example).
References
A pattern language for microservices — see references.md.
Source directory