Why DDD for CQRS and Event Sourcing application?
Asked Answered
R

2

5

I am just starting with the CQRS, Event Sourcing. While examining many frameworks, I have realized that one of the thing most of the frameworks assumed that our application is modelled based on the DDD principles. I am not sure but can anyone explain to me that how DDD, CQRS, and Event Sourcing are related to each other and why DDD is generally required for implementing the CQRS and Event Sourcing pattern?

Riffraff answered 24/1, 2022 at 14:36 Comment(0)
S
10

why DDD is generally required for implementing the CQRS and Event Sourcing pattern?

Tradition.

Before being rebranded as "CQRS", the same collection of ideas wore the label "Distributed Domain Driven Design". Greg Young's early presentations were experience reports describing how he adapted the ideas published by Evans when designing an algorithmic trading system.

In effect, this means that most of the audience that discovers CQRS and Event Sourcing have already been convinced that Domain Driven Design is a good idea.

They are different ideas, though; the CQRS pattern might be useful even when you aren't doing event sourcing.

can anyone explain to me that how DDD, CQRS, and Event Sourcing are related to each other

Event sourcing is really a pair of ideas

  • changes should be first class citizens in our model
  • our authoritative data model should be a sequence of changes (aka a history)

This kind of data model is great for writes (just append a new change on the end of the list), and has the additional benefit that edits are non-destructive; all of the information added to the system is still there, so you can easily time machine back to understand what things looked like prior to some change.

But histories suck for low latency query. Therefore: caches - we'll pre-compute the answers to some questions, and stick them in a property bag.

Now we have two models: the history, and the cache. And what is CQRS?

CQRS is simply the creation of two objects where there was previously only one -- Greg Young 2010

Songstress answered 24/1, 2022 at 16:33 Comment(0)
F
0

I don't think it's only tradition. When designing a system that would be using events as state transitions, the most useful approach is getting away from CRUD-style events (SomethingCreated, SomethingUpdated, etc), and model the actual behaviour instead. That's where Event Sourcing comes very close to the foundational idea of DDD - rich Domain Models, expressed with Ubiquitous Language.

Also, when we design an event-sourced system, we put some effort into defining stream boundaries, so we don't get large transactions, we don't have multi-stream transactions, the average stream length is upper-bound, etc. All these concerns align very nicely with the tactical Aggregate pattern of DDD.

When people just shovel events to Kafka topics and project them to some databases, they essentially are not doing Event Sourcing, but implementing an event-driven system instead.

Fluorescein answered 29/1, 2022 at 19:59 Comment(1)
Thank you for your answer. I understand how the idea of DDD and event sourcing align nicely with each other. I have one more question on this regard. What are the advantages of the system that has implemented CQRS and Event Sourcing (ES) design pattern which has followed DDD pattern wrt the system which hasn't modelled its software based on DDD principle and has still implemented the CQRS and ES pattern?Riffraff

© 2022 - 2024 — McMap. All rights reserved.