Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 37 ยท Designing Service Boundaries

Decomposition Strategies

Decompose a system by business capability or subdomain, migrate safely with the strangler fig pattern, and protect new services with an anti-corruption layer.

Knowing your bounded contexts (Finding Boundaries with Domain-Driven Design) tells you where the lines should be. Decomposition is the practical question of how to get there: how to carve services out of a system that already exists and keeps earning money while you work on it.

This lesson covers two ways to choose slices (by business capability and by subdomain) and two patterns for carrying out the move safely: the strangler fig and the anti-corruption layer. You will build a small routing facade that moves traffic from an old system to a new one one route at a time.

Two ways to choose a slice

Business capability What the business does: sell, ship, bill.
Subdomain Core, supporting or generic parts.
1 / 2

Decomposing by business capability

A business capability is something the business does to create value, described in business terms and independent of software: manage catalog, take orders, process payments, fulfil shipments, handle returns. Capabilities are stable. The way you implement "take orders" changes every few years, but a shop will always need to take orders.

To decompose this way:

  1. List the organization's capabilities, usually from a business or product person, not from the code.
  2. Group them into coherent areas of responsibility, each with clear goals.
  3. Make each area a service (or first, a module) that owns the data for those capabilities.

Capabilities line up well with teams, since a team can own "payments" end to end. The weakness is that the first draft is often too coarse or too fine, so expect to refine it.

Decomposing by subdomain

In DDD a subdomain is a part of the business problem. They come in three kinds, and the kind tells you how much effort to invest.

  • Core subdomain. What makes your business different, such as a recommendation engine for a streaming service. Build it yourself, with your best people.
  • Supporting subdomain. Necessary but not special, such as inventory tracking. Build it simply, or buy it.
  • Generic subdomain. The same for everyone, such as authentication or sending email. Buy it or use a standard solution; do not reinvent it.

Subdomains and bounded contexts often match one to one, but not always: a subdomain is part of the problem, a bounded context is part of your solution. Subdomains help you decide where microservices are worth their cost. A core subdomain that changes weekly benefits from independent deployment. A generic one probably belongs in a managed service.

Obstacles you will meet

Slicing by the book works until the data pushes back.

  • Shared data. Two capabilities use the same table. You must decide which context owns it, and give the other a copy or an API (Database per Service and Data Ownership).
  • Chatty cuts. If two slices call each other many times per request, they belong together.
  • God classes. One class referenced everywhere, often Customer or Order, resists splitting. Break it by context first, inside the monolith.
  • Transactions. Work that used one database transaction now crosses services and needs a saga (Sagas).

The strangler fig pattern

A strangler fig is a tree that grows around another tree until the original is gone. In software, the strangler fig pattern means you put a thin facade in front of the old system and route all traffic through it. Then you build one new service, switch a single route to it, watch it, and repeat. Over months the old system handles less and less, until you can switch it off.

flowchart LR
    C["Clients"] --> F["Routing facade"]
    F -->|"/orders"| NEW["New orders service"]
    F -->|"everything else"| OLD["Legacy monolith"]
    NEW --> ND[("Own database")]
    OLD --> OD[("Legacy database")]

The facade is the only thing clients know. Routes move to new services one by one.

The big advantages are that every step is small and reversible, the business never stops, and you deliver value along the way. The alternative, a "big bang" rewrite, usually runs late, has to reproduce years of hidden behavior and lands all the risk on one release day.

A routing facade you can run

The stand-in below builds the facade as plain functions in one Node file, with no sockets, so its output is deterministic. A route table says, per path, which backend handles it and what share of calls (0 to 100 percent) goes to the new one. Shares let you migrate gradually: first 10 percent, then 50, then 100. Real facades are NGINX, an API gateway or a service mesh doing the same job.

Node.js Strangler facade with gradual rollout

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice that rollback is a one-line configuration change, not a redeploy. That safety net is the main reason the strangler fig works. In a real facade you often route by user (a stable hash of the user id) rather than counting calls, so one person does not see the old and the new behaviour alternate; the counter here is only a simple model. Deployment Strategies and CI/CD develops canary releases further, and Project: Strangling a Monolith walks through a full migration.

The anti-corruption layer

The legacy system has its own model, often awkward: cryptic field names, status codes like "S3", dates as strings, a "customer" that mixes five concepts. If your new service talks to it directly, those quirks seep into the new code, and you rebuild the same mess with a new name.

An anti-corruption layer (ACL) is a translator at the boundary. The new service speaks its own clean language; the ACL converts legacy shapes into it and back. All knowledge of the legacy quirks lives in one place, and it can be deleted when the legacy system retires.

Node.js Translating a legacy record

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The new service never sees STAT or TOT_AMT. When an unknown legacy code appears, the failure is loud and located at the boundary, instead of corrupting data deep inside the new service.

Both stand-ins run inside a single process. A real facade adds network hops, health checks and timeouts, and a real ACL often sits in its own adapter service or library. The ideas are the same.

Choosing where to start

Pick the first service carefully. A good first candidate is valuable but not entangled: a capability with clear boundaries, few dependencies and a motivated team, such as notifications or product search. A bad first candidate is the center of the system, such as the core order flow with transactions everywhere. Early wins teach you the platform skills (pipelines, monitoring, deployment) on low-risk traffic before you touch the dangerous parts.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main idea of the strangler fig pattern?

    A facade lets you replace the old system piece by piece, with small reversible steps and no business interruption.
  2. 2. Which kind of subdomain most deserves your best engineers and custom code?

    The core subdomain is what differentiates the business. Generic subdomains such as authentication are usually better bought.
  3. 3. What is an anti-corruption layer for?

    It keeps legacy quirks out of the new code by translating at one place, which can be removed later.
  4. 4. Why is decomposing by technical layer, such as one service per database, a poor strategy?

    Business capabilities cut across technical layers. Splitting by layer makes each feature touch every service.
  5. 5. In the facade stand-in, how was a bad release undone?

    Routing is configuration. Setting the new-traffic share to zero sends everything back to the legacy system immediately.

Summary

  • Decompose by business capability (what the business does) or by subdomain (core, supporting, generic) to choose slices.
  • Expect obstacles: shared data, chatty cuts, god classes and cross-service transactions.
  • The strangler fig pattern puts a facade in front of the old system and moves routes to new services step by step, with instant rollback.
  • An anti-corruption layer translates legacy models at the boundary so they do not leak into new services.
  • Start with a valuable, loosely coupled capability to learn the platform on low-risk traffic.

Next: Database per Service and Data Ownership.