SlideShare a Scribd company logo
CQRS
&
Event sourcing
Event sourcing
Event sourcing
About me
• Lifetime hacker
• Explorer of technologies
• Polyglot programmer
• @sorenmat
• https://github.com/sorenmat
CQRS + ES
• What is CQRS
• What is Event sourcing
• Demo
Typical N-TIER architecture
pros
• Well known
• Accepted by
management
• Lots of resources
• Lots of frameworks
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
Cqrs+es
CQRS to the rescue
CQRS
W
TF
!!
CQRS to the resuce
Command
Query
Responsibility
Segregation
W
TF
!!
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)
Why CQRS ?
• Scalability
• Reduced complexity
• Flexibility
• Focus on the business
Making commands, gives focus on
the business
Making commands, gives focus on
the business
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
CQRS Structure
Commands
• Commands ask the
system to do changes
• Commands can fail
• State intent
• Can be asynchronous
Stale data
• Async - ooohh nooes
• Eventual consistency
• How stale must stale data be ?
• How stale is your data ?
• Design for it
Read Models
• Build for the “UI”
• Optimized for the
use case
• Comes in all shape
and forms
Event sourcing
Don’t save the result, save the steps !
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
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
Aggregates
•Must ensure that its state never ever violates its
business invariants
•Between the boundaries, there can only be
eventual consistency
Aggregate example
• Aggregate are the “base” of the model
• Order is the aggregate root
• Order has an addLineItem method
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
Events
• Represents the state of the aggregate
• Passed tense, can’t be undone
• Events never changes
Event sourcing
• Conflict merging
• Easy integration
• Expose events through feed
• Very testable
Event store
• Stores all events
• Append only
• Single source of truth
Event store
• Simple in design
• Few operations
• append
• getAllEvents
• getEventsForAggregate
Event Store
• Performance can be gained through
snapshots
• Serialize the aggregate with an eventId
Summary
Pros and Cons
• Great performance
• Very scalable
• Auditing
• Easy to integrate
• Testable
• Tech free
• Not well known
• Different mindset
DEMO
Our model

More Related Content

PDF
Serverless Logging Architecture
PDF
Iot meets Serverless
PDF
stackconf 2020 | Scalable testing infrastructure with Kubernetes and Concord ...
PPTX
The Hardest Part of Microservices: Calling Your Services
PDF
Microservices with Spring Boot
PPTX
Icinga Camp Bangalore - Icinga and Icinga Director
PDF
How to build streaming data pipelines with Akka Streams, Flink, and Spark usi...
PPTX
JakartaOne Livestream CN4J: Eclipse MicroProfile - Your Cloud-Native Companion
Serverless Logging Architecture
Iot meets Serverless
stackconf 2020 | Scalable testing infrastructure with Kubernetes and Concord ...
The Hardest Part of Microservices: Calling Your Services
Microservices with Spring Boot
Icinga Camp Bangalore - Icinga and Icinga Director
How to build streaming data pipelines with Akka Streams, Flink, and Spark usi...
JakartaOne Livestream CN4J: Eclipse MicroProfile - Your Cloud-Native Companion

What's hot (19)

PPTX
Intelligent Cloud Conference 2018 - Building secure cloud applications with A...
PPTX
Icinga Camp Bangalore - Enterprise exceptions
PPTX
Essential git for developers
PDF
Go, Swarm and DevOps vs The Mighty Monolith
PPTX
Microservices Testing
PPTX
Intelligent Cloud Conference 2018 - Next Generation of Data Integration with ...
PDF
Migrating .NET and .NET Core to Pivotal Cloud Foundry (1/2)
PPTX
Azure Event Grid: Glue for the Internet
PPTX
Getting started with Azure Cognitive services
PDF
REST API Overview with Nutanix
PDF
Redux data flow with angular
PPTX
Expose BizTalk to the world (ACSUG)
PPTX
Tech Talks Microservices
PDF
Control and monitor_microservices_with_microprofile
PDF
Transactions in micro-services (fall 2019)
PPTX
Reporting
PPTX
Stateful stream processing of iIoT events with C# and containers
PPTX
Automate your development environment with Jira and Saltstack
PPTX
Monoliths vs microservices
Intelligent Cloud Conference 2018 - Building secure cloud applications with A...
Icinga Camp Bangalore - Enterprise exceptions
Essential git for developers
Go, Swarm and DevOps vs The Mighty Monolith
Microservices Testing
Intelligent Cloud Conference 2018 - Next Generation of Data Integration with ...
Migrating .NET and .NET Core to Pivotal Cloud Foundry (1/2)
Azure Event Grid: Glue for the Internet
Getting started with Azure Cognitive services
REST API Overview with Nutanix
Redux data flow with angular
Expose BizTalk to the world (ACSUG)
Tech Talks Microservices
Control and monitor_microservices_with_microprofile
Transactions in micro-services (fall 2019)
Reporting
Stateful stream processing of iIoT events with C# and containers
Automate your development environment with Jira and Saltstack
Monoliths vs microservices
Ad

