Catalog My learning Blog
System Design Roadmap: 100-Day Course for Engineers · Phase 10 · Lesson 93 · 4 min read

Domain-Driven Design for Architects

On this page
  1. Why models rot
  2. Strategic vs tactical DDD
  3. Ubiquitous language: one vocabulary in code and conversation
  4. Aggregates as consistency boundaries
  5. Entities vs value objects
  6. Domain events and when to publish them
  7. When DDD is worth it
  8. Check yourself

Why models rot

A system's code says Customer.Status = 3. Sales calls that a "lapsed account"; finance calls it "on hold"; support calls it "suspended". Requirements come in using one vocabulary and get translated into another, and every translation is a chance for a bug. Over years the model drifts away from how the business works, until changing anything requires an archaeologist. Domain-driven design (DDD), from Eric Evans' 2003 book, is a set of ideas for keeping the software model and the business aligned.

Strategic vs tactical DDD

DDD has two halves:

  • Strategic design: dividing a large domain into bounded contexts and mapping their relationships. Day 52 covered this; it's what shapes service boundaries.
  • Tactical design: how to model inside one bounded context. That's this lesson.

Ubiquitous language: one vocabulary in code and conversation

Within a bounded context, developers and domain experts agree on one vocabulary and use it everywhere: conversations, documents, class names, method names, database tables and events. If the experts say "an order is placed, then fulfilled", the code has Order.Place() and OrderFulfilled, not OrderManager.UpdateStatus(2). When the language changes, the code changes with it.

Aggregates as consistency boundaries

An aggregate is a cluster of objects treated as one unit for changes, with one aggregate root as the only entry point. An Order aggregate contains its order lines; outside code never edits a line directly, only through Order.AddLine() or Order.ChangeQuantity(), so the order can enforce its rules ("total can't exceed the customer's credit limit", "can't change a shipped order").

Rules for aggregates that matter architecturally:

  • One transaction changes one aggregate. Its invariants are always consistent after each transaction.
  • Keep aggregates small. A huge aggregate means contention and slow loads.
  • Reference other aggregates by id, not by object. Order holds CustomerId, not a Customer object.
  • Consistency across aggregates is eventual, coordinated with domain events.

The third and fourth points line up neatly with distributed systems: an aggregate is a natural unit for a single database transaction, a partition key (Day 43) or a shard key (Day 26).

An Order aggregate with its root and lines enforcing rules, referring to the Customer aggregate only by id, and publishing domain events.
An Order aggregate with its root and lines enforcing rules, referring to the Customer aggregate only by id, and publishing domain events.

Entities vs value objects

  • Entity: has an identity that persists while its attributes change. A customer is the same customer after changing address.
  • Value object: defined only by its values, and immutable. Money(129.00, "EUR"), an Address, a DateRange. Two equal values are interchangeable. Value objects put validation and behavior where they belong: Money can refuse to add EUR to USD.

Using value objects instead of primitives (decimal price, string currency) removes a whole class of bugs.

EF Core can persist this style of model without bending it. Use private setters and methods like Order.AddLine() instead of public setters, map collections through backing fields, and store value objects as owned types or, from EF Core 8, complex types, so Money becomes two columns in the order table rather than its own table. If every entity has public setters for everything, rules end up in services and the model is a set of data bags, the anemic domain model DDD warns against.

Domain events and when to publish them

A domain event records something meaningful that happened in the domain: OrderPlaced, PaymentCaptured. Aggregates raise them; other aggregates and bounded contexts react. They're the bridge from tactical DDD to event-driven architecture (Day 46), sagas (Day 49) and event sourcing (Day 47).

Timing matters. If the aggregate's event is sent to a message broker before the transaction commits, and the commit then fails, other services react to something that never happened. Collect events on the aggregate, save them to an outbox table in the same transaction (Day 50), and publish only after commit.

When DDD is worth it

DDD pays off in complex domains with real business rules and long-lived systems: insurance, logistics, finance, healthcare. For simple CRUD, the tactical patterns add ceremony. Strategic DDD, finding bounded contexts and a shared language, is useful almost everywhere.

Check yourself

The aggregate is the consistency boundary: its rules are guaranteed within a transaction. Spanning several makes transactions larger, slower and harder to distribute.

It keeps each aggregate small and independent: loading an order doesn't drag in the whole customer and product graphs, and changes to one aggregate can't silently modify another inside the same transaction. It also leaves the door open to storing aggregates in different databases or services later.

An entity has a lasting identity even as its data changes; a value object is defined only by its values and is immutable.

The event goes out before the transaction commits, so a failed commit leaves an event without an order. Store events in an outbox in the same transaction and publish after commit.