DDD at Large Scale: Advanced Techniques
Complex domains call for structured modeling and a clear architecture so the system stays understandable and easy to develop. Separating responsibilities well makes systems easier to grasp and lets them evolve more efficiently.
The most intricate domains call for tools that can model bigger structures and concepts. To evolve a model well, developers have to understand it. Losing sight of where things belong happens easily, and exposing the meaning of and relationships between separate parts of the system gets hard. The biggest benefits of DDD can slip away fast. The model needs to be given structure, with a specific architecture introduced at the model level.
An architecture sets constraints that make the model more explicit and easier to read. The relevant business aspects have to be kept carefully apart so that each stays meaningful and comprehensible. What we aim for is a system that is easy to reason about and to build. We need tools that let us describe the system as a whole.
Responsibility Layers
This approach relies on a layered model, in which the system is split into layers. Every layer has a tightly scoped responsibility and is unaware of what sits above it, whereas higher layers may use the functions offered by those beneath them.
There are two main variants here:
- a stricter one, in which only the layer directly below may be used,
- a looser one, in which any lower layer may be used.
By definition, the Responsibility Layers model follows the second, more lenient variant, so every layer is free to use domain functions from any layer below it.
When applied at the system or application level, the layered model draws its lines of responsibility according to technical aspects. Here, by contrast, decisions stem purely from business considerations, with the aim of splitting the model into smaller pieces. Our focus is on highlighting business responsibilities and how they relate to one another. The layers also become a key part of the ubiquitous language.
Every business is naturally stratified, so which layers we pick depends on the specific case. Defining such layers up front would be hard; they emerge as the model evolves instead. As our understanding of the domain grows, we start to see that some concepts rest on others located lower down.
A few well-tested heuristics help us recognize that a model is layered.
- Linguistic Context - Talking about sales is impossible without a product or service, because the notion of sales rests on the product/service. A product, on the other hand, can be modeled without any awareness that sales exist.
- Evolutionary Context - Certain parts of the model change at a different rate and for different reasons than others. The concept of price will change less often than the discount model built on top of it.
- The storytelling context of layers conveys the priorities and realities of the domain. Choosing the layers is a purely business decision.
Let’s walk through a sample layered model for a transport company. Comparable layers appear in many other domains, and the first two in particular show up nearly everywhere.
Capabilities what we are really able to do. For a logistics company these are trucks, railway wagons, agreements with airports or ports, and access to traffic density data – every resource that makes it possible to move goods from point A to point B. Resources are unaware of what we use them for; they model capabilities. Our business won’t necessarily use every possibility. Transport schedules can be discussed without taking into account the actual loads these units carry.
Operations this layer covers what the business is currently and actually doing. A logistics company collects a package at one place and promises to deliver it to another. Naturally, this layer has to rely on trucks, cars or trains to make that happen, yet completing the whole process will take several means of transport before the package finally reaches the recipient. To track a shipment, we need to know which unit carries it – at this point we depend on the layer below and coordinate what it does.
Policies here we express what we want to achieve or what we aim to optimize – be it the safety of the shipment, the delivery cost, or the delivery time. These take the form of specifications that dictate how operations behave.
Commitments every sort of agreement and contract – in other words, whatever binds us to particular behaviors, such as delivering a package within three days.
Decision Support the layer in charge of choosing the right policy. A decision may stem from legal provisions in a client contract and can differ by period or even by individual client. For a key customer, we may prioritize building the relationship and getting the package there sooner, occasionally even at the cost of profit on that single order.
Each of these layers can form its own independent context or parts of one. In the decision layer we might apply strategies or machine learning, lean on existing data warehousing mechanisms, or use an existing rule engine. Notably, such a split does not have to stay within one bounded context.
The advantages of this approach really show in highly complex systems. Consciously separating what our business wants to achieve from what it is capable of keeps concepts from getting mixed up. It also helps keep confusion out of the management model of our shipping organization – for instance, mixing in the details of how particular means of transport are used, the fine points of process optimization, or the ways decisions are made. We handle these concerns on three separate levels:
- Usage - which resources we use and how they are coordinated,
- Optimization and Prioritization - which aspects of our operation we want to improve and give greater weight,
- Rationale - why we choose particular strategies under particular conditions.
During planning, these separate responsibilities get tangled more easily, resulting in a model that is hard to read, understand and evolve. What we want is to deliberately define dependencies and awareness within our operations. Such a separation not only makes roles and responsibilities clearer but also makes our model more stable.
Guidance
When we introduce changes – say, moving our focus from cost to customer relationships and targeting shorter delivery times – we have to make sure the modifications don’t accidentally impact unrelated parts of the system. Suppose, for instance, we choose to send trucks out earlier to speed up deliveries, even if they aren’t full. We then need to be sure this doesn’t trigger unexpected problems, such as GPS failures in our vehicles. Managing change this carefully means that an improvement in one area won’t cause trouble in another.
The layers sketch the outline of the story our system tells. The split enforced by bounded contexts keeps models from blurring and contaminating each other, but it doesn’t help us view the system as one coherent whole. In a large system, people will work independently on different levels.
Lacking guidance, teams will build separate solutions, making it hard to see how the individual pieces fit together. The large-scale structure has to evolve as part of the domain model. A poorly chosen structure is worse than having none. The right structure can only be discovered by understanding the domain and the problem deeply, and the practical path to that understanding is an iterative development process.
A common criticism is that the capability layer deals with things the business never asked for – potential abilities we possess but the business doesn’t use. Yet such a split frequently makes adding new features easier and quicker. When the capability layer is modeled in full isolation, we keep artificial limitations out of it. The operational layer simply makes use of only part of the capabilities on offer.
Knowledge Level
Relationships among objects can get highly intricate. They can even shift dynamically and stem from configuration. Organizations, for instance, consist of people and smaller organizations, each having defined roles and relationships with other units. Such structures can be extremely complex and – more importantly – can differ from one organization to the next, with various departments, names, permissions or policies.
The Knowledge Level is a specific case of the reflection pattern, which lets us capture evolving needs by making the system aware of itself. A given concept is split into two levels:
- The Base Level handles operational responsibilities
- Meta Level captures knowledge about behavior and structure.
The meta-model describes how the various employees and their relationships are arranged and which rules apply to them. It makes it possible to organize the data of the base model, defining how that data can be organized and manipulated. In short, the meta-model is the blueprint for organizing the base model.
In this way, it answers questions such as:
- A department may hold many positions
- A manager supervises the employees in their own department
- An employee belongs to a single department
These rules aren’t fixed; they can depend on numerous factors. The meta-model can be derived from configuration.
This sounds a lot like the layers discussed earlier, but they are different layers. What sets them apart is the direction of dependencies: the Knowledge Level relies on a two-way dependency between the base model and the meta-model.
You can clearly see this approach in the Party Archetype. The model of the structure is complex, so handling the relationships separately pays off. Each part can then evolve without impacting the other, and reusing pieces of the system becomes easier. That said, we’re not trying to build a one-size-fits-all solution. We want the system to stay flexible while keeping our attention on the crucial aspects of the domain.
Conclusion
These are not the only model architectures available, but they are excellent examples for appreciating how powerful such architectures are. Together with bounded context and distillation, these structures are ways of shaping models. Still, using them is optional.
Every model architecture has its own strengths, and these examples show how effective they become when used properly. Bounded context means drawing clear boundaries inside which a given model is defined and applicable. Distillation, by contrast, means refining a model and simplifying it down to its essence.
Introducing structures such as new layers or components can extend what a system can do, but it also adds complexity that might be unnecessary. The trick is to weigh the benefits of these structures against the complexity they cost. They should be brought in only when they deliver clear value and fit the domain’s goals. Keep in mind that piling more structures onto a system makes it more complex, and most of the time the effort simply doesn’t pay off.
Treating these structures as optional keeps our design flexible, so we can adapt to change without getting stuck in an excessively rigid model framework.