5. Typical N-TIER architecture
cons
• Complex
• Business logic bleeds through
layers
• Validation everywhere
• No provable audit trail
• No concept of change
• Caching
• Hard to scale
No concept of change -> class
change customer state means
what ever hibernate says
No concept of change -> class
change customer state means
what ever hibernate says
8. CQRS to the resuce
Command
Query
Responsibility
Segregation
W
TF
!!
9. CQRS
Based upon Bertrand Meyer’s Command
Query Separation (CQS) principle:
“every method should either be a command that
performs an action, or a query that returns data to
the caller, but not both. In other words, asking a
question should not change the answer” (Meyer)
10. Why CQRS ?
• Scalability
• Reduced complexity
• Flexibility
• Focus on the business
Making commands, gives focus on
the business
Making commands, gives focus on
the business
11. CQRS in the making
• Let’s focus on the language -
AddItemToBasket vs UpdateBasket
• Task based UI
• We need to separate reads from writes
• Lets split our architecture
18. Event sourcing
•Analysis and access to historic data
•Provable Audit log
•Flexibility to create new views or queries
(CQRS)
•Replaying events for debugging
•Enables Memory Image
19. Aggregates
•A boundary for transactional consistency
•Only one aggregate can be modified in a single
transaction
•A cloud of related objects, both entities and
value objects
•Only the root can be referenced from outside the
aggregate
20. Aggregates
•Must ensure that its state never ever violates its
business invariants
•Between the boundaries, there can only be
eventual consistency
21. Aggregate example
• Aggregate are the “base” of the model
• Order is the aggregate root
• Order has an addLineItem method
22. Event stream
•Every state-modifying operation on an
aggregate results in an event
•The actual state change is achieved by applying
the event to the aggregate
OrderCreatedOrderCreated OrderLineItemAddedOrderLineItemAdded OrderLineItemRemovedOrderLineItemRemoved OrderLineItemAddedOrderLineItemAdded
23. Events
• Represents the state of the aggregate
• Passed tense, can’t be undone
• Events never changes