Introduction to CQRS and Event Sourcing
Joe Drumgoole
Director of Developer Advocacy, EMEA
@jdrumgoole
V1.0
What a Terrible Title for a Talk
4
Event Sourcing – not a new idea
5
Our Relational Friends Love It
6
We do It Ourselves
7
Think of a File backup system
Source
Disk
Backup Disk
8
Trivial Backup
Source
Disk
Backup Disk
MyFile.jpg
9
Just Copy The file
Source
Disk
Backup Disk
MyFile.jpg
MyFile.jpg
10
But…
Source
Disk
Backup Disk
MyFile.jpg
MyFile.jpg
What Happens:
• If the source file changes
• The target file changes
• I copy a dir with 1000 files
• Time passes
11
Lets Use a Backup Tool
Source
Disk
Backup Disk
MyFile.jpg
MyFile.jpg
Admin
12
What if We are a SaaS Backup Company?
MyFile.jpg MyFile.jpg
MyFile.jpg
MyFile.jpg
MyFile.jpg
Audit
MyFile.jpg
MyFile.jpg
MyFile.jpg
MyFile.jpg
MyFile.jpg
MyFile.jpg
MyFile.jpg
MyFile.jpg
MyFile.jpg
MyFile.jpg
MyFile.jpg
MyFile.jpg
MyFile.jpg
MyFile.jpg
Audit
13
What real SaaS Backup Companies do
Event 1
MyFile.jpg
Event 2
Event 3
Event 4
Event 5
Event N
MyFile.jpg Start
MyFile.jpg End
14
Where else is this Useful?
• Taxi haling apps
• Putting stuff into and out of a shopping basket
• Retail inventory
• Everywhere where the system actually turns out to be an
eventually consistent model of the world
15
Key Facets of Event Sourcing
• A immutable log file of events
• The current state is the sum of all previous states
• The events represent
• Events are “domain” level
• Events are generated by the “Commands” or “tasks” your system
undertakes
16
How do you Query an Event Sourcing System?
• Generate snapshots of your state
– Each file upload that is completed in a backup
– All the files per hour for each client computer
– All the files per day for each client computer
• But what about:
– “Find me the myfile.jpg that I backed up last year” ?
17
Command Query Responsibility Segregration
• Most data is queried more often than it is written
• Queries can be eventually consistent
• Commands have to be strongly consistent
• Let’s treat commands as first class objects
• Is predated by CQS
• Commands are related to the business domain
18
Split the Responsibilities
Commands
Commands
Commands
Commands
Commands
Commands
Domain Objects
Events
Events
Events
Events
Events
Events
Events
Events
Domain Objects
Commands
Commands
Commands
Commands
Commands
Queries
Query Objects
Query Objects
Domain Data
Snapshot
Snapshot
Snapshot
19
Things To Note
• Your event store gives you consistent state
• Your domain store gives you business level queries
• Domain store will lag the event store
• Event Store gives you state in the past and future
• Its not a systems design pattern so no frameworks
• Use it if it makes sense to you
20
Lambda Architecture
New
Data
Batch Layer
(Master Data Set)
Speed Layer
Serving Layer
Query
Query
Query
Query
Batch View
Batch View
Batch View
Real Time
View
Real Time
View
21
An Example Project : Filemap
• 226,830 files in $HOME, 139.9 GB
22
The Commands
Add File
Detect File
Change
Compute
Checksum
• Scans Filesystem tree
• Does a stat per file
• Pushes the file to a queue
• Write queue to event store
• Detects create, delete,
rename, write
• Captures new stat
information
• Writes to event store
• Adds checksums to files
without them
23
We have a complete Event State
• When a file was created
• When it was modified
• Its checksum
• To Do:
– When a file changes : recompute its checksum
– Other meta data (image analysis, faster checksum)
– Parser for third party storage (Facebook, Google Pictures)
– Server side storage of data (on AWS)
24
What about the Query Side?
• I want a synthesized view of individual files
• Time ordered
• Duplicate or irrelevant events eliminated
• Can use the aggregation framework to generate this data
• The ability to link data across multiple machines
• The ability to link data across multiple services
25
Summary
• Event Sourcing : Capture the state as first class objects
• CQRS : Optimise differently for commands and queries
• Not an architectural design pattern
• MongoDB ideally suited to build both event store and query store
• Use different storage engines depending on use case
• The aggregation framework generates summary data
Introduction to CQRS and Event Sourcing

