Architecture

Design Aggregates and Consistency Boundaries

Choose aggregate boundaries around invariants and make concurrent state changes safe.

An aggregate is a cluster of domain objects treated as one consistency boundary. The aggregate root controls changes that must preserve its invariants.

Start from invariants

Ask which facts must be true immediately after each operation. Keep the data needed to enforce those rules within one aggregate where practical. If a rule spans many independent records, consider whether it needs a reservation, workflow, or eventual-consistency design instead.

Keep aggregates small

Large aggregates create contention and make unrelated changes block one another. Prefer references by identifier to loading entire object graphs. Update another aggregate through its public operation or an asynchronous event rather than directly modifying its internal state.

Protect concurrency

Optimistic version checks can detect conflicting updates. Database transactions and unique constraints protect critical invariants under concurrent requests. A domain method alone cannot prevent two processes from making incompatible decisions at the same time.

Checklist

  • Define invariants before aggregate boundaries.
  • Keep aggregates focused and small.
  • Reference other aggregates by identity.
  • Use version checks or database constraints.
  • Design cross-aggregate workflows explicitly.
Share this page Help other developers discover this resource.