# Integrating Bounded Contexts
> **In one sentence:** bounded contexts evolve on their own but still have to talk to each other,
> and how the *teams* relate decides how the *systems* should integrate.

The touchpoints between contexts are **contracts**. Each side speaks its own language, so you have
to decide whose language the contract uses and who adapts when it changes. DDD groups the answers
by the kind of team relationship.

## Cooperation

The teams communicate well, or their success depends on each other.

### Partnership

Integration is worked out as it comes up and in both directions. One team changes an API, tells
the other, and the other adapts without friction. Nobody dictates. Problems get fixed together.

- *Needs:* trust, frequent syncing and continuous integration of both sides' changes.
- *Weak spot:* teams spread across locations or time zones, where syncing is hard.

### Shared kernel

A small piece of model is **shared** by several contexts, for example a custom permissions model
that all of them must apply identically. A change to it affects everyone at once.

- **Keep it tiny**, ideally just the contracts and data structures that cross the boundary.
- **Implement it as one source of truth**: the same files in a monorepo, or one shared library.
  Every change triggers integration tests in **all** dependent contexts.
- **Use it only when duplicating would cost more than coordinating.** That's most likely for
  fast-changing core logic.
- **It bends the one-team-per-context rule**, so it needs a real justification. Typical reasons:
  teams that can't work as partners (because of distance or politics), a temporary step while
  breaking up a legacy system, or one team that wants a clear, explicit line between its own contexts.

## Customer–supplier

One context (the **upstream** supplier) provides a service, and another (the **downstream**
customer) uses it. Each can succeed without the other, so one side usually has more power over the
contract.

### Conformist

The upstream side holds the power and offers a take-it-or-leave-it contract. The downstream side
**adopts the upstream model as it is**. That's reasonable when the upstream model is an industry
standard or simply good enough.

### Anticorruption layer (ACL)

The upstream side still holds the power, but the downstream side **won't let the foreign model
leak in**. It adds a translation layer that turns the upstream model into its own. Use it when:

- the downstream context is **core** and needs a clean model of its own,
- the upstream model is **awkward or messy**, which is common with legacy systems (adopt a mess
  and you become one), or
- the upstream contract **changes often**, so only the translator has to change.

### Open-host service (OHS)

The power sits with the **consumers**, and the supplier wants to serve them well. It separates its
internal model from a **public, consumer-friendly protocol** called the **published language**.

- The internal model can change freely as long as it still maps to the published language.
- Several versions of the published language can run side by side, so consumers migrate when they're ready.
- It's the mirror image of an ACL: here the **supplier** does the translating.

## Separate ways

Sometimes the cheapest option is **not to integrate at all** and to let each context build its own version.

- **Collaboration is too costly**, because of organization size, politics or poor communication.
- **The capability is generic and easy to plug in**, like a logging library. Each context just uses it directly.
- **The models are too different** to conform to, and translating would cost more than duplicating.

> **Avoid separate ways for core subdomains.** Building the most valuable capability twice
> contradicts the reason it's core.

## Context map

A **context map** is a diagram of all bounded contexts and the patterns between them. It shows:

- **High-level design:** the components and the models they own.
- **Team dynamics:** who works closely together and who keeps their distance (ACL, separate ways).
- **Organizational smells:** if every consumer of one team builds an ACL, or all the separate-ways
  links cluster around one team, ask why.

*Maintenance:* start one on day one and keep it up to date as a shared job, with each team
updating its own links. It can live as code (for example with Context Mapper).
*Limit:* when a context spans several subdomains, one pair of contexts may use more than one
pattern, which makes the map harder to read.

## Pattern cheat sheet

| Pattern | Power sits with | Who adapts or translates | Good fit when |
|---|---|---|---|
| Partnership | Shared | Both, as needed | Teams sync often and trust each other |
| Shared kernel | Shared | Nobody (same code) | Duplicating costs more than coordinating |
| Conformist | Upstream | Downstream adopts as is | The upstream model is standard or good enough |
| Anticorruption layer | Upstream | Downstream translates | Protecting a core model or a messy upstream |
| Open-host service | Downstream | Upstream translates | Many consumers, versioned public API |
| Separate ways | n/a | n/a | Duplicating is cheaper (never for core) |

## Check yourself

1. Which pattern should never be used for a core subdomain?
2. Which kind of downstream subdomain most needs an ACL?
3. Which kind of upstream subdomain is likely to offer an open-host service?
4. Which pattern, in a sense, breaks ownership boundaries?

<details><summary>Answers</summary>

1. Separate ways.
2. Core.
3. Any type can. What matters is wanting to protect consumers.
4. Shared kernel.

</details>
