What is a DDD Aggregate?

The One Question To Haunt Everyone!

Thomas Ploch

Principal Engineer @

0. Outlook

0. Outlook

  1. Why do we need Aggregates?

  2. What is an Aggregate?

  3. How big should an Aggregate be?

1. Why do we need Aggregates?

1. Why do we need Aggregates?

It is difficult to guarantee the consistency of changes to objects in a model with complex associations. Invariants need to be maintained that apply to closely related groups of objects, not just discrete objects. Yet cautious locking schemes cause multiple users to interfere pointlessly with each other and make a system unusable.

Eric Evans; Domain-Driven Design: Tackling Complexity in the Heart of Software

1. Why do we need Aggregates?

Execution plan for a complex SQL query

1. Why do we need Aggregates?

Execution plan for a complex SQL query

IT'S FUN TO GET THERE,

BUT NOT FUN TO BE THERE!

1. Why do we need Aggregates?

We need Aggregates so that successful systems do not spin out of control due to the growing complexity of the models caused by scaling up operations and development.

We want successful systems to stay successful!

2. What is an Aggregate?

2. What is an Aggregate?

An Aggregate is a cluster of associated objects that we treat as a unit for the purpose of data changes. Each Aggregate has a root and a boundary. The boundary defines what is inside the Aggregate. The root is a single, specific Entity contained in the Aggregate.

Eric Evans; Domain-Driven Design: Tackling Complexity in the Heart of Software

2. What is an Aggregate?

The Aggregate boundary is a consistency boundary! That means that everything within this boundary should be immediately consistent whenever data changes occur. Preferably in an atomic operation, i.e. an ACID transaction.

The Aggregate boundary

2. What is an Aggregate?

All data changes are handled through the Aggregate ROOT, but the ROOT itself can delegate capabilities to other objects if needed.

The Aggregate boundary is a consistency boundary! That means that everything within this boundary should be immediately consistent whenever data changes occur. Preferably in an atomic operation, i.e. an ACID transaction.

Within the Aggregate Boundary Entities and Value Objects are allowed have references to each other, but they are only unique within the boundary.

References to Aggregate internals from the outer world are strictly forbidden!

Other parts of the model are allowed to hold references to the ROOT

Account
ENTITY
ROOT

Status
Value Object

Email
Value Object

Token
ENTITY

The Aggregate boundary

Outer
World

Invariant

Invariant

Invariants are rules that need to be satisfied at all times, they are an integral part of the consistency guarantees.

2. What is an Aggregate?

<?php
// Examples violating the principles
// We get an Account Aggregate somewhere
$account = $repository->get();

// Reference given to the outer world
$token = $account->getToken();

// Data changes not guarded by the root
$token->invalidate();

// Invariant checked outside the Aggregate
$challenge = Challenge::fromRequest($request);
if (!$account->getToken()->confirm($challenge)) {
    throw new Exception('Challenge invalid.')
}

2. What is an Aggregate?

<?php
// A better approach
// We get an Account Aggregate somewhere
$account = $repository->get();

// Data changes handled through the root
$account->invalidateToken();

// Give out immutable values instead
$readOnlyToken = $account->getReadOnlyToken();

// Invariant checked within Aggregate
$challenge = Challenge::fromRequest($request);
$account->confirm($challenge);
// throws Domain Exception

2. What is an Aggregate?

...a unit of consistency
and concurrency!

An Aggregate is...

2. What is an Aggregate?

Image attribution: Cyberguru, CC BY-SA 4.0, via Wikimedia Commons

2. What is an Aggregate?

Image attribution: Cyberguru, CC BY-SA 4.0, via Wikimedia Commons

Identical twins

Dániel Tarr & Bence-László Tarr

It's impossible to determine who is who just by the information we have here.

2. What is an Aggregate?

Account
ENTITY
ROOT

Status
Value Object

Email
Value Object

Token
ENTITY

ID
Value
Object

Account
ENTITY
ROOT

Status
Value Object

Email
Value Object

Token
ENTITY

==

Aggregates are distinct.
So they need to carry some kind of identity!

ID
Value
Object

2. What is an Aggregate?

