SlideShare a Scribd company logo
A visual introduction to

Event Sourcing and CQRS
1
@nicusX
https://opencredo.com/author/lorenzo/
Lorenzo Nicora
Senior Consultant @ OpenCredo
2
A couple of concepts from DDD
Aggregate
(Current) State

of the Aggregate
Lorenzo Nicora Intro to Event Sourcing and CQRS
3
Once upon a time…
Everything
is synchronous
Request - Response
Lorenzo Nicora Intro to Event Sourcing and CQRS
4
Scaling up…
Updates —> Locks —> Contention!
<— Block <—
Lorenzo Nicora Intro to Event Sourcing and CQRS
5
Let’s go Asynchronous
Pwd —> “secret”
Pwd —> “12345”
Pwd —> “54321”
Pwd —> “secret”
Pwd —> “54321”
Pwd —> “12345”
===>

Out of 

Order
Asynchronous, Message-driven
Request/Response
ACID Transaction
Distributed, Message-based 

—> No order guaranteed
Lorenzo Nicora Intro to Event Sourcing and CQRS
6
Command Sourcing
💡
• Append Only —> No Contention
• Build State from Command history
Write fast,
Think later
K/V Store
Distributed
Lorenzo Nicora Intro to Event Sourcing and CQRS
Command
“Submit Order!”
—> A request (imperative sentence)
—> May fail
—> May affect multiple Aggregates
7
Commands vs Events
✉
Rebuild Aggregate State 

from Commands
Lorenzo Nicora Intro to Event Sourcing and CQRS
8
Event
“Order submitted”
—> Statement of facts (past tense)
—> Never fails
—> May affect a single Aggregate
✉
Rebuild Aggregate State 

from Events
Lorenzo Nicora Intro to Event Sourcing and CQRS
9
Commands to Events
(DDD patterns: Aggregate / Process Manager)
X
Y
Z
Lorenzo Nicora Intro to Event Sourcing and CQRS
10
Command > Event Sourcing
💡
Think a little,
Write,
Think later
Lorenzo Nicora Intro to Event Sourcing and CQRS
11
Event Sourcing
In many domains
Commands Events
Lorenzo Nicora Intro to Event Sourcing and CQRS
12
Additional Benefits
Easy

Eventual Business Consistency
—> Corrective Events
Robust to data corruption
(bugs, fat fingers…)
—> Rebuild state ignoring wrong events
Lorenzo Nicora Intro to Event Sourcing and CQRS
13
Additional Benefits
History
(for free)
Rebuild State 

at a point in Time
Lorenzo Nicora Intro to Event Sourcing and CQRS
14
Main Benefit
Scalable
—> Append only
-> Fits distributed k/v stores
—> Low-latency writes
—> Allows asynchronous processing
Lorenzo Nicora Intro to Event Sourcing and CQRS
What about reads?
15
Lorenzo Nicora Intro to Event Sourcing and CQRS
16
Retrieving the State
How do I retrieve the State?
“Get details of Order ‘AB123’”
❔
not very efficient, but…
…may work
Lorenzo Nicora Intro to Event Sourcing and CQRS
17
Querying (Searching) the State
❓
❓ How do query the State?
“Get all Orders delivered to ‘SE1 0NZ’”
❓
❓
Lorenzo Nicora Intro to Event Sourcing and CQRS
18
CQRS
Command
Query
Responsibility
Segregation
💡 Separate
• Code
• muService
• Datastore
-> Update
-—> Retrieve
“Query”
datastore is
downstream
Lorenzo Nicora Intro to Event Sourcing and CQRS
19
Not a new idea
Specialised
Downstream
Lorenzo Nicora Intro to Event Sourcing and CQRS
20
CQRS and Event Sourcing
Lorenzo Nicora Intro to Event Sourcing and CQRS
21
Materialised Views 

(of current State)
Lorenzo Nicora Intro to Event Sourcing and CQRS
22
Materialised Views (of State)
Latest (known) State
(Persistent)

Rebuildable
from Events
In Memory

K/V Store
Graph
…
RDBMS
Delayed
💡
Lorenzo Nicora Intro to Event Sourcing and CQRS
23
Materialised View of State
Query a RDBMS?!?
Wasn’t it the old way?
❓
RDBMS is just one of our options:

easy to use, easily become a bottleneck
Lorenzo Nicora Intro to Event Sourcing and CQRS
24
Materialised Views of State
* Views are optimised for 

