Decomposition strategies: where to cut
Splitting a monolith only helps if the service boundaries are in the right place. Draw them wrong and every change still needs several teams. This chapter shows how FTGO finds boundaries that hide details, keep related changes together and survive the god classes that every part of the monolith uses.
key idea / coordinator service / good path in progress / watch out problem / failure neutral
In chapter 1 FTGO decided to leave its monolith, for a measured reason and one domain at a time. We saw that the property to protect is independent deployability, and that a bad split gives a distributed monolith. That left one problem open: where exactly do the service boundaries go?
How do we find service boundaries that let each FTGO team change and deploy its service alone? By the end you can judge a boundary by cohesion and coupling, derive services from business capabilities or DDD subdomains, run an event storming workshop, spot the obstacles a split creates, and break up a god class such as Order.
What a good boundary hides
FTGO's first new Delivery service reads the customer's address straight from the monolith's ORDERS table. A month later the order team splits the address into street and city columns. Nobody tells the delivery team, because nobody knows they depend on it. That evening couriers get empty addresses.
A microservice is a module with a network between it and the other modules, so the old rules of good modular design still apply. David Parnas showed that modules pay off only when their boundaries hide details. The connections between modules are the assumptions they make about each other; fewer assumptions mean freer modules.
A service exposes a public API of commands (change data), queries (read data) and events (announce that something changed). Its database is private: no other service reads or writes its tables.
Size matters less than people think. A good service is one a small team can build and change without waiting for others. Share code between services only for things that rarely change, such as a Money class.
Two words describe how good a boundary is. Cohesion is about the things inside it: code that changes together should live together. Coupling is about things across it: a change on one side should not force a change on the other. Constantine's law puts it in one line: a structure is stable when cohesion is strong and coupling is low. Only a stable boundary allows independent deployment.
Services that work together are always coupled somehow: Order must ask Accounting to charge the card. "No coupling" is not an option. We need a way to tell harmless coupling from the kind that will hurt.
Not all coupling is equal
With every call now going through APIs, FTGO can still build boundaries that leak.
To place an order, Order Service builds a delivery manifest and sends it to Kitchen Service, only so that Kitchen can forward it to Delivery Service. When the delivery team adds a field to the manifest, the order team must change and redeploy too, even though Order never uses the manifest.
Name the kind of coupling between two services. Domain coupling is normal; pass-through, common and content coupling each have a known fix.
| Kind | FTGO example | What to do |
|---|---|---|
| Domain | Order Service calls Kitchen to create a ticket and Accounting to authorize the card | Unavoidable. Send as little data as possible. Many downstream calls hint that one service does too much. |
| Pass-through | Order sends a delivery manifest to Kitchen only so Kitchen can forward it to Delivery | Let the service that needs the manifest build it, so changes to it stay hidden from Order. |
| Common | Order and Kitchen both update the status column of one shared orders table | Give the table one owner. Others send it requests, which it can reject if the status change is invalid. |
| Content | Kitchen reads and writes Order Service's internal tables directly | Send requests to Order Service's API instead. |
A fifth kind sits beside the ranking: temporal coupling, where both services must be up at the same moment for a call to work. Asynchronous messaging (chapter 5) relieves it.
Cohesion and coupling tell FTGO whether a boundary is good once it exists. They do not say where to draw the first one. For that the team needs a method that starts from the business.
Start from what the business does: capabilities
The FTGO architects stand at an empty whiteboard with a long backlog of user stories. Grouping the existing code by package gives the old modules back, tangles included. They need a starting point that does not come from today's code.
Start from the requirements. First find the system operations, then group them into services, then define each service's API and how services collaborate. For the grouping, use business capabilities: the things the company does to make money.
Step 1 reads the user stories: verbs become operations (createOrder, acceptTicket), nouns become domain objects (Order, Ticket, Courier). Step 2 groups them by capability. A capability stays stable even when the way the business performs it changes, so a service built around it stays stable too.
Step 3 assigns each operation to a service and works out who must call whom. Placing an order, for example, belongs to Order Service but needs four others:
A capability map names the services but says little about what goes inside them. Worse, the same word means different things to different parts of the business: to the kitchen an "order" is a list of dishes to cook, to accounting it is an amount to charge.
Give each part its own model: DDD subdomains
Domain-driven design (DDD) attacks exactly that problem: one word, several meanings.
The FTGO monolith has one enterprise-wide data model. The kitchen team wants to add a prepare-by time to orders, accounting wants a refund state, delivery wants a pickup window. Every change lands in the same Order definition, and every team argues over it.
Split the business domain into subdomains, such as order taking, kitchen, delivery and accounting, and give each one its own model with an explicit boundary, called a bounded context. Each bounded context becomes a service, or a small group of services.
In practice FTGO ends up with nearly the same services as the capability map. What DDD adds is a vocabulary for what lives inside each boundary. Three ideas matter most for drawing service boundaries:
| Idea | Meaning | Why it helps |
|---|---|---|
| Ubiquitous language | Use the same words in code as the business uses | Gives APIs and events a shared vocabulary. A generic data model that calls every product an "arrangement" hides the real domain. |
| Aggregate | A cluster of objects managed as one unit, such as an Order, with state, identity and a life cycle | Keep one aggregate inside one service. Other services ask for changes, and the aggregate may refuse. |
| Bounded context | An explicit boundary around one model that exposes some things and hides the rest | It is information hiding by design, so it makes a stable service boundary. |
The same real-world thing can have a different name and different fields in each context. A short side example from MusicCorp, an online music shop: Finance keeps payments for a customer, while Warehouse ships goods to a recipient. They are the same person. A shared model can also expose less than the internal one, such as a stock count instead of shelf locations.
A context can even be split into several services inside, as long as the outside world still sees one boundary. Two design guidelines, borrowed from object-oriented design, help decide what belongs together:
Subdomains are only as good as the team's understanding of the business. Developers who model from the existing database tables will rebuild the monolith's structure with new names.
Find the model with the experts: event storming
FTGO's developers draft the subdomains alone, from the database schema. They miss that a restaurant can reject a ticket after accepting it, a case the kitchen staff deal with every day. The model looks tidy and is wrong.
Run a workshop with the people who know the business. Put everyone in one room with a long wall and plenty of sticky notes. First write down what happens, as events in the past tense; then work toward the structure: commands, aggregates and bounded contexts.
- Events. Orange notes, one per business event: Order Placed, Ticket Accepted, Ticket Rejected, Payment Authorized. Pain points and open questions get notes of their own.
- Commands. Blue notes for the actions that cause each event, such as Place Order or Accept Ticket, and the actors who issue them.
- Aggregates. Yellow notes for the nouns the events cluster around: Order, Ticket, Delivery.
- Bounded contexts. Group the aggregates into contexts. These often follow the company's structure, and they are the candidate services.
The wall also shows where events cross a boundary, which is where event schemas will have to be shared. The result can feed an event-driven design or a plain request and response one. Treat it as a draft of the domain model, not a finished design.
A workshop takes the time of busy domain experts, and it finds the boundaries the domain suggests. But the domain is not the only force on a boundary: law, technology and teams push too.
When the domain is not the only force
The domain is the best default because domain boundaries change least. Do not turn it into dogma: the goal you are chasing decides which driver matters most.
Card numbers enter FTGO with every order. If Order Service passes them on to Accounting, both services, and everything that can reach them, fall under the strict PCI audit for card data. The domain map does not say anything about that.
Start from domain boundaries, then refine them by volatility, data sensitivity, technology and team ownership. Keep each extra cut only when it serves a goal you can name.
- Volatility: pull out the parts that change most often. It helps time to market but does little for a scaling problem. Beware fixed "fast" and "slow" labels such as bimodal IT: they let the hard-to-change parts stay untouched.
- Data: keep sensitive data (personal data, card data, health data) in a few services. A payments company, PaymentCo, kept full card numbers in one red zone, so most of its system escaped the strictest audit. For FTGO this means Accounting alone handles card numbers, and data-protection (GDPR) requests touch only the few services that hold personal data.
- Technology: a part that needs a different runtime must become its own service. Do not make this the general rule, or you rebuild the three-tier layers from chapter 1.
- Organization: a service owned by two teams invites delivery contention. Code that changes together should be owned together, following Conway's law. When teams are reorganized, a service can simply change owner.
Mixing drivers is fine. A domain-shaped Restaurant service might still be split further because one part, say a menu-recommendation engine, needs a different language.
Every extra boundary is one more network hop. With the boundaries drawn, FTGO now has to face what the split breaks in operations that used to be simple method calls.
What splitting breaks
In the monolith, createOrder was a few method calls inside one database transaction. Now it calls four services over the network. Each hop adds latency. If Accounting is down, nobody can order. If Kitchen creates a ticket and the card is then declined, there is no single transaction to roll both back.
These obstacles are why the rest of the course exists. Each has a fix, and each fix gets its own chapter:
| Obstacle | What to do for now |
|---|---|
| Network latency | If two services chat constantly, give them a batch API that fetches many things in one call, or merge them. |
| Synchronous calls reduce availability | If Order needs Accounting, Kitchen and Consumer all up at once, its availability is lower than any of theirs. Asynchronous messaging (chapter 5) removes that dependency. |
| Consistency across services | No ACID transaction spans services. Sagas (chapter 9) use local transactions and compensations instead. |
| No single consistent view | Each service's data is consistent on its own, but a snapshot across services is not. If some data must always be read consistently, keep it in one service. |
Those four have later chapters. The last obstacle in the figure blocks the split itself, today: some classes are used by every part of the monolith, so no boundary can be drawn around them.
The god class: Order is needed everywhere
FTGO's Order class does order taking, restaurant preparation, delivery and billing. It has dozens of fields and every module uses it. Put it in Order Service, and Kitchen, Delivery and Accounting call Order Service for every step. Copy it into every service, and four teams share one model: back to common coupling.
Apply DDD: each service keeps its own small model of the order, with only the fields and behavior it needs, under its own name. Order Service keeps an Order (status, total, items), Kitchen Service a Ticket (items, prepare-by time), Delivery Service a Delivery (pickup, address, courier).
Each model can now change without the others. When the kitchen adds a prepare-by time, only the Ticket changes. Billing data moves to Accounting in the same way. The models refer to one another by order id, not by sharing an object.
One real order now lives as three models in three services, and they must stay in step: when an order is cancelled, the ticket and the delivery must follow. That means deciding how big each model should be, and how a change in one reaches the others.
Aggregates, and when to add a service
The unit that answers both questions is the aggregate.
FTGO wants to add scheduled orders, placed today for delivery on Friday. Should this extend the Order aggregate in Order Service, or become a new service? And how much should one aggregate hold: one order, or a restaurant's whole day of orders?
DDD gives the vocabulary. A bounded context is the scope of one model with its own ubiquitous language. It holds one or more aggregates, each modelling one domain concept; an entity is one instance of it. A command changes one aggregate and may be rejected. The change that happened is announced as an event. An anti-corruption layer translates at the edge so one context's concepts do not leak into another.
Aggregate size is a performance decision. One aggregate is changed by one thread at a time, so small aggregates allow more parallel work, while a large aggregate keeps a bulk change consistent. A side example from an online beauty shop makes the trade-off visible: its products have variants (sizes or colors) and belong to categories.
Match the aggregates of your consumers where you can. If the stock service emits one event per variant but the discount service works per category, the discount service must do a lot of work to relate them. For FTGO, one aggregate per order is the natural size: a restaurant's whole day in one aggregate would force every order through one thread.
Add the feature to an existing service when it belongs to the same concept and changes with it. Create a new service when the existing one is overloaded, unclearly owned, or needs a different stack or scaling.
| Add to the existing service when | Create a new service when |
|---|---|
| The concept is closely related and changes together, like an order and its order lines. | The service already depends on many others and carries too much responsibility. |
| A new service would otherwise have to share the existing one's database. That is a red flag; consume its event stream instead. | Several teams own the existing service and nobody owns it clearly. |
| The existing service is anemic: a thin shell around a database with no domain logic. | It needs a different technology stack, needs to scale on its own, or has a different usage pattern. |
Scheduled orders change together with orders, so they extend the Order aggregate. A courier-payout feature with its own team and its own batch load would be a new service. Not every service needs to be event-driven either. A small, low-traffic admin service can be a plain synchronous service with strong consistency, while data that many services share travels as events.
The boundaries are drawn and each service has its own models. But we have said little about how the code inside a service should be organized, how an aggregate enforces its rules, or how it announces its changes so the other services can follow.
Patterns in this chapter
| Pattern | Problem it solves | What it costs |
|---|---|---|
| Information hiding behind an API | Services break when another team changes its tables | Every access needs an API call; the API must be designed and kept stable |
| Coupling ranking (domain, pass-through, common, content) | Boundaries that look clean but still force joint changes | Judges an existing boundary; does not say where to draw the first one |
| Decompose by business capability | No starting point that is independent of today's code | Coarse; says little about the model inside each service |
| Decompose by subdomain (bounded contexts) | One enterprise-wide model that every team fights over | Needs real domain knowledge; one concept now has several models |
| Event storming | Developers modelling the domain alone, from database tables | Workshop time from domain experts; the output is only a draft |
| Boundaries by volatility, data, technology or team | Sensitive data, fast-changing parts or ownership disputes spread across services | Extra boundaries and network hops; must serve a named goal |
| One model per service (split the god class) | A class like Order that every part needs blocks any split | Several models of one real thing must be kept in step |
| Aggregate sized for its consumers | Unclear unit of consistency and change | Small means more events and broker load; large means sequential processing |
Recap
- A good service boundary hides its data behind an API of commands, queries and events, with strong cohesion inside and low coupling across it.
- Domain coupling is normal, but pass-through, common and content coupling each have a fix, and a thin CRUD wrapper is a sign the logic has leaked to callers.
- Go from requirements to operations, then to services, then to APIs, and group operations by business capability or by DDD subdomain; both usually give FTGO the same services.
- DDD supplies ubiquitous language, aggregates and bounded contexts, and event storming finds them with the domain experts, events first.
- Volatility, sensitive data, technology and team ownership can refine domain boundaries, while latency, availability and consistency problems are the price of every cut.
- A god class such as
Orderis broken up by giving each service its own small model, sized as aggregates that suit their consumers.
Quick check
Show answer
Each service has its own model in its own bounded context. The kitchen only cares about items, the prepare-by time and the ticket status, so a small Ticket fits better than the huge shared Order, and it can change without touching Order Service.
Show answer
It is content coupling, the tightest kind: Kitchen reaches into Order's internal data. When the order team renames the column or adds a status rule, Kitchen breaks or writes invalid states that Order Service never checked. Kitchen should send a request through Order Service's API, or publish a "ticket ready" event that Order Service reacts to, so Order stays the only owner of its data.
Show answer
It is a data-driven boundary. Keep full card numbers inside Accounting only, the "red zone", and let Order and the other services work with a token or payment id. Then only Accounting falls under the strict card-data audit.
FTGO now has services with clear boundaries and their own models. Next we look inside one service: how to organize its business logic, how aggregates enforce their rules, and how they publish domain events.