Introduction to CQRS and Event Sourcing

  • 2.
    Introduction to CQRSand Event Sourcing Joe Drumgoole Director of Developer Advocacy, EMEA @jdrumgoole V1.0
  • 3.
    What a TerribleTitle for a Talk
  • 4.
    4 Event Sourcing –not a new idea
  • 5.
  • 6.
    6 We do ItOurselves
  • 7.
    7 Think of aFile backup system Source Disk Backup Disk
  • 8.
  • 9.
    9 Just Copy Thefile Source Disk Backup Disk MyFile.jpg MyFile.jpg
  • 10.
    10 But… Source Disk Backup Disk MyFile.jpg MyFile.jpg What Happens: •If the source file changes • The target file changes • I copy a dir with 1000 files • Time passes
  • 11.
    11 Lets Use aBackup Tool Source Disk Backup Disk MyFile.jpg MyFile.jpg Admin
  • 12.
    12 What if Weare a SaaS Backup Company? MyFile.jpg MyFile.jpg MyFile.jpg MyFile.jpg MyFile.jpg Audit MyFile.jpg MyFile.jpg MyFile.jpg MyFile.jpg MyFile.jpg MyFile.jpg MyFile.jpg MyFile.jpg MyFile.jpg MyFile.jpg MyFile.jpg MyFile.jpg MyFile.jpg MyFile.jpg Audit
  • 13.
    13 What real SaaSBackup Companies do Event 1 MyFile.jpg Event 2 Event 3 Event 4 Event 5 Event N MyFile.jpg Start MyFile.jpg End
  • 14.
    14 Where else isthis Useful? • Taxi haling apps • Putting stuff into and out of a shopping basket • Retail inventory • Everywhere where the system actually turns out to be an eventually consistent model of the world
  • 15.
    15 Key Facets ofEvent Sourcing • A immutable log file of events • The current state is the sum of all previous states • The events represent • Events are “domain” level • Events are generated by the “Commands” or “tasks” your system undertakes
  • 16.
    16 How do youQuery an Event Sourcing System? • Generate snapshots of your state – Each file upload that is completed in a backup – All the files per hour for each client computer – All the files per day for each client computer • But what about: – “Find me the myfile.jpg that I backed up last year” ?
  • 17.
    17 Command Query ResponsibilitySegregration • Most data is queried more often than it is written • Queries can be eventually consistent • Commands have to be strongly consistent • Let’s treat commands as first class objects • Is predated by CQS • Commands are related to the business domain
  • 18.
    18 Split the Responsibilities Commands Commands Commands Commands Commands Commands DomainObjects Events Events Events Events Events Events Events Events Domain Objects Commands Commands Commands Commands Commands Queries Query Objects Query Objects Domain Data Snapshot Snapshot Snapshot
  • 19.
    19 Things To Note •Your event store gives you consistent state • Your domain store gives you business level queries • Domain store will lag the event store • Event Store gives you state in the past and future • Its not a systems design pattern so no frameworks • Use it if it makes sense to you
  • 20.
    20 Lambda Architecture New Data Batch Layer (MasterData Set) Speed Layer Serving Layer Query Query Query Query Batch View Batch View Batch View Real Time View Real Time View
  • 21.
    21 An Example Project: Filemap • 226,830 files in $HOME, 139.9 GB
  • 22.
    22 The Commands Add File DetectFile Change Compute Checksum • Scans Filesystem tree • Does a stat per file • Pushes the file to a queue • Write queue to event store • Detects create, delete, rename, write • Captures new stat information • Writes to event store • Adds checksums to files without them
  • 23.
    23 We have acomplete Event State • When a file was created • When it was modified • Its checksum • To Do: – When a file changes : recompute its checksum – Other meta data (image analysis, faster checksum) – Parser for third party storage (Facebook, Google Pictures) – Server side storage of data (on AWS)
  • 24.
    24 What about theQuery Side? • I want a synthesized view of individual files • Time ordered • Duplicate or irrelevant events eliminated • Can use the aggregation framework to generate this data • The ability to link data across multiple machines • The ability to link data across multiple services
  • 25.
    25 Summary • Event Sourcing: Capture the state as first class objects • CQRS : Optimise differently for commands and queries • Not an architectural design pattern • MongoDB ideally suited to build both event store and query store • Use different storage engines depending on use case • The aggregation framework generates summary data

Editor's Notes

  • #5 1495 Luca Pacioli invented double entry accounting. Born Borgo Sansepolcro, Tuscany. A write only log.
  • #10 This is what we do when we do our own backups to an external drive. We drag and drop the disks.
  • #11 This is what we do when we do our own backups to an external drive. We drag and drop the disks.
  • #12 Now we track the source and target. When the backup was done. We are storing events about the system.
  • #13 Now we track the source and target. When the backup was done. We are storing events about the system.
  • #14 The state of the file is defined by the events. The events represent uploaded blocks but they are basically almost redundant. It’s the meta data that matters. Now we can reover from file renames and deletions. Edits and the ability to restart very large files after reboot. Event sourced systems work really well for intermittenly connected devices. Whats more back companies don’t ever coalesce these blocks. Why because they are checksumed and deduped.
  • #18 Commands : as opposed to methods on an object. Bertrand Meyer. Void commands queries with node side effects.
  • #21 Nathan Marz