All academies
Roadmap
Level 05Foundations 35 min

Domain-Driven Design (DDD)

Bounded contexts, ubiquitous language, and aggregates — the strategic tools for finding service boundaries.

Why DDD matters for microservices

The hardest question in microservices is not 'how do services talk' but 'where do we draw the lines'. Domain-Driven Design, from Eric Evans, gives us the vocabulary and tools to answer it.

DDD's central insight: large domains cannot have one unified model. The word 'Product' means something different to Catalog (descriptions, images), Inventory (stock levels, locations), and Shipping (weight, dimensions). Forcing one Product model onto all three creates a tangled god-object.

Bounded contexts

A bounded context is a boundary within which a domain model is consistent and unambiguous. Inside the Ordering context, 'Order' has exactly one meaning. Another context may use the same word differently — and that's fine, because translation happens explicitly at the boundary.

Bounded contexts are the single best candidate for microservice boundaries. One service per bounded context is the default heuristic.

E-commerce domain
Catalog context · Ordering context · Shipping context
Each context → one candidate microservice

Ubiquitous language

Within each context, developers and domain experts share one precise vocabulary — the ubiquitous language — used in conversation, documentation, and code. If the business says 'reserve stock' and the code says 'decrementCounter', misunderstandings breed bugs.

Aggregates and tactical patterns

An aggregate is a cluster of domain objects treated as a single consistency unit, with one entity as the aggregate root. All changes go through the root, and a transaction should modify only one aggregate.

This maps beautifully to microservices: an aggregate is the natural unit of strong consistency inside a service, while consistency between aggregates (and services) is eventual, coordinated by domain events.

Order aggregate root
class Order {                    // aggregate root
  private lines: OrderLine[] = [];
  private status: OrderStatus = "DRAFT";

  addLine(product: ProductRef, qty: number) {
    if (this.status !== "DRAFT") throw new DomainError("Order is locked");
    this.lines.push(new OrderLine(product, qty));
  }

  place(): OrderPlaced {          // returns a domain event
    if (this.lines.length === 0) throw new DomainError("Empty order");
    this.status = "PLACED";
    return new OrderPlaced(this.id, this.total());
  }
}

Key takeaways

  • Large domains need multiple models — one unified model becomes a god-object.
  • A bounded context is a boundary where one model is consistent; it is the default microservice boundary.
  • Ubiquitous language aligns code with how domain experts actually speak.
  • Aggregates are units of strong consistency; between aggregates, use events and eventual consistency.

Knowledge check

Q1Multiple choice

What is a bounded context?

Q2System design

Catalog needs product descriptions; Shipping needs product weight. According to DDD, how should 'Product' be modeled?

Q3Multiple choice

Why should a transaction modify only one aggregate?

Answer all questions to submit