https://2023.springio.net/sessions/the-aggregate-is-dead-long-live-the-aggregate/
SARA PELLEGRINI - AXONIQ
MILAN SAVIC - AXONIQ
DDD’s definition of Aggregate may seem somewhat confusing - “An aggregate is a cluster of associated objects that we treat as a unit for the purpose of data changes.” Okay, let’s try to clarify - “You should consider your aggregate as a unit of consistency in your Domain.”. That doesn’t help either. As a matter of fact, while modeling our systems, we tend to group together events related to the same domain concept; we tend to define groups based on the nouns we find inside our events’ name: saying “this is our aggregate!”. According to the aggregate definition, we should instead ignore these nouns, and put together the data that change together. Easier said than done: in the modeling phase it is easy to make mistakes trying to identify the boundaries of our aggregates based on this rule. If we opt for saving the state of our aggregate as a series of events, we are in big trouble - any (serious) refactoring of the aggregate structure becomes close to impossible. The reason for this trouble is that we have to make a decision in the design phase for which we cannot be lenient. We are basically married to this decision forever. Due to the aforementioned reasons (and many others), people struggle with the Aggregate pattern. Some even say it is unnecessary, we are one of those. Let’s see whether we can model our business constraints without aggregates. Could we be more relaxed when consistency is in question? Join us to discover how!
7. The aggregate
An aggregate is a cluster of associated objects
that we treat as a unit for the purpose of data changes.
From blue book - Domain-Driven Design: Tackling Complexity in the Heart of Software - Eric Evans
10. From theory to practice
Cluster the ENTITIES and VALUE OBJECTS into AGGREGATES and define
boundaries around each.
From blue book - Domain-Driven Design: Tackling Complexity in the Heart of Software - Eric Evans
11. From theory to practice
Cluster the ENTITIES and VALUE OBJECTS into AGGREGATES and define
boundaries around each.
Choose one ENTITY to be the root of each AGGREGATE, and control all access to
the objects inside the boundary through the root.
From blue book - Domain-Driven Design: Tackling Complexity in the Heart of Software - Eric Evans
12. From theory to practice
Cluster the ENTITIES and VALUE OBJECTS into AGGREGATES and define
boundaries around each.
Choose one ENTITY to be the root of each AGGREGATE, and control all access to
the objects inside the boundary through the root. Allow external objects to hold
reference to the root only.
From blue book - Domain-Driven Design: Tackling Complexity in the Heart of Software - Eric Evans
16. Event Storming
Education domain
1) A course cannot accept more than N students
2) N, the Course Capacity, can change any time
to any positive integer different from the current one
17. Event Storming
Education domain
1) A course cannot accept more than N students
2) N, the Course Capacity, can change any time
to any positive integer different from the current one
(even if the number of currently subscribed students is larger than the new value)
26. Story Telling
Storytelling is at the heart of human communication.
Storytelling does not require any special competence/skill.
27. Story Telling
Storytelling is at the heart of human communication.
Storytelling does not require any special competence/skill.
It helps break down the barriers between technicians and business experts.
29. The aggregate is the boundary of consistency
When a change to any object with the aggregate boundary is committed, all
invariants of the whole aggregate must be satisfied.
From blue book - Domain-Driven Design: Tackling Complexity in the Heart of Software - Eric Evans
35. Aggregate does not fit with Story Telling
It is not naturally part of the story: the story teller is forced to add this element
36. Aggregate does not fit with Story Telling
It is not naturally part of the story: the story teller is forced to add this element
Should business experts understand what a consistency boundary is?
37. Aggregate does not fit with Story Telling
It is not naturally part of the story: the story teller is forced to add this element
Should business experts understand what a consistency boundary is?
Leads back to the old modelling-first/data centric approach.
Moves the focus from the behavior to the model.
47. Event sourced COURSE aggregate
Course Created Event
Student Subscribed to Course
Event
Student Unsubscribed from
Course Event
Course Capacity Changed
Event
48. Event sourced COURSE aggregate
Course Created Event
Student Subscribed to Course
Event
Student Unsubscribed from
Course Event
Course Capacity Changed
Event
Subscribe Student to Course
Command
Unsubscribe Student to Course
Command
49. Event sourced COURSE aggregate
Course Created Event
Student Subscribed to Course
Event
Student Unsubscribed from
Course Event
Course Capacity Changed
Event
Subscribe Student to Course
Command
Unsubscribe Student to Course
Command
Update Course
Capacity Command
50. Event Storming of this problem:
Education domain
1) A course cannot accept more than N students
2) N, the Course Capacity, can change any time
to any positive integer different from the current one
(even if the number of currently subscribed students is larger than the new value)
3) The course title can change any time
to any title different from the current one
51. Event Storming of this problem:
Education domain
1) A course cannot accept more than N students
2) N, the Course Capacity, can change any time
to any positive integer different from the current one
(even if the number of currently subscribed students is larger than the new value)
3) The course title can change any time
to any title different from the current one
84. Event Storming of this problem:
Education domain
1) A course cannot accept more than N students
2) N, the Course Capacity, can change any time
to any positive integer different from the current one
(even if the number of currently subscribed students is larger than the new value)
3) The course title can change any time
to any title different from the current one
4) The student cannot join more than 10 courses
85. Event Storming of this problem:
Education domain
1) A course cannot accept more than N students
2) N, the Course Capacity, can change any time
to any positive integer different from the current one
(even if the number of currently subscribed students is larger than the new value)
3) The course title can change any time
to any title different from the current one
4) The student cannot join more than 10 courses
98. We need some form of synchronization...
Subscribe Student to
Course Command
99. Saga
We need some form of synchronization...
Subscribe Student to
Course Command
100. Saga
We need some form of synchronization...
Subscribe Student to
Course Command
Subscribe Student to
Course Command
Subscribe Student to
Course Command
Student
Course
1
1
101. Saga
We need some form of synchronization...
Subscribe Student to
Course Command
Subscribe Student to
Course Command
Subscribe Student to
Course Command
Student Subscribed
Course Event
Student Subscribed
Course Event
Student
Course
1
1
102. Saga
We need some form of synchronization...
Subscribe Student to
Course Command
Subscribe Student to
Course Command
Subscribe Student to
Course Command
Student Subscribed
Course Event
Student
Course
1
1
103. Saga
We need some form of synchronization...
Subscribe Student to
Course Command
Subscribe Student to
Course Command
Subscribe Student to
Course Command
Student Subscribed
Course Event
Student
Course
revert
1
1
2
104. Saga
We need some form of synchronization...
Subscribe Student to
Course Command
Subscribe Student to
Course Command
Subscribe Student to
Course Command
Student Subscribed
Course Event
Student
Course
revert
Unsubscribe Student
to Course Command
Student Unsubscribed
Course Event
Student
1
1
2
3
107. It's just a long transaction...
Long transaction are very common when the business rule spans bounded contexts.
108. It's just a long transaction...
Long transaction are very common when the business rule spans bounded contexts.
But when the rule spans multiple aggregates in the same bounded context?
109. It's just a long transaction...
Long transaction are very common when the business rule spans bounded contexts.
But when the rule spans multiple aggregates in the same bounded context?
Is this a necessary complexity or it can be avoided?
112. ● Does not fit the story telling
● Puts focus on model instead of behaviour
113. ● Does not fit the story telling
● Puts focus on model instead of behaviour
● Mixes technical and business aspects
114. ● Does not fit the story telling
● Puts focus on model instead of behaviour
● Mixes technical and business aspects
● Can cause unnecessary contention
115. ● Does not fit the story telling
● Puts focus on model instead of behaviour
● Mixes technical and business aspects
● Can cause unnecessary contention
● Can cause unnecessary complexity
116. ● Does not fit the story telling
● Puts focus on model instead of behaviour
● Mixes technical and business aspects
● Can cause unnecessary contention
● Can cause unnecessary complexity
● Hard to refactor
117. Is there a better way to implement the command model?
118. Is there a better way to implement the command model?
-
F
o
c
u
s
o
n
B
e
h
a
v
i
o
r
-
131. Event sourcing
Event sourcing provides a huge advantage because it decouples
the persistence from the model needed for taking the decision
The message handler can build on the fly any model needed for
taking the decision starting from the correct event stream
132. Event sourcing
Event sourcing provides a huge advantage because it decouples
the persistence from the model needed for taking the decision
The message handler can build on the fly any model needed for
making the decision starting from the correct event stream
174. Limited contention
Before the contention boundaries were those of the aggregate...
Now, the contention boundaries are those of the stream query
175. Limited contention
Before the contention boundaries were those of the aggregate...
Now, the contention boundaries are those of the stream query
176. Does not seem very artificial?
Student
Subscribed
to Course
Event
Student
Unsubscribed
from Course
Event
Subscribe
Student to
Course
Command
Unsubscribe
Student to
Course
Command
Course Course
Student
Subscribed
to Course
Event
Student
Unsubscribed
from Course
Event
Subscribe
Student to
Course
Command
Unsubscribe
Student to
Course
Command
Student Student
179. From the story-telling perspective
Student
Subscribed
to Course
Event
Student
Unsubscribed
from Course
Event
Subscribe
Student to
Course
Command
Unsubscribe
Student to
Course
Command
180. From the story-telling perspective
Student
Subscribed
to Course
Event
Student
Unsubscribed
from Course
Event
Subscribe
Student to
Course
Command
Unsubscribe
Student to
Course
Command
Decision Decision
191. Subscribe
Student to
Course
Command
Decision based on the following business rules:
- A course cannot accept more than N student
- The student cannot join more than 10 courses
Course
Created
Student
Created
….
Course
Renamed
Course
Capacity
Changed
….
Course: jdsj4 Student:gfhl3 ---
Course: jdsj4 Course: jdsj4
---
Pure Events
Tags
(Domain Identifiers)
192. Subscribe
Student to
Course
Command
Decision based on the following business rules:
- A course cannot accept more than N student
- The student cannot join more than 10 courses
Course
Created
Student
Created
….
Course
Renamed
Course
Capacity
Changed
….
Course: jdsj4 Student:gfhl3 ---
Course: jdsj4 Course: jdsj4
---
Pure Events
Tags
(Domain Identifiers)
193. Student
Subscribed
to Course
Event
Subscribe
Student to
Course
Command
Decision based on the following business rules:
- A course cannot accept more than N student
- The student cannot join more than 10 courses
Course
Created
Student
Created
….
Course
Renamed
Course
Capacity
Changed
….
Course: jdsj4 Student:gfhl3 ---
Course: jdsj4 Course: jdsj4
---
Pure Events
Tags
(Domain Identifiers)
Student
Subscribed
to Course
Course: jdsj4
Student: gfh3
199. Domain Identifier
KEY: VALUE
The concept in the domain The unique instance identifier
This is just an example,
They can be anything, as far as they are unique inside the bounded context
200. Student
Subscribed
to Course
Event
Subscribe
Student to
Course
Command
Decision based on the following business rules:
- A course cannot accept more than N students
- The student cannot join more than 10 courses
Course
Created
Student
Created
….
Course
Renamed
Course
Capacity
Changed
….
Course: jdsj4 Student:gfh3j ---
Course: jdsj4 Course: jdsj4
---
Pure Events
Tags
(Domain Identifiers)
Student
Subscribed
to Course
Course: jdsj4
Student: gfh3j
201. Student
Subscribed
to Course
Event
Subscribe
Student to
Course
Command
Decision based on the following business rules:
- A course cannot accept more than N students
- The student cannot join more than 10 courses
Course
Created
Student
Created
….
Course
Renamed
Course
Capacity
Changed
….
Course: jdsj4 Student:gfh3j ---
Course: jdsj4 Course: jdsj4
---
Pure Events
Tags
(Domain Identifiers)
Student
Subscribed
to Course
Course: jdsj4
Student: gfh3j
Course
Fully
Booked
Event
Course Fully
Booked
Course: jdsj4
207. Pure events
An event does NOT belong to an aggregate
An event is just a description of a fact that is important for the business
An event can be related to one or multiple domain concepts, addressed by their
Domain Identifiers
208. Pure events
An event does NOT belong to an aggregate
An event is just a description of a fact that is important for the business
An event can be related to one or multiple domain concepts, addressed by their
Domain Identifiers
209. Pure events
An event does NOT belong to an aggregate
An event is just a description of a fact that is important for the business
An event can be related to one or multiple domain concepts
addressed by their Domain Identifiers
211. Less complexity
No owner of the event
The business decision can easily involve several domain concepts
The decision is taken in one place
212. Less complexity
No owner of the event
The business decision can easily involve several domain concepts
The decision is taken in one place
213. Less complexity
No owner of the event
The business decision can easily involve several domain concepts
The decision is taken in one place
214. Saga
Subscribe Student to
Course Command
Subscribe Student to
Course Command
Subscribe Student to
Course Command
Student Subscribed
Course Event
Student
Course
revert
Unsubscribe Student
to Course Command
Student Unsubscribed
Course Event
Student
1
1
2
3
215. More flexibility
Event sourcing provides a huge advantage because it decouples the persistence
from the model needed for taking the decision
Refactoring
● Change the StreamQuery
● Add/Remove Domain Identifiers
216. More flexibility
Event sourcing provides a huge advantage because it decouples the persistence
from the model needed for taking the decision
Refactoring
● Change the StreamQuery
217. More flexibility
Event sourcing provides a huge advantage because it decouples the persistence
from the model needed for taking the decision
Refactoring
● Change the StreamQuery
● Add/Remove Domain Identifiers
221. Dynamic Consistency Boundary - aka DCB
● Perfect match with story telling
● Focus on behaviour instead of model
222. Dynamic Consistency Boundary - aka DCB
● Perfect match with story telling
● Focus on behaviour instead of model
● Less contention
223. Dynamic Consistency Boundary - aka DCB
● Perfect match with story telling
● Focus on behaviour instead of model
● Less contention
● Less complexity
224. Dynamic Consistency Boundary - aka DCB
● Perfect match with story telling
● Focus on behaviour instead of model
● Less contention
● Less complexity
● Simpler to refactor