Building large systems following DDD leads to decomposition of the domain into subdomains and creation of multiple bounded contexts in the solution architecture.
While this is a good thing and reduces complexity and cognitive load of the single contexts, you will need different solutions for the inter-context application flow of your over-all system.
From time to time you may find yourself puzzled with the different possibilities these approaches have to offer.
Having used them for quite some time now, we stumbled upon some interesting integration questions implementing bounded contexts using Axon Framework and CQRS/ES patterns.
Especially, the design of the overall architecture offers trade-offs in combining of onion, hexagonal and other styles. In this talk we demonstrate identified patterns, share some experience in integration and discuss the decisions made during the implementation of the multiple bounded context solutions using Axon Framework. In doing so, we provide examples and propose best practices we identified in our projects.
1. Beyond DDD 101 A practice report on building multi
bounded context CQRS/ES applications
Jan Galinski & Simon Zambrovski
Amsterdam, 23.09.2022
1
2. Who we are
Jan Galinski
Simon Zambrovski
23.09.2022, AxonIQCon 2022 2
Consultants @ holisticon
Backend developers, software architects
DDD, CQRS/ES, Axon Framework, Spring, Kotlin, Java
3. What is Holisticon
• IT Consulting
• Hamburg, Hannover, Kiel
• Cool crew
• Best Employer 2022
in Hamburg
• We are hiring!
23.09.2022, AxonIQCon 2022 3
BESTE BERATUNG – BESTE SOFTWARE
4. Motivation
• We love architecture
• DDD
• CQRS
• Event Sourcing
• Large-scale applications running on JVM
• We love Axon Framework
• We want to share our experience
23.09.2022, AxonIQCon 2022 4
https://www.pexels.com/de-de/foto/verschiedene-puzzlespiele-1586951/
5. Foundation – Theory - DDD
• Set of principles for system implementation
• Requirement Engineering
• Domain Modeling
• Strategic Design
• Bounded Context identification
• Context Mapping
• Design
• Ubiquitous Language
• Design Patterns
23.09.2022, AxonIQCon 2022 5
https://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215
6. Foundation – Theory – DDD Patterns
• Modeling of Bounded Context relationships (for Microservices)
• Separate Ways
• Upstream/Downstream
• Open Host Service + Published Language
• Anti Corruption Layer
• Tactical Design
• Value Object, Entity, Aggregate, Aggregate Root
• Domain Service, Factory, Repository
• Domain Event
• Saga Pattern
23.09.2022, AxonIQCon 2022 6
7. Foundation – Theory – CQRS/ES
• Command Query Responsibility Segregation Pattern
• Reduce complexity of Domain model
• Separate Command model from Query model (optimized)
• Scale-out independently
• Event Sourcing Persistence Strategy
• Store Domain Events (versus store State)
• Derive State from Domain Event stream
• CQRS/ES
• Use Domain Events to communicate between Command and Query models
• Allow late stream consumption (stream replay)
23.09.2022, AxonIQCon 2022 7
8. Domain
Foundation - Onion Architecture
Driving
(primary)
Adapter
Driven
(secondary)
Adapter
Domain
Model
Ring
Port Port
Application Service Ring
Infrastructure Ring
23.09.2022, AxonIQCon 2022 8
9. Axon Framework 101
• FOSS framework in Java for building DDD CQRS(/ES) systems
• Explicit message concept
• 3 buses for decoupling
• Command -> Addressed intent of a change, may fail, unicast
• Event -> Fact, broadcast
• Query -> Request for data, has result, multicast
• Domain Model Segregation (implements CQRS)
• Event Sourcing implementation (different Event Stores)
• Separation of business logic from configuration
23.09.2022, AxonIQCon 2022 9
https://developer.axoniq.io/download
10. Possible Architecture using Axon Framework
23.09.2022, AxonIQCon 2022 10
https://docs.axoniq.io/reference-guide/
11. System evolution in single context
• Build components using buses for communication
• Start with modulith
• Distribute along buses
• Scale out projections
• Build new projections later
• Change infrastructure
23.09.2022, AxonIQCon 2022 11
12. Domain Core
Onion Architecture with Axon Framework
REST
Adapter
Query Model
Command Model
Command
Gateway
Query
Gateway
Projection
Repository
Aggregate
Repository
Query
Bus
Comma
nd Bus
Event
Bus
Pivotal
Event
Bus
Pivotal
Event
Bus
Domain
Event
Bus
Domain
Comma
nd Bus
Query
Bus
Saga
23.09.2022, AxonIQCon 2022 12
14. Domain Core
Onion Architecture with Axon Framework
REST
Adapter
Query Model
Command Model
Command
Gateway
Query
Gateway
Projection
Repository
Aggregate
Repository
Query
Bus
Comma
nd Bus
Event
Bus
Pivotal
Event
Bus
Pivotal
Event
Bus
Domain
Event
Bus
Domain
Comma
nd Bus
Query
Bus
Saga
23.09.2022, AxonIQCon 2022 14
15. Simplified context building block
Query Model
Command Model
Query
Bus
Comma
nd Bus
Event
Bus
Event
Bus
Comma
nd Bus
Query
Bus
Saga
23.09.2022, AxonIQCon 2022 15
17. Q
Master Data Query
Q
S
D U
23.09.2022, AxonIQCon 2022 17
„You know some things that I need to know, please tell me!“
Query
18. Master Data Query
Q
S
Q
D U
23.09.2022, AxonIQCon 2022 18
Query
Benefits
• Simple (~ GET)
• No redundancy (GDPR)
• Encapsulate complex business
logic
Challenges
• Runtime dependency
• Trap: Building specialized
projections for other teams
• Outdated data, caching
19. Trigger a query
C
U D
23.09.2022, AxonIQCon 2022 19
Q Q
„I made some changes … ask me if you want more details.“
Query
C‘
20. Trigger a query
C
U D
23.09.2022, AxonIQCon 2022 20
Q Q
Query
Benefits
•„Cache invalidation“
Challenges
•Runtime dependency*
•Load (query avalanche)
22. Query Aggregator
C
Q
U D
23.09.2022, AxonIQCon 2022 22
Query
Benefits
• Decoupling
• Build complex projections
from other contexts
Challenges
• Event Payload definition
• Payload interpretation
23. Reacting to Fact
C
S
C
U D
23.09.2022, AxonIQCon 2022 23
Command
„I changed some things … deal with it.“
C‘ C‘
24. Reacting to Fact
C
S
C
U D
23.09.2022, AxonIQCon 2022 24
Command
Benefits
• Simple
• Decoupled
• Event Driven
Challenges
• Downstream Command model
must follow
• Command-Event-Command
bridge
26. Downstream Saga
C
S
C
D U
23.09.2022, AxonIQCon 2022 26
Command
Benefits
• Build for failure
• Delegate sub processes
Challenges
• Compensation
• Complex for more than
one context
28. External Orchestrator
C
S
C
D U
U
23.09.2022, AxonIQCon 2022 28
Command
Benefits
• Build for failure
• Separate ways
• Business process
Challenges
• Compensation
• Complex for more than one
context
29. Key Takeaways
• Domain Driven Design
• Start with Strategic Design
• Do proper Context Mapping – Relationships!
• CQRS/ES
• Use Command / Event / Query abstractions („Events only“ is not enough)
• Use Axon Framework if working on JVM
• Practical
• Start with a modulith
• Build evolutionary
• Distribute on demand
23.09.2022, AxonIQCon 2022 29
30. Thank you... Questions?
• Holisticon AG
• https://holisticon.de/
• Jan
• https://about.me/jangalinski
• Simon
• https://about.me/zambrovski
• Axon Framework
• https://developer.axoniq.io/
23.09.2022, AxonIQCon 2022 30
https://www.pexels.com/de-de/foto/fragezeichen-auf-zerknittertem-papier-5428826/
This was a lot to swallow – DD/CQR/Oninon in 10 Minutes
Reached the Plateau of maximum complexityStory of ranked
Break down
we only care about the external/pivotal view
2 models, a saga, 3 busses
Separate ways is already a patternnot invented but recognized/discoveredIntroduce a model for context communication
how they commincate: patterns – no true or false, combinations are possible
We have 6 today, but there might be more
Query Patterns
Command Patterns
Reporting collects customer data and account to send out letters
credit manager needs balance history to calculate interest rates ...
Hint: GraphQL
Remote projection
Event: Milestone/Trigger/NotificationRuntime: not that hard
Query might not return the result the event intended
Event: Fat Event, contains data
Do we event source fat pivotal events?
next: command
ACL
Pure Choreography
customer wants to cancel releationship
account management discovers open debt and rejects
if sync: command failing might be enoughif async: ack/nack event
pure orchestration
Separate ways
Complex business scenarios
Process engine