Similar to Cqrs+es (20)

PPTX
Cqrs and Event Sourcing Intro For Developers
PPTX
Real World Event Sourcing and CQRS
PDF
fram^ TechTalk #1 - CQRS and Event Sourcing (ES)
PDF
GECon2017_Building scalable application with cqrs and event sourcing (a. hars...
PDF
An Introduction to event sourcing and CQRS
PDF
A visual introduction to Event Sourcing and CQRS
PDF
A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora
PDF
Unite 2017 - CQRS - Jens Gheerardyn
PPTX
Design | expose ap is with cqrs
PPTX
Intro to event sourcing and CQRS
PPTX
CQRS and Event Sourcing, An Alternative Architecture for DDD
PDF
Microservice Architecture with CQRS and Event Sourcing
PPTX
PPTX
Design | expose ap is with cqr
PDF
Developing event-driven microservices with event sourcing and CQRS (Shanghai)
PDF
CQRS_EventSourcing Architecture Overview.pptx.pdf
PDF
CQRS and Event Sourcing
PDF
CQRS + Event Sourcing
PPTX
Cqrs event sourcing slide landis+gyr
PPTX
LevelsConf 2018 Event Sourcing - Dasith Wijesiriwardena
Cqrs and Event Sourcing Intro For Developers
Real World Event Sourcing and CQRS
fram^ TechTalk #1 - CQRS and Event Sourcing (ES)
GECon2017_Building scalable application with cqrs and event sourcing (a. hars...
An Introduction to event sourcing and CQRS
A visual introduction to Event Sourcing and CQRS
A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora
Unite 2017 - CQRS - Jens Gheerardyn
Design | expose ap is with cqrs
Intro to event sourcing and CQRS
CQRS and Event Sourcing, An Alternative Architecture for DDD
Microservice Architecture with CQRS and Event Sourcing
Design | expose ap is with cqr
Developing event-driven microservices with event sourcing and CQRS (Shanghai)
CQRS_EventSourcing Architecture Overview.pptx.pdf
CQRS and Event Sourcing
CQRS + Event Sourcing
Cqrs event sourcing slide landis+gyr
LevelsConf 2018 Event Sourcing - Dasith Wijesiriwardena
Ad

Recently uploaded (20)

PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Approach and Philosophy of On baking technology
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Cloud computing and distributed systems.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Encapsulation_ Review paper, used for researhc scholars
Diabetes mellitus diagnosis method based random forest with bat algorithm
MYSQL Presentation for SQL database connectivity
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Big Data Technologies - Introduction.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The AUB Centre for AI in Media Proposal.docx
Approach and Philosophy of On baking technology
Advanced methodologies resolving dimensionality complications for autism neur...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Building Integrated photovoltaic BIPV_UPV.pdf
Network Security Unit 5.pdf for BCA BBA.
Machine learning based COVID-19 study performance prediction
Cloud computing and distributed systems.
Digital-Transformation-Roadmap-for-Companies.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Spectral efficient network and resource selection model in 5G networks
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Encapsulation_ Review paper, used for researhc scholars

Cqrs+es

  • 2. About me • Lifetime hacker • Explorer of technologies • Polyglot programmer • @sorenmat • https://github.com/sorenmat
  • 3. CQRS + ES • What is CQRS • What is Event sourcing • Demo
  • 4. Typical N-TIER architecture pros • Well known • Accepted by management • Lots of resources • Lots of frameworks
  • 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
  • 7. CQRS to the rescue CQRS W TF !!
  • 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
  • 13. Commands • Commands ask the system to do changes • Commands can fail • State intent • Can be asynchronous
  • 14. Stale data • Async - ooohh nooes • Eventual consistency • How stale must stale data be ? • How stale is your data ? • Design for it
  • 15. Read Models • Build for the “UI” • Optimized for the use case • Comes in all shape and forms
  • 17. Don’t save the result, save the steps !
  • 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
  • 24. Event sourcing • Conflict merging • Easy integration • Expose events through feed • Very testable
  • 25. Event store • Stores all events • Append only • Single source of truth
  • 26. Event store • Simple in design • Few operations • append • getAllEvents • getEventsForAggregate
  • 27. Event Store • Performance can be gained through snapshots • Serialize the aggregate with an eventId
  • 29. Pros and Cons • Great performance • Very scalable • Auditing • Easy to integrate • Testable • Tech free • Not well known • Different mindset
  • 30. DEMO