Lorenzo Nicora makes an introductory presentation on event sourcing, what you want to achieve and how to use CQRS to implement event sourcing for your microservices.
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
12. 12
Additional BeneïŹts
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
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
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
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
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
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 BeneïŹts
+ 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