Search code examples
cqrsevent-sourcing

Is CQRS tied up to Event Sourcing?


I recently read a lot about CQRS and for me it seems like it's closely tied up to Event Sourcing.

But like this answer said https://stackoverflow.com/a/9217461/277067 For me Event SOurcing seems a bit too complicated/scary for a beginners like me ("what ? my object current state is nto stored anywhere ??").

So i'd like to know if indeed they are tied up or if there is any tools/famework that would help for doing cqrs (event observer, command handler) without the complicated part of event sourcing.

Thanks


Solution

  • Short answer: No, CQRS and event-sourcing are not tied to each other.

    Long answer: No, CQRS and event-sourcing are not tied to each other, and they aren't tied as well to domain-driven design (DDD).

    If you want to define what CQRS, event-sourcing and DDD are in a few words, you may come up with explanations as the following ones (yes of course, they are over-simplified, but that's exactly the point here):

    • CQRS is a design pattern that separates writing state from reading state (commands vs queries).
    • Event-sourcing is a way to store data in a database, where the deltas are stored rather than the actual state.
    • DDD is a method to make communication on the domain easier within interdisciplinary teams.

    Each of them works without the others very well. E.g., you can model a domain using DDD, and then implement it without CQRS or event-sourcing. You may also do event-sourcing without ever needing DDD or CQRS. And so on…

    But: The three concepts play very well together, which is why they are often called together within a single sentence. So, no they aren't tied to each other, but they make a lot of sense in combination with each other.

    The following picture shows how they may interact with each other:

    CQRS, event-sourcing and DDD in combination

    (The image is taken from the documentation of wolkenkit, a CQRS and event-sourcing framework for JavaScript and Node.js.)

    • CQRS describes that you send commands to the write model, and that you receive events and subscribe to queries from the read model.
    • Event-sourcing is used with the write model to store the events that are published as result of the commands the client sends.
    • DDD is used within the write model to turn commands into events and to run the appropriate logic.