Domain-Driven Design (DDD)
Strategic DDD (bounded contexts, context maps, subdomains) and tactical DDD (aggregates, entities, value objects, domain events), and how DDD drives service boundaries.
Intent
Domain-Driven Design (Eric Evans, 2003) aligns software structure with the business domain through a shared ubiquitous language and explicit model boundaries.
Strategic design
| Concept | Meaning |
|---|---|
| Subdomain | Part of the business: core (differentiating), supporting, generic |
| Bounded context | A boundary within which a model and its language are consistent |
| Context map | Relationships between contexts: customer/supplier, conformist, anti-corruption layer, shared kernel, open host service, published language |
| Ubiquitous language | Terms shared by domain experts and code inside a context |
Example: "Product" in Catalog (descriptions, media), Pricing (price books), Inventory (stock units) and Shipping (weight, dimensions) are different models in different bounded contexts.
flowchart LR CAT[Catalog context] -- published language: ProductPublished --> SRCH[Search context] ORD[Ordering context] -- customer/supplier --> PAY[Payments context] ORD -- ACL --> ERP[Legacy ERP context] INV[Inventory context] -- events --> ORD
Tactical design
| Building block | Description |
|---|---|
| Entity | Identity that persists over time (Order) |
| Value object | Immutable, defined by attributes (Money, Address) |
| Aggregate | Consistency boundary with a root; transactions modify one aggregate |
| Domain event | Something that happened in the domain |
| Repository | Loads/saves aggregates |
| Domain service | Logic that does not fit an entity |
Java example: aggregate with invariants
public class Order { // aggregate root
private final OrderId id;
private final List<OrderLine> lines = new ArrayList<>();
private OrderStatus status = OrderStatus.DRAFT;
private final List<DomainEvent> events = new ArrayList<>();
public void addLine(Sku sku, int qty, Money unitPrice) {
if (status != OrderStatus.DRAFT) throw new IllegalStateException("Order is not editable");
if (qty <= 0) throw new IllegalArgumentException("Quantity must be positive");
lines.add(new OrderLine(sku, qty, unitPrice));
}
public void place() {
if (lines.isEmpty()) throw new IllegalStateException("Empty order");
status = OrderStatus.PLACED;
events.add(new OrderPlaced(id, total()));
}
public Money total() { return lines.stream().map(OrderLine::subtotal).reduce(Money.ZERO, Money::add); }
}
public record Money(BigDecimal amount, Currency currency) { // value object
public static final Money ZERO = new Money(BigDecimal.ZERO, Currency.getInstance("EUR"));
public Money add(Money o) {
if (!currency.equals(o.currency)) throw new IllegalArgumentException("Currency mismatch");
return new Money(amount.add(o.amount), currency);
}
}Discovering boundaries: Event Storming
Domain experts and engineers map business events on a timeline (orange stickies), then commands, actors, policies and aggregates; clusters of events reveal bounded contexts. Useful before splitting a monolith.
Pros and cons
| Pros | Cons |
|---|---|
| Boundaries that match the business; fewer cross-service changes | Requires domain expert time |
| Shared language reduces misunderstanding | Tactical patterns can be over-applied to simple CRUD |
| Natural guide for microservice/module boundaries | Steep learning curve |
Interview questions
How big should an aggregate be?
As small as possible while still protecting invariants that must be consistent within one transaction. Reference other aggregates by ID and use eventual consistency between aggregates.
What is an anti-corruption layer?
A translation layer that protects your model from another context's model (often legacy), converting its concepts into your ubiquitous language so its design does not leak into yours.