specific query use cases
—> multiple Views from same Events
* Updated asynchronously, delayed
—> to Scale
-> may reorder Events
Lorenzo Nicora Intro to Event Sourcing and CQRS
25
Materialised Views of State
* Views can be rebuilt from Events
Event Log
is our Source of Truth
* Easy to evolve or fix
—> change or fix logic;
rebuild view from events
(not the View)
Lorenzo Nicora Intro to Event Sourcing and CQRS
26
Indexes
Lorenzo Nicora Intro to Event Sourcing and CQRS
27
Indexes
Search Engines

K/V Stores
• Optimised for querying (less for retrieving)
• Latest State; rebuild on the fly
💡
Lorenzo Nicora Intro to Event Sourcing and CQRS
28
Hybrid solutions
Lorenzo Nicora Intro to Event Sourcing and CQRS
29
Hybrid solutions: e.g. Snapshots
• Speed up rebuilding the current State
• Use recent Events to rebuild up-to-date
💡 Long delayed
Lorenzo Nicora Intro to Event Sourcing and CQRS
30
Eventual (Business) Consistency
Guess —> Compensate—> Apologies
Lorenzo Nicora Intro to Event Sourcing and CQRS
31
Eventual Consistency: Sagas
StatefulOut of band
Corrective Command 

or Event
Saga
Lorenzo Nicora Intro to Event Sourcing and CQRS
32
Lesson

from the Trenches
Lorenzo Nicora Intro to Event Sourcing and CQRS
33
Lesson Learned #1
If you put data in…
…you will eventually

have to get them out!
The “Query” side 

is not secondary
Lorenzo Nicora Intro to Event Sourcing and CQRS
34
Lessons Learned #2
In old days:

normalising one DB 

to support as many queries as possible
With CQRS: 

multiple denormalised “data stores”

optimised for different queries
No single “Q” implementation 

for all your queries
Lorenzo Nicora Intro to Event Sourcing and CQRS
35
Lessons Learned #3
A central, shared Event Store

may not be the best option
No Event-sourced Monolith
Prefer persistence 

per Bounded-Context
Lorenzo Nicora Intro to Event Sourcing and CQRS
+++ Summing up +++
36
Lorenzo Nicora Intro to Event Sourcing and CQRS
37
ES/CQRS Optimal Use Cases
High Volume
Low Latency writes
(big data)
Event Sourcing + CQRS
😋
Out-of-order Commands/Events

(IoT)
Lorenzo Nicora Intro to Event Sourcing and CQRS
38
ES/CQRS Drawbacks
x No “One-Size-Fits-All”

—> Multiple “Q” implementations
x Delayed reads
x No ACID Transactions
x Additional complexity (!)
🙁
Lorenzo Nicora Intro to Event Sourcing and CQRS
39
ES/CQRS Benefits
+ No “One-Size-Fits-All”
—> “Q” are optimised for use cases
+ Eventual (Business) Consistency
+ History, Temporal queries
+ Robust to data corruption
😀
Lorenzo Nicora Intro to Event Sourcing and CQRS
That’s all, Folks!
40
Lorenzo Nicora Intro to Event Sourcing and CQRS
??? Questions ???
41
Thanks.
⏳
Lorenzo Nicora Intro to Event Sourcing and CQRS

More Related Content

PDF
2019 DevSecOps Reference Architectures
Sonatype
 
PPTX
Monitoring & Observability
Lumban Sopian
 
PPTX
How to Move from Monitoring to Observability, On-Premises and in a Multi-Clou...
Splunk
 
PPTX
CQRS and Event Sourcing
Inho Kang
 
PPTX
Observability
Enes Altınok
 
PDF
Docker 101: Introduction to Docker
Docker, Inc.
 
PDF
Understanding MicroSERVICE Architecture with Java & Spring Boot
Kashif Ali Siddiqui
 
2019 DevSecOps Reference Architectures
Sonatype
 
Monitoring & Observability
Lumban Sopian
 
How to Move from Monitoring to Observability, On-Premises and in a Multi-Clou...
Splunk
 
CQRS and Event Sourcing
Inho Kang
 
Observability
Enes Altınok
 
Docker 101: Introduction to Docker
Docker, Inc.
 
Understanding MicroSERVICE Architecture with Java & Spring Boot
Kashif Ali Siddiqui
 

What's hot (20)

PDF
DevSecOps and the CI/CD Pipeline
James Wickett
 
