Introduction to Event Sourcing and CQRS

70 %
30 %
Information about Introduction to Event Sourcing and CQRS
Technology

Published on October 20, 2014

Author: vladikkk

Source: slideshare.net

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.

1. Introduction to Event Sourcing … and CQRS

2. Vladik Khononov Solutions Architect at Plexop vladikk http://vladikk.com vladikk http://il.linkedin.com/in/vladikkhononov/

3. Introduction to Event Sourcing … and CQRS

4. Domain Driven Design?

5. Introduction How we are used to do things

6. Business Domain Model Logic PresentationDAL

7. Model: A simplified representation of a system or phenomenon. – www.dictionary.com מודל: ייצוג תאורטי של מערכת מורכבת, שמטרתו לחקות את המערכת בהיבטים מהותיים. המודל אינו מתאר כל תופעה במערכת, אלא מתייחס להיבטים מוגדרים ומצומצמים שלה. המודל מבוסס על קירוב של המציאות בדרך של הפשטה, איחוד ישויות והתעלמות מגורמים שהשפעתם אינה מהותית. – www.wikipedia.org

8. User Interface Storage Domain Model

9. Domain Model

10. Good Domain Model • Not too much • Not too less • Sweet spot • The information we need • The information we might need

11. 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

12. Case Study: Customers Management

13. • 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)

14. • 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

15. 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) )

16. • 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

17. • 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

18. • Analysts should be able to review status changes history for each customer - change date and the new status

19. 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) )

20. Id Name Email Phone number Status Scheduled Call 10 John john@gmail.com 04-2342343 New

21. Id Name Email Phone number Status Scheduled Call 10 John john@gmail.com 04-2342343 CallLater 27/10/2014

22. Id Name Email Phone number Status Scheduled Call 10 John john@gmail.com 08-9876653 CallLater 27/10/2014

23. Id Name Email Phone number Status Scheduled Call 10 John john@gmail.com 08-9876653 Converted

24. Id Name Email Phone number Status Scheduled Call 10 John john@gmail.com 08-9876653 Converted

25. Event Sourcing Capture all changes to an application state as a sequence of events ~ Martin Fowler

26. 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; }

27. 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); } …. …. }

28. 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); } … }

29. 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); } }

30. Id Name Email Phone number Status Scheduled Call 10 John john@gmail.com 08-9876653 Converted

31. 1. new StatusChanged(Status.CallLater) 2. new FutureCallScheduled(’27/10/2014’) 3. new ContactDetailsWereUpdated( NewPhoneNumber=’08-9876653’ ) 4. new StatusChanged(Status.Converted)

32. Event Storage Entity Id + New events Event1, Entity Id Event2, Event3, ….

33. 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); } }

34. 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) ); } …. …. }

35. class Customer { int Id; string Name; string Email; string PhoneNumber; Status Status; DateTime? ScheduledCall; List<IEvent> Events; }

36. class CustomerSearchModel { int Id; List<string> Names; List<string> Emails; List<string> PhoneNumbers; Status Status; DateTime? ScheduledCall; }

37. class CustomerAnalysisModel { int Id; string Name; string Email; string PhoneNumber; List<StatusChange> StatusChanges; DateTime? ScheduledCall; }

38. Good Domain Model ✓ Not too much ✓ Not too less ✓ Sweet spot ✓ The information we need ✓ The information we might need

39. 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

40. Event Storage Entity Id + New events Event1, Entity Id Event2, Event3, ….

41. CQRS Command Query Responsibility Segregation

42. Layered Architecture

43. Step 1

44. Step 2

45. Step 3

46. CQRS

47. Conclusions • 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

48. 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

49. Questions?

50. http://vladikk.com http://twitter.com/vladikk http://facebook.com/vladikk http://il.linkedin.com/in/vladikkhononov

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Reference 3: Introducing Event Sourcing - msdn.microsoft.com

... Introducing Event Sourcing. Reference 3: ... This chapter is an introduction; Chapter 4, "A CQRS and ES Deep Dive," explores event sourcing and its ...
Read more

Introduction to Domain Driven Design, CQRS and Event Sourcing

Introduction to Domain Driven Design, CQRS and Event Sourcing. Posted on Thursday, December 5th, 2013 at 02:11, by Kenneth Truyers
Read more

Event Sourcery - Introduction to Domain Modeling, CQRS ...

Introduction TO Domain Modeling, CQRS, and Event Sourcing. ... CQRS / Event Sourcing; Intro to Event Sourcing;
Read more

Introduction to CQRS - CodeProject

Introduction to CQRS. ... Events are usually connected to another pattern called Event Sourcing ... (Event Sourcing) CQRS is a great idea and I will use it ...
Read more

Introduction to Event Sourcing and CQRS - Nomad PHP

Product Description. Have you heard about event sourcing and wondered what it is all about? Have you looked into it and wondered what sort of sorcery is ...
Read more

CQRS - Martin Fowler

CQRS stands for Command Query ... This allows these services to easily take advantage of Event Sourcing. ... site introduction; about me; FAQ;
Read more

Introduction to Event Sourcing and Command-Query ...

The concepts of event sourcing (ES) and command-query responsibility segregation (CQRS) have been around for quite a while. They are getting more and more ...
Read more

Greg Young - CQRS and Event Sourcing - Code on the Beach ...

Watch Greg Young's talk "CQRS and Event Sourcing" from ... Building and deploying microservices with event sourcing, CQRS ... Querying Event ...
Read more

Event Sourcing - Martin Fowler

Event Sourcing ensures that all changes to application state are ... With Event Sourcing we also capture each event. ... site introduction; about ...
Read more