Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CQRS for Great Good

CQRS for Great Good

presented at geecon 2013 in Kraków, Poland

Oliver Wolf

May 16, 2013
Tweet

More Decks by Oliver Wolf

Other Decks in Technology

Transcript

  1. The default architecture for distributed business apps Domain Model User

    Interface Remote Facade Application Services DTO DTO ORM DB
  2. The default architecture for distributed business apps Domain Model User

    Interface Remote Facade Application Services DTO DTO ORM DB findCustomers() getCustomer() updateCustomer() <customer> <name>John Doe</name> <address>...</address> ... </customer>
  3. The default architecture for distributed business apps Domain Model User

    Interface Remote Facade Application Services DTO DTO ORM DB GET /customers?filter=... GET /customer/{id} PUT /customer/{id} { "name": “John Doe”, "address": { ... } }
  4. The default architecture for distributed business apps Domain Model User

    Interface Remote Facade Application Services DTO DTO ORM DB GET /customers?filter=... GET /customer/{id} PUT /customer/{id} { "name": “John Doe”, "address": { ... } } Anything wrong with this?
  5. “The features that characterize a class are divided into commands

    and queries. A command serves to modify objects, a query to return information about objects.” Bertrand Meyer ETH Zurich Text cited from: Object-Oriented Software Construction, second edition, 1997
  6. class Foo { void command(); Result query(); } Mutates state

    Returns a value without causing side effects
  7. CQRS = CQS in the large Scope is a single

    class Scope is a Bounded Context
  8. The default architecture for distributed business apps Domain Model User

    Interface Remote Facade Application Services DTO DTO ORM DB
  9. The default architecture for distributed business apps Domain Model User

    Interface Query Facade Query Services DTO DTO ORM DB Command Facade Command Services CQRS PATTERN APPLIED
  10. Assumption: Reads and Writes are strongly cohesive, so they must

    be part of the same Bounded Context. FALSE
  11. The default architecture for distributed business apps Domain Model User

    Interface Query Facade Query Services DTO DTO ORM DB Command Facade Command Services CQRSified
  12. The default architecture for distributed business apps CQRSified User Interface

    Query Facade Query Services DTO DTO ORM DB Command Facade Command Services Domain Model ORM Domain Model Query Facade Query Services ORM Domain Model Query Facade Query Services ORM Domain Model Command and query parts can scale independently, e.g. to accommodate highly asymmetric load.
  13. Assumption: Reads and writes use the same data, so they

    must be served from and applied to the same domain model.
  14. Assumption: Reads and writes use the same data, so they

    must be served from and applied to the same domain model. FALSE
  15. The default architecture for distributed business apps CQRSified Queries can

    benefit from a specialized query model, optimized for quick data retrieval (de-normalized, pre- aggregated,...) User Interface Query Facade Query Services DTO DTO ORM DB Command Facade Command Services Command Model ORM Domain Model Query Facade Query Services ORM Domain Model Query Facade Query Services ORM Query Model
  16. The default architecture for distributed business apps CQRSified Queries can

    benefit from a specialized query model, optimized for quick data retrieval (de-normalized, pre- aggregated,...) User Interface Query Facade Query Services DTO DTO ORM DB Command Facade Command Services Command Model ORM Domain Model Query Facade Query Services ORM Domain Model Query Facade Query Services ORM Query Model ‣ validate and process commands ‣ keep data consistent ‣ guarantee ACID properties ‣ behaviour of domain model ‣ more difficult to scale ‣ rich query capabilities ‣ short response times ‣ different views on data ‣ potentially denormalized ‣ easier to scale
  17. Assumption: For queries, we have to use a domain model

    to abstract from the underlying data model.
  18. Assumption: For queries, we have to use a domain model

    to abstract from the underlying data model. FALSE
  19. Query Services Query Services Thin Read Layer User Interface Query

    Facade Query Services DTO DTO ORM DB Command Facade Command Services Command Model Query Facade Query Services Query Facade Query Services SELECT … FROM … WHERE ... The default architecture for distributed business apps CQRSified Queries are just dealing with data, not with behavior. Why do we need objects?
  20. Assumption: We must use the same database for queries and

    commands to make sure that data is consistent.
  21. Assumption: We must use the same database for queries and

    commands to make sure that data is consistent. FALSE
  22. The default architecture for distributed business apps CQRSified In many

    cases, eventual consistency is sufficient. The data users are looking at in the UI is always stale to some extent. Query Services Query Services Thin Read Layer User Interface Query Facade Query Services DTO DTO ORM DB Command Facade Command Services Command Model Query Facade Query Services Query Facade Query Services SELECT … FROM … WHERE ... Query DB Query DB Query DB Event Handler Event Handler Event Handler Events Write
  23. The default architecture for distributed business apps CQRSified In many

    cases, users don’t care whether their actions have immediate effect – as long as they eventually get feedback. Query Services Query Services Thin Read Layer User Interface Query Facade Query Services CMD DTO ORM DB Command Facade Command Services Command Model Query Facade Query Services Query Facade Query Services SELECT … FROM … WHERE ... Query DB Query DB Query DB Event Handler Event Handler Event Handler Events Write Command Queue Commands update read model asynchronously
  24. Query Services Query Services Thin Read Layer User Interface Query

    Facade Query Services CMD DTO DB Command Facade Command Services Command Model Query Facade Query Services Query Facade Query Services SELECT … FROM … WHERE ... Query DB Query DB Query DB Event Handler Event Handler Event Handler Events Write Command Queue Commands Processor The default architecture for distributed business apps CQRSified CQRS plays well with an Event Sourcing architecture – you just store events and re-create the state of domain objects as needed. event store
  25. ‣ capture each update to application state in an event

    ‣ changes to domain objects are the result of applying events ‣ events are immutable ‣ events are a representation of what has happened at a specific time Event Sourcing in a nutshell
  26. ‣ allows you to rebuild application state at any point

    in time just by replaying events up to that point in time ‣ allows you to analyse historic data based on detailed events that would otherwise have been lost ‣ gives you an audit log “for free” ‣ you can add new, optimized read models (potentially in-memory) later without migration hassle and such What’s so great about Event Sourcing?
  27. That’s it. Really. Feel free to ask me anything! @owolf

    http://www.flickr.com/photos/kaptainkobold/5170454747/ How do I convince by boss that we’ll have to rewrite our application with CQRS???
  28. Don’t do CQRS… …if your application is just a simple

    CRUD-style app. …if you don’t have scaling issues. …if it doesn’t improve your domain models.
  29. Consider doing CQRS… …if your write/read load ratio is highly

    asymmetrical. …if scaling your application is difficult. …if your domain model is bloated by complex domain logic, making queries inefficient. …if you can benefit from event sourcing.