PDF
More Than Monitoring: How Observability Takes You From Firefighting to Fire P...
DevOps.com
 
PPTX
DevSecOps reference architectures 2018
Sonatype
 
PDF
[DevSecOps Live] DevSecOps: Challenges and Opportunities
Mohammed A. Imran
 
PPTX
Squirreling Away $640 Billion: How Stripe Leverages Flink for Change Data Cap...
Flink Forward
 
PDF
DevSecOps: Taking a DevOps Approach to Security
Alert Logic
 
PPTX
Observability, what, why and how
Neeraj Bagga
 
PPTX
ABN AMRO DevSecOps Journey
Derek E. Weeks
 
PDF
A visual introduction to Event Sourcing and CQRS
Lorenzo Nicora
 
PDF
Introduction to DevSecOps
Setu Parimi
 
PDF
DevSecOps in Baby Steps
Priyanka Aash
 
PDF
Slide DevSecOps Microservices
Hendri Karisma
 
PDF
Cloud-Native Observability
Tyler Treat
 
PDF
ATT&CK Updates- Defensive ATT&CK
MITRE ATT&CK
 
PDF
Observability for modern applications
MoovingON
 
PPTX
MicroServices at Netflix - challenges of scale
Sudhir Tonse
 
PDF
DevSecOps Basics with Azure Pipelines
Abdul_Mujeeb
 
PPSX
Elastic-Engineering
Araf Karsh Hamid
 
PDF
Shift left Observability
Eric D. Schabell
 
PDF
Monitoring Kubernetes with Prometheus
Grafana Labs
 
DevSecOps and the CI/CD Pipeline
James Wickett
 
More Than Monitoring: How Observability Takes You From Firefighting to Fire P...
DevOps.com
 
DevSecOps reference architectures 2018
Sonatype
 
[DevSecOps Live] DevSecOps: Challenges and Opportunities
Mohammed A. Imran
 
Squirreling Away $640 Billion: How Stripe Leverages Flink for Change Data Cap...
Flink Forward
 
DevSecOps: Taking a DevOps Approach to Security
Alert Logic
 
Observability, what, why and how
Neeraj Bagga
 
ABN AMRO DevSecOps Journey
Derek E. Weeks
 
A visual introduction to Event Sourcing and CQRS
Lorenzo Nicora
 
Introduction to DevSecOps
Setu Parimi
 
DevSecOps in Baby Steps
Priyanka Aash
 
Slide DevSecOps Microservices
Hendri Karisma
 
Cloud-Native Observability
Tyler Treat
 
ATT&CK Updates- Defensive ATT&CK
MITRE ATT&CK
 
Observability for modern applications
MoovingON
 
MicroServices at Netflix - challenges of scale
Sudhir Tonse
 
DevSecOps Basics with Azure Pipelines
Abdul_Mujeeb
 
Elastic-Engineering
Araf Karsh Hamid
 
Shift left Observability
Eric D. Schabell
 
Monitoring Kubernetes with Prometheus
Grafana Labs
 
Ad

Viewers also liked (20)

PDF
Developing event-driven microservices with event sourcing and CQRS (phillyete)
Chris Richardson
 
PDF
Microservice Architecture with CQRS and Event Sourcing
Ben Wilcock
 
PDF
Haufe #msaday - Building a Microservice Ecosystem by Daniel Bryant
OpenCredo
 
PDF
Voxxed Bristol 2017 - From C to Q, one event at a time: Event Sourcing illust...
OpenCredo
 
PDF
Reactive Microservices By Lorenzo Nicora
OpenCredo
 
PDF
QCON London 2017 - Monitoring Serverless Architectures by Rafal Gancarz
OpenCredo
 
