Microservice Patterns
Service Decomposition
Where to draw service boundaries: by business capability, not by technical layer — and not so fine that every request becomes a fan-out of synchronous calls, a distributed monolith.
What you will learn
- Compare decomposition by technical layer and by business capability.
- Measure coupling as network calls per request.
- Recognise the distributed monolith and its symptoms.
- Use domain-driven design ideas to find boundaries.
Before you start
Overview
A company organised by tool — all the people who type in one room, all who use phones in another — needs a meeting for every customer request. A company organised by customer journey — sales, delivery, support — lets each team finish its job mostly alone.
Good service boundaries put things that change together and are used together in the same service (high cohesion) and keep interactions between services rare and coarse (low coupling). A practical measure of coupling is network calls per request: every extra synchronous call adds latency, a failure mode and a deployment dependency.
Run it
The same checkout, decomposed three ways. Divide Calls between services by Checkouts, compare p50, and in C make the loyalty service slow.
Decomposition styles
| Style | Example | Result |
|---|---|---|
| By technical layer | web / business logic / data access services | Every feature touches every service; many calls per request. |
| By entity | customer, product, order "CRUD services" | Business logic leaks into callers; chatty orchestration. |
| By business capability / bounded context | ordering, payments, fulfilment, catalog | A feature usually changes one service; few, coarse calls. |
| Too fine-grained | one service per function | Distributed monolith: everything must deploy and stay up together. |
Symptoms of a distributed monolith
- Most features need coordinated releases of several services.
- One request synchronously calls five or more services.
- Services share a database or a "common" library with domain logic.
- A slow or failed minor service (loyalty, recommendations) breaks a core flow.
Reducing calls per request
// Before: checkout asks five services every time.
var customer = customers.get(id); var price = pricing.quote(cart); var stock = inventory.check(cart);
var rate = shipping.rate(address); var points = loyalty.balance(id);
// After: orders keeps the copies it needs, updated asynchronously…
@KafkaListener(topics = "customer-events") void on(CustomerChanged e) { localCustomers.upsert(e); }
@KafkaListener(topics = "price-events") void on(PriceChanged e) { localPrices.upsert(e); }
// …calls only what must be decided now, and treats optional features as optional.
var stock = inventory.reserve(cart); // must be synchronous
var points = loyalty.balanceOrDefault(id, Duration.ofMillis(100), 0); // timeout + fallbackMore ways to find and keep boundaries
- Decompose by subdomain: use domain-driven design to split the business into core, supporting and generic subdomains; each bounded context becomes a service candidate, and generic ones (billing, identity) are often bought, not built.
- Self-contained service: a service should be able to answer a synchronous request without waiting on other services — keep the data it needs locally (replicated by events) and use asynchronous messaging for the rest.
- Service per team: give each service exactly one owning team, sized so the team can understand, change and operate it alone; if two teams must change one service together, the boundary is wrong.
Trade-offs
| Benefits | Costs | |
|---|---|---|
| Coarse services | Few calls, simple transactions, fast requests. | Larger codebases per team; less independent scaling. |
| Fine services | Very focused, independently scalable units. | Chatty calls, more failure modes, coordinated releases. |
| Replicated data | Fewer synchronous dependencies. | Eventual consistency and event plumbing to maintain. |
Production considerations
- Map capabilities with domain experts (event storming) before drawing service boxes.
- Track calls per request and co-deployment frequency as architecture health metrics.
- Align team ownership with service boundaries (Conway's law works either way).
- Merge services that always change together; it is a valid refactoring.