introduction to event sourcing and cqrs
DESCRIPTION
Event sourcing is a pattern for modelling your application’s business logic. It states that all changes to application state should be defined and stored as a sequence of events. The idea of recording events for information storage is nothing new. It has been used for decades in finance, healthcare, and other fields. A few years ago it was rediscovered in software design and its advantages are many: - Suitable for building scalable, highly concurrent, distributed systems. - The stored events give you the true history of a system. This audit is required by law in some industries. - The system’s state can be reversed to any point in the past for retroactive debugging and data analysis. - Gives freedom to refactor your business logic, thus allows much better response to new requirements. - The required infrastructure is simple - no monstrous databases are involved. The focus of my talk will be the Event Sourcing pattern, but I’ll also briefly describe CQRS - an architecture that goes hand in hand with Event Sourcing.TRANSCRIPT
![Page 1: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/1.jpg)
Introduction to Event Sourcing … and CQRS
![Page 2: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/2.jpg)
Vladik KhononovSolutions Architect at Plexop
http://il.linkedin.com/in/vladikkhononov/
vladikk vladikkhttp://vladikk.com
![Page 3: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/3.jpg)
Introduction to Event Sourcing… and CQRS
![Page 4: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/4.jpg)
Domain Driven Design?
![Page 5: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/5.jpg)
![Page 6: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/6.jpg)
IntroductionHow we are used to do things
![Page 7: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/7.jpg)
PresentationDALBusiness Logic
Domain Model
![Page 8: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/8.jpg)
– www.dictionary.com
Model: A simplified representation of a system or phenomenon.
– www.wikipedia.org
מודל: ייצוג תאורטי של מערכת מורכבת, שמטרתו לחקות את המערכת בהיבטים מהותיים. המודל
אינו מתאר כל תופעה במערכת, אלא מתייחס להיבטים מוגדרים ומצומצמים שלה. המודל
מבוסס על קירוב של המציאות בדרך של הפשטה, איחוד ישויות והתעלמות מגורמים שהשפעתם
אינה מהותית.
![Page 9: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/9.jpg)
User Interface StorageDomain
Model
![Page 10: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/10.jpg)
Domain Model
![Page 11: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/11.jpg)
Good Domain Model• Not too much
• Not too less
• Sweet spot
• The information we need
• The information we might need
![Page 12: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/12.jpg)
Why domain models fail
• We absolutely suck at predicting the future
• No single model can suit all the use cases (e.g.: transactional processing, business intelligence, search)
• Model transformations are painful
![Page 13: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/13.jpg)
Case Study: Customers Management
![Page 14: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/14.jpg)
• A customer has customer id number and a name • A customer has contact information: email, phone
number • A customer can be in on of the following states:
New, CallLater, Converted, NotInterested • A seller has seller id number, name and password • The selling home page contains a list of the
customers in the following statuses: • New • CallLater (when scheduled call date is met)
![Page 15: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/15.jpg)
• Seller chooses a customer to call from the home page
• Seller can change the customer’s status to “Converted”, “Not Interested”
• Seller can schedule a future call by setting the future call date. The Customer’s status will change to “CallLater”
• Seller can change name, email, and phone number
![Page 16: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/16.jpg)
enum Status { New, CallLater, Converted, NotInterested }
class Customer { int Id; string Name; Status Status; DateTime? ScheduledCall; string Email; string PhoneNumber;}
CREATE TABLE Customers ( ID INTEGER, Name CHAR(40), Email CHAR(40), PhoneNumber CHAR(40), Status INTEGER, ScheduledCall DATETIME, PRIMARY KEY (ID))
class Seller { int Id; string Name; string Password;}
CREATE TABLE Sellers ( ID INTEGER, Name CHAR(40), Password CHAR(40) PRIMARY KEY (ID))
![Page 17: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/17.jpg)
• Seller can find customers by name, email, and/or phone number in the search page
• The customers should be found by the current and the past values
![Page 18: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/18.jpg)
• If no new customers are available on the home page, it should display customers in the NotInterested status, if it was set more than a 30 days ago
![Page 19: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/19.jpg)
• Analysts should be able to review status changes history for each customer - change date and the new status
![Page 20: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/20.jpg)
class Seller { int Id; string Name; string Password;}
CREATE TABLE Sellers ( ID INTEGER, Name CHAR(40), Password CHAR(40) PRIMARY KEY (ID))
enum Status { New, CallLater, Converted, NotInterested }
class Customer { int Id; string Name; Status Status; DateTime? ScheduledCall; string Email; string PhoneNumber;}
CREATE TABLE Customers ( ID INTEGER, Name CHAR(40), Email CHAR(40), PhoneNumber CHAR(40), Status INTEGER, ScheduledCall DATETIME, PRIMARY KEY (ID))
![Page 22: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/22.jpg)
Id Name Email Phone number Status Scheduled Call
10 John [email protected] 04-2342343 CallLater 27/10/2014
![Page 23: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/23.jpg)
Id Name Email Phone number Status Scheduled Call
10 John [email protected] 08-9876653 CallLater 27/10/2014
![Page 26: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/26.jpg)
Event SourcingCapture all changes to an application
state as a sequence of events ~ Martin Fowler
![Page 27: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/27.jpg)
![Page 28: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/28.jpg)
class StatusChanged : IEvent { Status NewStatus; DateTime CreatedOn; int CreatedBy; }
class FutureCallScheduled : IEvent { DateTime ScheduledCallTime; DateTime CreatedOn; int CreatedBy; }
class ContactDetailsWereUpdated : IEvent { string NewName; string NewEmail; string NewPhone; DateTime CreatedOn; int CreatedBy; }
![Page 29: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/29.jpg)
class Customer { int Id; string Name; string Email; string PhoneNumber; Status Status; DateTime? ScheduledCall;
List<IEvent> Events; …
void Apply(StatusChanged e) { Status = e.NewStatus;Events.Add(e);
} …. ….}
![Page 30: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/30.jpg)
class Customer { int Id; string Name; string Email; string PhoneNumber; Status Status; DateTime? ScheduledCall;
List<IEvent> Events; … …
void Apply(FutureCallScheduled e) { ScheduledCall = e.ScheduledCallTime;Events.Add(e);
} …}
![Page 31: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/31.jpg)
class Customer { int Id; string Name; string Email; string PhoneNumber; Status Status; DateTime? ScheduledCall;
List<IEvent> Events; … … …
void Apply(ContactDetailsWereUpdated e) { Name = e.NewName;Email = e.NewEmail;PhoneNumber = e.NewPhoneNumber;Events.Add(e);
} }
![Page 33: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/33.jpg)
1. new StatusChanged(Status.CallLater)
2. new FutureCallScheduled(’27/10/2014’)
3. new ContactDetailsWereUpdated( NewPhoneNumber=’08-9876653’)
4. new StatusChanged(Status.Converted)
![Page 34: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/34.jpg)
Event Storage
Entity Id + New events
Entity IdEvent1,Event2,Event3,
….
![Page 35: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/35.jpg)
class CustomerSearchModel { int Id; List<string> Names; List<string> Emails; List<string> PhoneNumbers; Status Status; DateTime? ScheduledCall; … … …
void Apply(ContactDetailsWereUpdated e) { Names.Add(e.NewName);Emails.Add(e.NewEmail);PhoneNumbers.Add(e.NewPhoneNumber);
} }
![Page 36: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/36.jpg)
class CustomerAnalysisModel { int Id; string Name; string Email; string PhoneNumber;
List<StatusChange> StatusChanges; DateTime? ScheduledCall; …
void Apply(StatusChanged e) { StatusChanges.Add(
new StatusChange(e.CreatedOn, e.NewStatus));
} …. ….}
![Page 37: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/37.jpg)
class Customer { int Id; string Name; string Email; string PhoneNumber; Status Status; DateTime? ScheduledCall; List<IEvent> Events;}
![Page 38: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/38.jpg)
class CustomerSearchModel { int Id; List<string> Names;
List<string> Emails;List<string> PhoneNumbers;
Status Status; DateTime? ScheduledCall;}
![Page 39: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/39.jpg)
class CustomerAnalysisModel { int Id; string Name; string Email; string PhoneNumber;
List<StatusChange> StatusChanges; DateTime? ScheduledCall; }
![Page 40: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/40.jpg)
Good Domain Model✓ Not too much
✓ Not too less
✓ Sweet spot
✓ The information we need
✓ The information we might need
![Page 41: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/41.jpg)
Why domain models fail
✓ We absolutely suck at predicting the future
✓ No single model can suit all the use cases (e.g.: transactional processing, business intelligence, search)
✓ Model transformations are painful
![Page 42: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/42.jpg)
Event Storage
Entity Id + New events
Entity IdEvent1,Event2,Event3,
….
![Page 43: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/43.jpg)
CQRSCommand Query Responsibility Segregation
![Page 44: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/44.jpg)
Laye
red
Arch
itect
ure
![Page 45: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/45.jpg)
Step
1
![Page 46: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/46.jpg)
Step
2
![Page 47: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/47.jpg)
Step
3
![Page 48: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/48.jpg)
CQ
RS
![Page 49: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/49.jpg)
• Event based models
• Effective modeling
• No future predicting required
• Time machine
• Retroactive debugging
• Infrastructural freedom
• Infinite scalability
• Easy to scale writes
• Easy to scale reads
Conclusions
![Page 50: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/50.jpg)
Before you try this at home
• Read “Implementing Domain Driven Design” by Vaughn Vernon
• Read “Domain Driven Design” by Eric Evans
• Follow Greg Young
• Read DDD/CQRS on Google Groups
![Page 51: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/51.jpg)
Questions?
![Page 52: Introduction to Event Sourcing and CQRS](https://reader034.vdocument.in/reader034/viewer/2022042514/5598e4591a28abb17d8b474e/html5/thumbnails/52.jpg)
http://il.linkedin.com/in/vladikkhononov
http://twitter.com/vladikk
http://facebook.com/vladikk
http://vladikk.com