For Aggregates to process data changes...

...they have to exist first!

...for the purpose of data changes.

Eric Evans; Domain-Driven Design: Tackling Complexity in the Heart of Software

2. What is an Aggregate?

  • Contract has been signed
  • Customer has registered
  • Car has been produced
  • Contract has expired
  • Partner company liquidated
  • Car has been shredded

Aggregates represent life-cycles

Account
ENTITY
ROOT

Status
Value Object

Token
ENTITY

Email
Value Object

During its lifetime the Aggregate processes relevant changes that make the model useful.

Account
ENTITY
ROOT

Genesis

Account
ENTITY
ROOT

The End

Time

2. What is an Aggregate?

...a distinct life-cycle and
has an identity!

An Aggregates is...

2. What is an Aggregate?

...apply to closely related groups of objects, not just discrete objects.

Eric Evans; Domain-Driven Design: Tackling Complexity in the Heart of Software

Closely related things are normally also used together!

You probably keep your knifes, forks and spoons in the same drawer, right?

2. What is an Aggregate?

Database A

Database B

Account
ENTITY

Token
ENTITY

The Aggregate can only be reconstituted by querying all shards!

We have put our forks and spoons into different cupboards!

2. What is an Aggregate?

Account Microservice

Token Microservice

2. What is an Aggregate?

Account Microservice

Token Microservice

Now we have put our forks and spoons onto different PLANETS!

2. What is an Aggregate?

Database Shard A

Database Shard B

The cutlery is neatly organised in our drawers!

2. What is an Aggregate?

...a unit of distribution!

An Aggregates is...

2. What is an Aggregate?

RECAP: Aggregates are...

  • part of the domain and its language
    Aggregates represent concepts from the Ubiquitous Language, and if not they should be introduced.
     
  • artificial boundaries around closely related models
    These boundaries partition the domain model into closely related groups. And often we have to invent them!
     
  • life-cycles with an identity
    Aggregates are "born" and "end" at some point, going through a series of data changes on its way.
     
  • units of consistency
    Aggregates ensure the consistency of changes for the domain concepts that are part of the domain.
     
  • units of concurrency
    One of the major drivers is managing concurrent access in large and complex software systems.
     
  • units of distribution
    The parts of Aggregates should be kept together closely since they are interacting very often.

2. What is an Aggregate?

But what if I need two Aggregates?

How to keep two units of consistency consistent?

2. What is an Aggregate?

Customer Registration

Bounded Context

Account
ENTITY
ROOT

Status
Value Object

Email
Value Object

Token
ENTITY

Whenever a customer has opened an account, an entry in our CRM module should be created.

Account
ENTITY
ROOT

Contact
Value Object

Email
Value Object

CRM

Bounded Context

2. What is an Aggregate?

Any rule that spans Aggregates will not be expected to be up-to-date at all times. Through event processing, batch processing, or other update mechanisms, other dependencies can be resolved within some specified time.

Eric Evans; Domain-Driven Design: Tackling Complexity in the Heart of Software

Communication between Aggregates is eventually consistent!

3. How big should an Aggregate be?

3. How big should an Aggregate be?

As big as necessary

and as small as possible.

Someone somewhere I unfortunately can't remember. Please let me know if this quote is from you!

Domain-Driven Design often means deciding on what is inside and what is outside of boundaries and how these interact, using the knowledge of domain experts and heuristics to guide us.

3. How big should an Aggregate be?

Thank You!

Thank You!

We are hiring @

1. Why do we need Aggregates?

Enforcing

invariants

Guaranteeing
consistency

Concurrent Access

Group of closely related domain objects

The One Question To Haunt Everyone: What is a DDD Aggregate @ #DDDFoundations '22

By Thomas Ploch

The One Question To Haunt Everyone: What is a DDD Aggregate @ #DDDFoundations '22

There is one question that I am getting asked at almost every conference or meetup: What is and what isn't an Aggregate? How do we design an Aggregate? To be honest, it's not an easy answer, even for experienced practitioners. In this session I am summarizing the current state of affairs regarding Aggregate design in the Domain-Driven Design community.

  • 1,730