Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 37 ยท Designing Service Boundaries

Finding Boundaries with Domain-Driven Design

Use bounded contexts, ubiquitous language and context maps from Domain-Driven Design to split an e-shop into services, with a context-map printer to try.

The hardest question in microservices is not "which framework?" but "where do I draw the lines?". Cut in the wrong place and services need each other for everything, so each change ripples through five teams. Cut in the right place and each service is mostly self-sufficient.

Domain-Driven Design (DDD), a way of modeling software around the business it serves, gives the most useful vocabulary for this. You do not need all of DDD. Three ideas, bounded context, ubiquitous language and context map, are enough to find good service boundaries. This lesson explains them with an online shop and ends with a small program that prints a context map from a description.

Three ideas to find boundaries

Ubiquitous language One vocabulary for devs and business.
Bounded context A zone where words keep one meaning.
Context map How the zones relate to each other.

A bounded context is the best first guess for a service.

1 / 2

Words change meaning across the business

Ask different departments what a "product" is in an online shop.

  • Catalog staff mean a description, photos, a category and attributes such as "color".
  • Inventory staff mean a stock-keeping unit (SKU) with a quantity in a warehouse location.
  • Pricing staff mean a price rule, with discounts and currencies.
  • Shipping staff mean a parcel with a weight and dimensions.

If you build one giant Product class with all of these fields, every team edits it, every change risks breaking someone else, and nobody understands the whole thing. The word is the same, but the model behind it is different in each place. DDD's answer is to stop sharing one model and let each area keep its own.

Ubiquitous language

Inside one area, developers and business experts agree on a precise vocabulary and use it everywhere: in conversations, in code and in tests. In the shipping area a "shipment" has a precise lifecycle (created, picked, dispatched, delivered). If the code calls it Consignment while the business says "shipment", every conversation needs a translation, and misunderstandings creep in.

Language is the first clue to a boundary. When the same word needs two different definitions, or when two groups of people can barely follow each other's conversation, you have probably found the edge of a context.

Bounded contexts

A bounded context is a boundary, in code and team terms, inside which one model and one language apply consistently. A word like "customer" can mean different things in different contexts, and that is fine, as long as each context is explicit about its own meaning.

An e-shop might have these contexts:

Context "Customer" means Owns
Identity A login account with credentials Accounts, passwords, sessions
Orders A buyer attached to an order (name, delivery address at purchase time) Carts, orders
Billing A payer with payment methods and invoices Payments, invoices
Support A person with a ticket history Tickets, conversations

Each context can store the customer data it needs in its own shape. Orders does not need a password hash, and Identity does not need ticket history. This is why bounded contexts make good candidates for microservices: the model is already cohesive and the data is already private.

A bounded context is a logical boundary. You can run several contexts inside one modular monolith. Whether a context becomes a separate service is a separate, later decision (When (and When Not) to Use Microservices). Start by getting the boundaries right.

How to discover contexts

You do not find boundaries by looking at database tables. You find them by talking to the business.

  1. Collect the language. List the nouns and verbs people use in each department. Notice where one word has several meanings.
  2. Follow the business processes. Trace "a customer places an order" from browsing to delivery. Note the hand-offs between groups of people.
  3. Look at the organization. Departments with different goals and different vocabulary often map to different contexts.
  4. Test for cohesion. Things that change together for the same reason belong together. Things that change for different reasons belong apart.

A group exercise called event storming puts the events of a process (such as "Order Placed", "Payment Taken", "Parcel Dispatched") on sticky notes along a timeline. Clusters of events around one concept, and the places where meaning shifts, reveal the contexts.

Context maps

Contexts do not live alone. A context map shows each context and the relationship between them. Relationships matter because they decide who has to adapt to whom. A few common ones:

  • Customer/Supplier. An upstream context provides something; the downstream one depends on it. The upstream team listens to the downstream team's needs. Example: Catalog (upstream) supplies product information to Orders (downstream).
  • Conformist. The downstream context accepts the upstream model as it is, because it has no power to change it. Typical with third-party systems.
  • Anti-corruption layer (ACL). The downstream context puts a translation layer between itself and a messy upstream model so the bad model does not leak in. See Decomposition Strategies.
  • Shared kernel. Two contexts share a small piece of model and promise to change it together. Use sparingly: it creates exactly the coupling you are trying to avoid.
  • Open host service / published language. A context offers a well-documented API or event format for anyone to use.
flowchart LR
    ID["Identity"] -->|"supplier"| OR["Orders"]
    CA["Catalog"] -->|"supplier"| OR
    OR -->|"supplier"| BI["Billing"]
    OR -->|"supplier"| SH["Shipping"]
    PS["External payment provider"] -->|"conformist"| BI

Arrows point from upstream to downstream. Billing conforms to the external provider's model.

Printing a context map

The stand-in below describes an e-shop's contexts and relationships as data, then prints a readable map and checks one rule of thumb: warn about a context that talks to many others, which may mean it is doing too much. This is a modeling aid, not a real tool; the rules are simple enough to adapt.

JS Context-map printer

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

No output.

Orders sits in the middle with five relationships. That is not automatically wrong, because an order really is central to a shop, but it is the first place to look for an overgrown context, and a good reason to prefer events over synchronous calls from Orders (Asynchronous Messaging and Events).

Splitting the e-shop

Now apply the ideas. Suppose your monolith has one Customer table that contains an email, password hash, shipping address, card token and a support-notes column. Splitting by context gives each service only what it needs and a way to refer to the same person: a shared identifier.

JS One person, four models

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

No output.

The only thing the four models share is the identifier. Each service can change its own fields without asking the others, and a breach of one store does not expose the data of the rest. This is the payoff of drawing the line at the language boundary. The next step, Decomposition Strategies, shows how to get there from an existing system.

Common mistakes

  • Boundaries from tables or layers. A "database service" or "validation service" splits technology, not business, so every feature needs all of them.
  • Too fine too soon. One context per entity ends in dozens of services that must call each other for every task. Start coarser; split when there is evidence.
  • One shared model. A canonical Customer for the whole company recreates the monolith.
  • Ignoring the organization. If one team must own five contexts, or two teams must edit one, the boundaries will not hold.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is a bounded context?

    Within a bounded context each term has a single clear meaning. The same word may mean something different in another context.
  2. 2. Catalog staff, warehouse staff and shipping staff all use the word 'product' but mean different things. What should you do?

    Diverging meanings of one word are a classic hint of a context boundary. A single giant model couples every team to every change.
  3. 3. In a context map, what does an anti-corruption layer do?

    An ACL protects the downstream model from being polluted by the upstream's concepts by translating at the boundary.
  4. 4. Which is a poor way to find service boundaries?

    Layer-based services split technology rather than business capability, so every feature must touch all of them.
  5. 5. Every context needs to refer to the same customer. What do they typically share?

    Contexts keep their own data shapes and refer to the same entity through a shared identifier.

Summary

  • Good service boundaries come from the business language, not from tables or technical layers.
  • A bounded context is a zone where one model and one vocabulary apply; it is the best first candidate for a service.
  • A context map records how contexts relate: customer/supplier, conformist, anti-corruption layer, shared kernel and published language.
  • Contexts share identifiers, not whole models; each stores the data it needs in its own shape.
  • Start with coarser contexts and split only when you have evidence.

Next: Decomposition Strategies.