What it is
Domain-Driven Design (DDD) is an approach to software for complex business domains, introduced by Eric Evans in 2003. It has two halves:
- Strategic design decides where the boundaries go: which parts of the business get their own model (bounded contexts), what each part calls things (ubiquitous language) and how the parts relate (context map).
- Tactical design gives building blocks for the model inside one context: entities, value objects, aggregates, domain events, repositories and domain services.
Why it exists
As a system grows, the business vocabulary splits without anyone noticing. "Product", "Customer" or "Order" mean different things to sales, the warehouse and finance. A single model that tries to serve all of them grows dozens of fields and conditionals, and every change needs every team's agreement. DDD makes those different meanings explicit and gives each its own model, so teams can change their part without negotiating with everyone else.
How it works
Ubiquitous language. Within a context, developers and domain experts use the same terms in conversation, documentation and code. If the experts say "an order is placed", the method is Place(), not SetStatus(2). When the language gets awkward, that is a modelling signal.
Bounded contexts. A bounded context is the boundary inside which one model and one language are consistent. Sales can have a Product with a name and price, Shipping a StockItem with weight and bin locations, Billing an InvoiceLine with a VAT code. They refer to the same thing by a shared identifier (here the SKU) and share nothing else. A context owns its data; others reach it through its API or its events. A context is a model boundary, not necessarily a deployable: a module of a modular monolith works as well as a separate service.
Aggregates. An aggregate is a cluster of objects (an Order with its OrderLines) treated as one unit for changes. All changes go through the aggregate root, which enforces the invariants ("at most 50 lines", "total within the credit limit"). The rules of thumb, from Vaughn Vernon's Effective Aggregate Design:
- protect true invariants inside the aggregate boundary;
- design small aggregates;
- reference other aggregates by identity only;
- change one aggregate per transaction, and use eventual consistency (domain events) outside it.
In .NET this usually means an entity with private setters and behaviour methods, persisted with EF Core: one SaveChangesAsync() per command, with a concurrency token ([Timestamp]/RowVersion on SQL Server, xmin on PostgreSQL via Npgsql) so two concurrent edits of the same aggregate cannot both win.
public sealed class Order
{
private readonly List<OrderLine> _lines = new();
public OrderId Id { get; private set; }
public CustomerId CustomerId { get; private set; } // reference by id
public Money Total => _lines.Aggregate(Money.Zero, (sum, l) => sum + l.Amount);
public Result AddLine(Sku sku, int quantity, Money price, Money creditLimit)
{
if (_lines.Count >= 50) return Result.Fail("An order has at most 50 lines.");
var amount = price * quantity;
if (Total + amount > creditLimit) return Result.Fail("Credit limit exceeded.");
_lines.Add(new OrderLine(sku, quantity, amount));
return Result.Ok();
}
}
Domain events. When something meaningful happens (OrderPlaced), the aggregate records an event. After the transaction commits, the event is published (reliably, with a transactional outbox) and other aggregates or contexts react in their own transactions.
Context map. The map names how contexts relate. Upstream/downstream: the downstream depends on the upstream's model or events. Patterns on the map include customer/supplier (the upstream takes the downstream's needs into account), conformist (the downstream adopts the upstream model as is), anti-corruption layer (the downstream translates the upstream model into its own), open host service and published language (a stable, documented protocol for many consumers), shared kernel (a small shared model both sides must agree on) and separate ways (no integration at all).
Subdomains. The core domain is what differentiates the business; supporting subdomains are specific but not differentiating; generic subdomains are solved problems (identity, invoicing, email). Rich modelling belongs in the core.
When to use it
- The domain logic is genuinely complex: many rules, exceptions, workflows, and they change often.
- Several teams work on one system and keep stepping on each other's models.
- You are planning service boundaries: bounded contexts are the best-known heuristic for where to split.
- Domain experts are available to talk to; the language comes from them.
When not to
- CRUD over a few tables with little behaviour: a rich domain model adds layers with nothing to protect.
- A small team on a small system where one model is still understood by everyone.
- Generic subdomains: buy them or keep them simple.
- Without access to domain experts, the "ubiquitous language" becomes developers' guesses with extra ceremony.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Separate models per context | Teams change independently; each model stays small | Translation at the boundaries, duplicated data (by SKU/id) |
| Small aggregates | Parallel commits, less contention, simpler loading | Cross-aggregate rules become eventually consistent policies |
| Large aggregates | One transaction can enforce more rules | Lock contention, concurrency conflicts, slow loads |
| Domain events between contexts | Temporal decoupling, no distributed transactions | Eventual consistency, harder debugging, event versioning |
| Anti-corruption layer | Your model survives upstream changes | One more component to build and maintain |
Alternatives
- Transaction script / CRUD: procedures over tables. Fast to build, fine while rules are few.
- Data-centric "canonical model": one enterprise-wide schema. Looks consistent, couples everyone.
- Layered or clean architecture without DDD: organises code technically; does not by itself find business boundaries.
- Team Topologies / Conway-driven boundaries: complementary; aligns teams with the contexts DDD identifies.
Failure modes
- The god aggregate. Making
Customercontain all orders turns every command into contention for one lock or version. The lesson's simulation shows it: ~40 commands/s ceiling, queues, timeouts. - Anaemic model. Entities with only getters/setters and all rules in services: the vocabulary of DDD without the encapsulation.
- Shared database between contexts. Contexts that read each other's tables are one model again, just harder to see.
- Context per entity. Splitting too fine ("Address service") creates chatty dependencies; contexts follow business capabilities, not nouns.
- Leaky upstream models. Using an external provider's types in your domain makes every provider change your change.
- Distributed transactions across contexts. Two-phase commit between services reintroduces tight coupling and availability risk.
In production
Start with event storming or domain-expert interviews to find language and boundaries; draw the context map and keep it in the repo next to the code. In .NET, a context is often a project or a set of projects in a modular monolith with its own DbContext and schema, exposing a small public contract (commands, queries, integration events). Use concurrency tokens on aggregates, publish integration events through an outbox, and version event contracts (OrderPlaced v1, v2) like public APIs. Revisit boundaries as the business changes; a wrong boundary is cheaper to move inside a monolith than across services.
Common misconceptions
- "DDD means microservices." Bounded contexts are model boundaries. They map well to services, but a modular monolith with one module per context is often the better start.
- "DDD is the tactical patterns." Repositories and value objects are the smaller part. The big wins come from boundaries and language.
- "An aggregate is any object graph you load together." It is the unit of consistency and concurrency, sized by the invariants it must protect.
- "Every context needs the full toolkit." Generic and supporting subdomains usually don't.
Interview angle
When splitting a system, name the bounded contexts and justify them by business capability and language ("in Shipping a product is a stock item with weight and bins"). Show you know aggregates are consistency boundaries: one per transaction, referenced by id, kept small, with cross-aggregate rules handled by events and eventual consistency. Mention optimistic concurrency, the outbox for publishing events, and an anti-corruption layer for third parties. Saying when you would not use tactical DDD scores points.
Model assumptions
Most of this lesson is a scripted illustration: the departments, contexts, event bus, anti-corruption layer and payment provider are scenery, and their packets follow a fixed script rather than a simulation. Two steps (Too big, Keep small) and the challenge run on the simulation engine: Sales receives Poisson-distributed commands (all writes); the Sales service has 32 workers and 5 ms of work per command; the aggregate lock is modelled as a store that runs one command at a time (1 connection, 25 ms mean, lognormal cv 0.3), while small Order aggregates commit in parallel (12 connections, 15 ms). Commands that wait more than 500 ms fail. Real lock and optimistic-concurrency behaviour differs in detail (conflicts and retries instead of a queue); the numbers are illustrative, not benchmarks.