PDF
Developing event-driven microservices with event sourcing and CQRS (svcc, sv...
Chris Richardson
 
PDF
GOTO LONDON 2016: Concursus Event sourcing Evolved (Updated)
OpenCredo
 
PDF
Haufe #msaday - Seven More Deadly Sins of Microservices by Daniel Bryant
OpenCredo
 
PDF
Evolving Project Management: from the sin to the virtue by Antonio Cobo
OpenCredo
 
PDF
ServerlessConf: Serverless for the Enterprise - Rafal Gancarz
OpenCredo
 
PPTX
O'Reilly 2016: "Continuous Delivery with Containers: The Trials and Tribulati...
OpenCredo
 
PDF
High Load Strategy 2016 - Project Management: from Stone Age to DevOps
OpenCredo
 
PDF
A year with event sourcing and CQRS
Steve Pember
 
PDF
muCon 2016: Authentication in Microservice Systems By David Borsos
OpenCredo
 
PPTX
CQRS and Event Sourcing, An Alternative Architecture for DDD
Dennis Doomen
 
PDF
Event-sourced architectures with Akka
Sander Mak (@Sander_Mak)
 
PDF
London Hashicorp Meetup #8 - Testing Programmable Infrastructure By Matt Long
OpenCredo
 
PDF
Haufe #msaday - The Actor model: an alternative approach to concurrency By Lo...
OpenCredo
 
PPTX
CQRS
Fabian Vilers
 
Developing event-driven microservices with event sourcing and CQRS (phillyete)
Chris Richardson
 
Microservice Architecture with CQRS and Event Sourcing
Ben Wilcock
 
Haufe #msaday - Building a Microservice Ecosystem by Daniel Bryant
OpenCredo
 
Voxxed Bristol 2017 - From C to Q, one event at a time: Event Sourcing illust...
OpenCredo
 
Reactive Microservices By Lorenzo Nicora
OpenCredo
 
QCON London 2017 - Monitoring Serverless Architectures by Rafal Gancarz
OpenCredo
 
Developing event-driven microservices with event sourcing and CQRS (svcc, sv...
Chris Richardson
 
GOTO LONDON 2016: Concursus Event sourcing Evolved (Updated)
OpenCredo
 
Haufe #msaday - Seven More Deadly Sins of Microservices by Daniel Bryant
OpenCredo
 
Evolving Project Management: from the sin to the virtue by Antonio Cobo
OpenCredo
 
ServerlessConf: Serverless for the Enterprise - Rafal Gancarz
OpenCredo
 
O'Reilly 2016: "Continuous Delivery with Containers: The Trials and Tribulati...
OpenCredo
 
High Load Strategy 2016 - Project Management: from Stone Age to DevOps
OpenCredo
 
A year with event sourcing and CQRS
Steve Pember
 
muCon 2016: Authentication in Microservice Systems By David Borsos
OpenCredo
 
CQRS and Event Sourcing, An Alternative Architecture for DDD
Dennis Doomen
 
Event-sourced architectures with Akka
Sander Mak (@Sander_Mak)
 
London Hashicorp Meetup #8 - Testing Programmable Infrastructure By Matt Long
OpenCredo
 
Haufe #msaday - The Actor model: an alternative approach to concurrency By Lo...
OpenCredo
 
Ad

Similar to A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora (9)

PDF
From C to Q, one event at the time: Event sourcing illustrated [Voxxed Ticino...
Lorenzo Nicora
 
PDF
Event sourcing for IoT and mobile - JAX London 2017
Lorenzo Nicora
 
PDF
From C to Q one event at a time: Event Sourcing illustrated
Lorenzo Nicora
 
PPTX
How Fast Data Is Turned into Fast Information and Timely Action (OOW 2014)
Lucas Jellema
 
PPTX
Introduction to ksqlDB and stream processing (Vish Srinivasan - Confluent)
KafkaZone
 
PDF
Big Data Warsaw
Maximilian Michels
 
PDF
Stream processing with Apache Flink - Maximilian Michels Data Artisans
Evention
 
PDF
Sync or swim: the challenge of complex offline apps
OutSystems
 
PDF
How fast data is turned into fast information and timely action - Oracle Open...
Getting value from IoT, Integration and Data Analytics
 
From C to Q, one event at the time: Event sourcing illustrated [Voxxed Ticino...
Lorenzo Nicora
 
Event sourcing for IoT and mobile - JAX London 2017
Lorenzo Nicora
 
From C to Q one event at a time: Event Sourcing illustrated
Lorenzo Nicora
 
How Fast Data Is Turned into Fast Information and Timely Action (OOW 2014)
Lucas Jellema
 
Introduction to ksqlDB and stream processing (Vish Srinivasan - Confluent)
KafkaZone
 
Big Data Warsaw
Maximilian Michels
 
Stream processing with Apache Flink - Maximilian Michels Data Artisans
Evention
 
Sync or swim: the challenge of complex offline apps
OutSystems
 
How fast data is turned into fast information and timely action - Oracle Open...
Getting value from IoT, Integration and Data Analytics
 

More from OpenCredo (17)

PDF
Webinar - Design Thinking for Platform Engineering
OpenCredo
 
PDF
MuCon 2019: Exploring Your Microservices Architecture Through Network Science...
OpenCredo
 
PDF
Goto Chicago; Journeys To Cloud Native Architecture: Sun, Sea And Emergencies...
OpenCredo
 
PPTX
Mucon 2018: Heuristics for Identifying Microservice Boundaries By Erich Eichi...
OpenCredo
 
PDF
Journeys To Cloud Native Architecture: Sun, Sea And Emergencies - Nicki Watt
OpenCredo
 
PDF
Machine Learning Game Changer for IT - Maartens Lourens
OpenCredo
 
PDF
Kafka Summit 2018: A Journey Building Kafka Connectors - Pegerto Fernandez
OpenCredo
 
PDF
MuCon 2017: A not So(A) Trivial Question by Tareq Abedrabbo
OpenCredo
 
PDF
DevOpsCon Berlin 2017: Project Management from Stone Age to DevOps By Antoni...
OpenCredo
 
PDF
Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki ...
OpenCredo
 
PDF
Succeeding with DevOps Transformation - Rafal Gancarz
OpenCredo
 
PDF
Progscon 2017: Serverless Architectures - Rafal Gancarz
OpenCredo
 
PDF
Vault: Beyond secret storage - Using Vault to harden your infrastructure
OpenCredo
 
PPTX
ThingMonk 2016 - Concursus Event sourcing for the IOT By Tareq Abedrabbo & Do...
OpenCredo
 
PDF
Spring Boot Microservices vs Akka Actor Cluster
OpenCredo
 
PDF
Microservices Manchester: Microservices and Macro-Economics - A Shorty Histor...
OpenCredo
 
PDF
Microservices Manchester: Security, Microservces and Vault by Nicki Watt
OpenCredo
 
Webinar - Design Thinking for Platform Engineering
OpenCredo
 
MuCon 2019: Exploring Your Microservices Architecture Through Network Science...
OpenCredo
 
Goto Chicago; Journeys To Cloud Native Architecture: Sun, Sea And Emergencies...
OpenCredo
 
Mucon 2018: Heuristics for Identifying Microservice Boundaries By Erich Eichi...
OpenCredo
 
Journeys To Cloud Native Architecture: Sun, Sea And Emergencies - Nicki Watt
OpenCredo
 
Machine Learning Game Changer for IT - Maartens Lourens
OpenCredo
 
Kafka Summit 2018: A Journey Building Kafka Connectors - Pegerto Fernandez
OpenCredo
 
MuCon 2017: A not So(A) Trivial Question by Tareq Abedrabbo
OpenCredo
 
DevOpsCon Berlin 2017: Project Management from Stone Age to DevOps By Antoni...
OpenCredo
 
Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki ...
OpenCredo
 
Succeeding with DevOps Transformation - Rafal Gancarz
OpenCredo
 
Progscon 2017: Serverless Architectures - Rafal Gancarz
OpenCredo
 
Vault: Beyond secret storage - Using Vault to harden your infrastructure
OpenCredo
 
ThingMonk 2016 - Concursus Event sourcing for the IOT By Tareq Abedrabbo & Do...
OpenCredo
 
Spring Boot Microservices vs Akka Actor Cluster
OpenCredo
 
Microservices Manchester: Microservices and Macro-Economics - A Shorty Histor...
OpenCredo
 
Microservices Manchester: Security, Microservces and Vault by Nicki Watt
OpenCredo
 

Recently uploaded (20)

PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
The Future of Artificial Intelligence (AI)
Mukul
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 

A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

  • 1. A visual introduction to
 Event Sourcing and CQRS 1 @nicusX https://opencredo.com/author/lorenzo/ Lorenzo Nicora Senior Consultant @ OpenCredo
  • 2. 2 A couple of concepts from DDD Aggregate (Current) State
 of the Aggregate Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 3. 3 Once upon a time… Everything is synchronous Request - Response Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 4. 4 Scaling up… Updates —> Locks —> Contention! <— Block <— Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 5. 5 Let’s go Asynchronous Pwd —> “secret” Pwd —> “12345” Pwd —> “54321” Pwd —> “secret” Pwd —> “54321” Pwd —> “12345” ===>
 Out of 
 Order Asynchronous, Message-driven Request/Response ACID Transaction Distributed, Message-based 
 —> No order guaranteed Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 6. 6 Command Sourcing 💡 • Append Only —> No Contention • Build State from Command history Write fast, Think later K/V Store Distributed Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 7. Command “Submit Order!” —> A request (imperative sentence) —> May fail —> May affect multiple Aggregates 7 Commands vs Events ✉ Rebuild Aggregate State 
 from Commands Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 8. 8 Event “Order submitted” —> Statement of facts (past tense) —> Never fails —> May affect a single Aggregate ✉ Rebuild Aggregate State 
 from Events Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 9. 9 Commands to Events (DDD patterns: Aggregate / Process Manager) X Y Z Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 10. 10 Command > Event Sourcing 💡 Think a little, Write, Think later Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 11. 11 Event Sourcing In many domains Commands Events Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 12. 12 Additional Benefits Easy
 Eventual Business Consistency —> Corrective Events Robust to data corruption (bugs, fat fingers…) —> Rebuild state ignoring wrong events Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 13. 13 Additional Benefits History (for free) Rebuild State 
 at a point in Time Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 14. 14 Main Benefit Scalable —> Append only -> Fits distributed k/v stores —> Low-latency writes —> Allows asynchronous processing Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 15. What about reads? 15 Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 16. 16 Retrieving the State How do I retrieve the State? “Get details of Order ‘AB123’” ❔ not very efficient, but… …may work Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 17. 17 Querying (Searching) the State ❓ ❓ How do query the State? “Get all Orders delivered to ‘SE1 0NZ’” ❓ ❓ Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 18. 18 CQRS Command Query Responsibility Segregation 💡 Separate • Code • muService • Datastore -> Update -—> Retrieve “Query” datastore is downstream Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 19. 19 Not a new idea Specialised Downstream Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 20. 20 CQRS and Event Sourcing Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 21. 21 Materialised Views 
 (of current State) Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 22. 22 Materialised Views (of State) Latest (known) State (Persistent)
 Rebuildable from Events In Memory
 K/V Store Graph … RDBMS Delayed 💡 Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 23. 23 Materialised View of State Query a RDBMS?!? Wasn’t it the old way? ❓ RDBMS is just one of our options:
 easy to use, easily become a bottleneck Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 24. 24 Materialised Views of State * Views are optimised for 
 specific query use cases —> multiple Views from same Events * Updated asynchronously, delayed —> to Scale -> may reorder Events Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 25. 25 Materialised Views of State * Views can be rebuilt from Events Event Log is our Source of Truth * Easy to evolve or fix —> change or fix logic; rebuild view from events (not the View) Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 26. 26 Indexes Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 27. 27 Indexes Search Engines
 K/V Stores • Optimised for querying (less for retrieving) • Latest State; rebuild on the fly 💡 Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 28. 28 Hybrid solutions Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 29. 29 Hybrid solutions: e.g. Snapshots • Speed up rebuilding the current State • Use recent Events to rebuild up-to-date 💡 Long delayed Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 30. 30 Eventual (Business) Consistency Guess —> Compensate—> Apologies Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 31. 31 Eventual Consistency: Sagas StatefulOut of band Corrective Command 
 or Event Saga Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 32. 32 Lesson
 from the Trenches Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 33. 33 Lesson Learned #1 If you put data in… …you will eventually
 have to get them out! The “Query” side 
 is not secondary Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 34. 34 Lessons Learned #2 In old days:
 normalising one DB 
 to support as many queries as possible With CQRS: 
 multiple denormalised “data stores”
 optimised for different queries No single “Q” implementation 
 for all your queries Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 35. 35 Lessons Learned #3 A central, shared Event Store
 may not be the best option No Event-sourced Monolith Prefer persistence 
 per Bounded-Context Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 36. +++ Summing up +++ 36 Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 37. 37 ES/CQRS Optimal Use Cases High Volume Low Latency writes (big data) Event Sourcing + CQRS 😋 Out-of-order Commands/Events
 (IoT) Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 38. 38 ES/CQRS Drawbacks x No “One-Size-Fits-All”
 —> Multiple “Q” implementations x Delayed reads x No ACID Transactions x Additional complexity (!) 🙁 Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 39. 39 ES/CQRS Benefits + No “One-Size-Fits-All” —> “Q” are optimised for use cases + Eventual (Business) Consistency + History, Temporal queries + Robust to data corruption 😀 Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 40. That’s all, Folks! 40 Lorenzo Nicora Intro to Event Sourcing and CQRS
  • 41. ??? Questions ??? 41 Thanks. ⏳ Lorenzo Nicora Intro to Event Sourcing and CQRS