Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

ISTA 2016: Event Sourcing

97 views

Published on

Slides from my talk at ISTA 2016 - https://www.istacon.org/Sessions/Sessions?year=2016

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

ISTA 2016: Event Sourcing

  1. 1. 15 – 16 November, SofiaISTACon.org Event Sourcing By Vladik Khononov A New Dimension in Software Design
  2. 2. 15 – 16 November, SofiaISTACon.org
  3. 3. 15 – 16 November, SofiaISTACon.org
  4. 4. 15 – 16 November, SofiaISTACon.org
  5. 5. 15 – 16 November, SofiaISTACon.org Agenda • How it happened • Why it happened • How event sourcing solves the problem • Why event sourcing benefits the company
  6. 6. 15 – 16 November, SofiaISTACon.org Call Center Management
  7. 7. 15 – 16 November, SofiaISTACon.org LEAD • Id • Name • Email • Phone • Status (Available / Follow-up / Closed / Converted)
  8. 8. 15 – 16 November, SofiaISTACon.org Name Email Phone Status Stefan Ivanov stefan@ivanov.com 03-27243457 Available Viktor Kovachev viktor@kovachev.com 08-8332491 Available Martin Mateev martin@mateev.com 04-8537112 Available Lilyana Yankov lilyana@yankov.com 04-6092212 Available
  9. 9. 15 – 16 November, SofiaISTACon.org Name Email Phone Status Stefan Ivanov stefan@ivanov.com 03-27243457 Available Viktor Kovachev viktor@kovachev.com 08-8332491 Available Martin Mateev martin@mateev.com 04-8537112 Available Lilyana Yankov lilyana@yankov.com 04-6092212 Available
  10. 10. 15 – 16 November, SofiaISTACon.org
  11. 11. 15 – 16 November, SofiaISTACon.org public class Lead { public long Id { get; set; } public string Name { get; set; } public string Email { get; set; } public string Phone { get; set; } public LeadStatus Status { get; set; } public DateTime? FollowupOn { get; set; } }
  12. 12. 15 – 16 November, SofiaISTACon.org
  13. 13. 15 – 16 November, SofiaISTACon.org SELECT * FROM leads WHERE [name] LIKE ‘%name%’ OR [phone] LIKE ‘%phone%’ OR [email] LIKE ‘%email%’;
  14. 14. WTF??? ISTACon.org
  15. 15. 15 – 16 November, SofiaISTACon.org Lead #1410 Name Viktor Radkov Email viktor@radkov.com Phone 09-9801298 Status Available
  16. 16. 15 – 16 November, SofiaISTACon.org
  17. 17. 15 – 16 November, SofiaISTACon.org PM
  18. 18. 15 – 16 November, SofiaISTACon.org …1 month later
  19. 19. 15 – 16 November, SofiaISTACon.org
  20. 20. 15 – 16 November, SofiaISTACon.org PM
  21. 21. 15 – 16 November, SofiaISTACon.org …2 months later
  22. 22. 15 – 16 November, SofiaISTACon.org
  23. 23. 15 – 16 November, SofiaISTACon.org PM
  24. 24. 15 – 16 November, SofiaISTACon.org Search on stale data Status change dates Audit log for BI
  25. 25. 15 – 16 November, SofiaISTACon.org PM
  26. 26. 15 – 16 November, SofiaISTACon.org PM
  27. 27. 15 – 16 November, SofiaISTACon.org public class Lead { public long Id { get; set; } public string Name { get; set; } public string Email { get; set; } public string Phone { get; set; } public LeadStatus Status { get; set; } public DateTime? FollowupOn { get; set; } }
  28. 28. 15 – 16 November, SofiaISTACon.org Name Email Phone Status Follow-up Stefan Ivanov stefan@ivanov.com 03-27243457 Available
  29. 29. 15 – 16 November, SofiaISTACon.org Name Email Phone Status Follow-up Date Stefan Ivanov stefan@ivanov.com 050-8139904 Follow-up 23/11/2016
  30. 30. 15 – 16 November, SofiaISTACon.org Name Email Phone Status Follow-up Date Stefan Ivanov stefan@ivanov.com 050-8139904 Converted
  31. 31. 15 – 16 November, SofiaISTACon.org Name Email Phone Status Follow-up Date Stefan Ivanov stefan@ivanov.com 050-8139904 Converted
  32. 32. 15 – 16 November, SofiaISTACon.org
  33. 33. 15 – 16 November, SofiaISTACon.org Event Sourcing
  34. 34. 15 – 16 November, SofiaISTACon.org Changes = First class citizens = Events
  35. 35. 15 – 16 November, SofiaISTACon.org Lead Events • Lead Was Initialized (Id) • Status Changed (NewStatus) • Name Changed (NewName) • Contact Information Changed (NewEmail, NewPhone) • Followup Set (Date)
  36. 36. 15 – 16 November, SofiaISTACon.org Lead: Stefan Ivanov • Lead Was Initialized (1410) • Status Changed (Available) • Contact Information Changed (stefan@ivanov.com, 03-27243457) • Status Changed (Followup) • Followup Set (23/11/2016) • Contact Information Changed (stefan@ivanov.com, 050-8139904) • Status Changed (Converted)
  37. 37. 15 – 16 November, SofiaISTACon.org public class Lead { public long Id { get; set; } public string Name { get; set; } public string Email { get; set; } public string Phone { get; set; } public LeadStatus Status { get; set; } public DateTime? FollowupOn { get; set; } public void Apply(LeadWasInitialized event) {…} public void Apply(StatusChanged event) {…} public void Apply(NameChanged event) {…} public void Apply(ContactInfoChanged event) {…} public void Apply(FollowupSet event) {…} }
  38. 38. 15 – 16 November, SofiaISTACon.org public class Lead { public long Id { get; set; } public string Name { get; set; } public string Email { get; set; } public string Phone { get; set; } public LeadStatus Status { get; set; } public DateTime? FollowupOn { get; set; } public void Apply(LeadWasInitialized event) {…} public void Apply(StatusChanged event) {…} public void Apply(NameChanged event) {…} public void Apply(ContactInfoChanged event) {…} public void Apply(FollowupSet event) {…} } Apply(StatusChanged event) { this.Status = event.NewStatus; }
  39. 39. 15 – 16 November, SofiaISTACon.org public class Lead { public long Id { get; set; } public string Name { get; set; } public string Email { get; set; } public string Phone { get; set; } public LeadStatus Status { get; set; } public DateTime? FollowupOn { get; set; } public void Apply(LeadWasInitialized event) {…} public void Apply(StatusChanged event) {…} public void Apply(NameChanged event) {…} public void Apply(ContactInfoChanged event) {…} public void Apply(FollowupSet event) {…} } Apply(NameChanged event) { this.Name = event.NewName; }
  40. 40. 15 – 16 November, SofiaISTACon.org public class Lead { public long Id { get; set; } public string Name { get; set; } public string Email { get; set; } public string Phone { get; set; } public LeadStatus Status { get; set; } public DateTime? FollowupOn { get; set; } public void Apply(LeadWasInitialized event) {…} public void Apply(StatusChanged event) {…} public void Apply(NameChanged event) {…} public void Apply(ContactInfoChanged event) {…} public void Apply(FollowupSet event) {…} } Apply(ContactInfoChanged event) { this.Phone = event.NewPhone; this.Email = event.NewEmail; }
  41. 41. 15 – 16 November, SofiaISTACon.org public class Lead { public long Id { get; set; } public string Name { get; set; } public string Email { get; set; } public string Phone { get; set; } public LeadStatus Status { get; set; } public DateTime? FollowupOn { get; set; } public void Apply(LeadWasInitialized event) {…} public void Apply(StatusChanged event) {…} public void Apply(NameChanged event) {…} public void Apply(ContactInfoChanged event) {…} public void Apply(FollowupSet event) {…} } Apply(FollowupSet event) { this.FollowUpOn = event.Date; }
  42. 42. 15 – 16 November, SofiaISTACon.org Lead: Stefan Ivanov Lead Was Initialized (1410) Status Changed (Available) Contact Information Changed (stefan@ivanov.com,03-27243457) Status Changed (Converted) Status Changed (Followup) Followup Set (23/11/2016) Contact Information Changed (stefan@ivanov.com, 050-8139904)
  43. 43. 15 – 16 November, SofiaISTACon.org Name Email Phone Status Follow-up Date Stefan Ivanov stefan@ivanov.com 050-8139904 Converted
  44. 44. 15 – 16 November, SofiaISTACon.org Lead: Stefan Ivanov Lead Was Initialized (1410) Status Changed (Available) Contact Information Changed (stefan@ivanov.com,03-27243457) Status Changed (Converted) Status Changed (Followup) Followup Set (23/11/2016) Contact Information Changed (stefan@ivanov.com, 050-8139904)
  45. 45. 15 – 16 November, SofiaISTACon.org Lead: Stefan Ivanov Lead Was Initialized (1410) Status Changed (Available) Contact Information Changed (stefan@ivanov.com,03-27243457)
  46. 46. 15 – 16 November, SofiaISTACon.org Lead: Stefan Ivanov Lead Was Initialized (1410) Status Changed (Available) Contact Information Changed (stefan@ivanov.com,03-27243457) Status Changed (Followup) Followup Set (23/11/2016) Contact Information Changed (stefan@ivanov.com, 050-8139904)
  47. 47. 15 – 16 November, SofiaISTACon.org Lead: Stefan Ivanov Lead Was Initialized (1410) Status Changed (Available) Contact Information Changed (stefan@ivanov.com,03-27243457) Status Changed (Converted) Status Changed (Followup) Followup Set (23/11/2016) Contact Information Changed (stefan@ivanov.com, 050-8139904)
  48. 48. 15 – 16 November, SofiaISTACon.org public class Lead { public long Id { get; set; } public string Name { get; set; } public string Email { get; set; } public string Phone { get; set; } public LeadStatus Status { get; set; } public DateTime? FollowupOn { get; set; } }
  49. 49. 15 – 16 November, SofiaISTACon.org Lead: Stefan Ivanov Lead Was Initialized (1410) Status Changed (Available) Contact Information Changed (stefan@ivanov.com,03-27243457) Status Changed (Converted) Status Changed (Followup) Followup Set (23/11/2016) Contact Information Changed (stefan@ivanov.com, 050-8139904)
  50. 50. 15 – 16 November, SofiaISTACon.org public class LeadSearch { public long Id { get; private set; } … public List<string> Emails; public List<string> Phones; … public void Apply(ContactInfoChanged event) {…} … } Apply(ContactInfoChanged event) { this.Phones.Append(event.NewPhone); this.Emails.Append(event.NewEmail); }
  51. 51. 15 – 16 November, SofiaISTACon.org public class LeadStatusChangesModel { public long Id { get; set; } public List<StatusLog> StatusChangesLog; … public void Apply(StatusChanged event) {…} … } Apply(StatusChanged event) { this.StatusChangesLog.Append( new StatusLog(event.NewStatus, DateTime.Now); );
  52. 52. 15 – 16 November, SofiaISTACon.org public class LeadStatusChangesModel { public long Id { get; set; } public List<StatusLog> StatusChangesLog; … } public class Lead { public long Id { get; set; } public string Name { get; set; } public string Email { get; set; } public string Phone { get; set; } public LeadStatus Status { get; set; } public DateTime? FollowupOn { get; set; } … } public class LeadSearch { public long Id { get; private set; } public List<string> Emails; public List<string> Phones; … }
  53. 53. 15 – 16 November, SofiaISTACon.org Lead: Stefan Ivanov Lead Was Initialized (1410) Status Changed (Available) Contact Information Changed (stefan@ivanov.com,03-27243457) Status Changed (Converted) Status Changed (Followup) Followup Set (23/11/2016) Contact Information Changed (stefan@ivanov.com, 050-8139904)
  54. 54. 15 – 16 November, SofiaISTACon.org Events = Source of Truth Event Sourcing
  55. 55. 15 – 16 November, SofiaISTACon.org
  56. 56. 15 – 16 November, SofiaISTACon.org
  57. 57. 15 – 16 November, SofiaISTACon.org
  58. 58. 15 – 16 November, SofiaISTACon.org Storage: Event Store
  59. 59. 15 – 16 November, SofiaISTACon.org Event Store API: The Good News Append(Entity Id + New events) GetEvents(Entity Id)Event1,
 Event2,
 Event3, ….
  60. 60. 15 – 16 November, SofiaISTACon.org Key (Entity Id) Value (Events list) Lead #1410 1. LeadWasInitialized(1410) 2. StatusChanged(Available) 3. NameChanged(“Martin Mateev”) 4. StatusChanged(Converted) Lead #1406 1. LeadWasInitialized(1406) 2. StatusChanged(Available’’’) 3. NameChanged(“Lilyana Yankov”)
  61. 61. 15 – 16 November, SofiaISTACon.org Event Store API: The Good News Append(Entity Id + New events) GetEvents(Entity Id)Event1,
 Event2,
 Event3, ….
  62. 62. 15 – 16 November, SofiaISTACon.org Event Store API: The Bad News Append(Entity Id + New events) GetEvents(Entity Id)Event1,
 Event2,
 Event3, ….
  63. 63. 15 – 16 November, SofiaISTACon.org CQRS Command Query Responsibility Segregation
  64. 64. 15 – 16 November, SofiaISTACon.org CQRS • Command - write data • Query - read data • A use case can be either Command or Query. Never both
  65. 65. 15 – 16 November, SofiaISTACon.org Queries Read Model Read DB Commands Write Model Writes DB Projection engine
  66. 66. 15 – 16 November, SofiaISTACon.org Event Sourced Model (Write) Lead #1410 1. LeadWasInitialized(1410) 2. StatusChanged(Available) 3. NameChanged(“Martin Mateev”) 4. StatusChanged(Closed) … 1000. StatusChanged(FollowUp) 1001. FollowupSet(16/11/2017) Read Model Id 1410 Name Martin Mateev Status Followup FollowupOn (Empty) Email … Phone …
  67. 67. 15 – 16 November, SofiaISTACon.org Queries Read Model Read DB Commands Write Model Writes DB Projection engine
  68. 68. 15 – 16 November, SofiaISTACon.org Event Sourced Model (Write) Lead #1410 1. LeadWasInitialized(1410) 2. StatusChanged(Available) 3. NameChanged(“Martin Mateev”) 4. StatusChanged(Closed) 5. StatusChanged(Available) 6. NameChanged(“Viktor Rumenov”) Read Model Id 1410 Phone … Status Available FollowupOn (Empty) Email … Name Martin Mateev
  69. 69. 15 – 16 November, SofiaISTACon.org public class Lead { public long Id { get; set; } public string Name { get; set; } public string Email { get; set; } public string Phone { get; set; } public LeadStatus Status { get; set; } public DateTime? FollowupOn { get; set; } public void Apply(LeadWasInitialized event) {…} public void Apply(StatusChanged event) {…} public void Apply(NameChanged event) {…} public void Apply(ContactInfoChanged event) {…} public void Apply(FollowupSet event) {…} } Apply(NameChanged event) { this.Name = event.NewName; }
  70. 70. 15 – 16 November, SofiaISTACon.org Queries Read Model Read DB Commands Event Sourcing Event Store Projection engine
  71. 71. 15 – 16 November, SofiaISTACon.org Queries Read Model Read DB Commands Write Model Writes DB Projection engine Projection • RDBMS • Documents • Graphs • Files
  72. 72. 15 – 16 November, SofiaISTACon.org Queries Read Model Commands Write Model Writes DB Projection engine Projection • RDBMS • Documents • Graphs • Files • Multiple DBs Read DBs
  73. 73. 15 – 16 November, SofiaISTACon.org Queries Read Model Commands Write Model Writes DB Projection engine Read DBs Concurrency • Pessimistic • Optimistic • Optimistic on steroids
  74. 74. 15 – 16 November, SofiaISTACon.org SetFollowupCommand + StatusChangeEvent => Collision ChangeNameCommand + StatusChangeEvent => OK
  75. 75. 15 – 16 November, SofiaISTACon.org Event Sourcing + CQRS • Flexible business domain modeling • Insane scalability and availability • Rock solid infrastructure • …look great on your C.V.
  76. 76. 15 – 16 November, SofiaISTACon.org
  77. 77. 15 – 16 November, SofiaISTACon.org CQRS: When? Event Sourcing ➡ CQRS Non-functional benefits
  78. 78. 15 – 16 November, SofiaISTACon.org Event Sourcing CQRS
  79. 79. 15 – 16 November, SofiaISTACon.org Event Sourcing: When? ?
  80. 80. 15 – 16 November, SofiaISTACon.org Subdomains Generic, Supporting, Core
  81. 81. 15 – 16 November, SofiaISTACon.org
  82. 82. 15 – 16 November, SofiaISTACon.org Subdomains Generic, Supporting, Core
  83. 83. 15 – 16 November, SofiaISTACon.org CREATIVE CATALOG CAMPAIGN PUBLISHING BILLING USERS MANAGEMENT LEAD MANAGEMENTCOMMISSIONS CALCULATION DESK MANAGEMENTVOIP MANAGEMENT EMAIL CAMPAIGNS
  84. 84. 15 – 16 November, SofiaISTACon.org Generic Subdomains
  85. 85. 15 – 16 November, SofiaISTACon.org CREATIVE CATALOG CAMPAIGN PUBLISHING BILLING USERS MANAGEMENT LEAD MANAGEMENTCOMMISSIONS CALCULATION DESK MANAGEMENTVOIP MANAGEMENT EMAIL CAMPAIGN
  86. 86. 15 – 16 November, SofiaISTACon.org CREATIVE CATALOG CAMPAIGN PUBLISHING BILLING USERS MANAGEMENT LEAD MANAGEMENTCOMMISSIONS CALCULATION DESK MANAGEMENTVOIP MANAGEMENT EMAIL CAMPAIGN
  87. 87. 15 – 16 November, SofiaISTACon.org Supporting Subdomains
  88. 88. 15 – 16 November, SofiaISTACon.org CREATIVE CATALOG CAMPAIGN PUBLISHING BILLING USERS MANAGEMENT LEAD MANAGEMENTCOMMISSIONS CALCULATION DESK MANAGEMENTVOIP MANAGEMENT EMAIL CAMPAIGN
  89. 89. 15 – 16 November, SofiaISTACon.org CREATIVE CATALOG CAMPAIGN PUBLISHING BILLING USERS MANAGEMENT LEAD MANAGEMENTCOMMISSIONS CALCULATION DESK MANAGEMENTVOIP MANAGEMENT EMAIL CAMPAIGN
  90. 90. 15 – 16 November, SofiaISTACon.org Core Domains
  91. 91. 15 – 16 November, SofiaISTACon.org CREATIVE CATALOG CAMPAIGN PUBLISHING BILLING USERS MANAGEMENT LEAD MANAGEMENTCOMMISSIONS CALCULATION DESK MANAGEMENTVOIP MANAGEMENT EMAIL CAMPAIGN
  92. 92. 15 – 16 November, SofiaISTACon.org Event Sourcing: When? Core business domains
  93. 93. 15 – 16 November, SofiaISTACon.org Before you try this at home
  94. 94. 15 – 16 November, SofiaISTACon.org Available Event Stores • http://GetEventStore.com • NEventStore • Akka Persistence • Elastic Event Store (Coming soon)
  95. 95. 15 – 16 November, SofiaISTACon.org
  96. 96. 15 – 16 November, SofiaISTACon.org €100 Discount for attendees of ISTA 2016 https://ti.to/webengineers/ddd17/discount/istacon2016
  97. 97. 15 – 16 November, SofiaISTACon.org Domain-Driven Design • How to identify subdomains? • How to define business entities? • How do define events? • How to talk to business experts?
  98. 98. 15 – 16 November, SofiaISTACon.org
  99. 99. 15 – 16 November, SofiaISTACon.org Thank you! @vladikk vladikk.com vladikkhononov vladik@khononov.com

×