Top Banner
100

ISTA 2016: Event Sourcing

Apr 16, 2017

Download

Engineering

Vladik Khononov
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ISTA 2016: Event Sourcing
Page 2: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Event Sourcing

By Vladik Khononov

A New Dimension in Software Design

Page 3: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Page 4: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Page 5: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Page 6: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Agenda• How it happened

• Why it happened

• How event sourcing solves the problem

• Why event sourcing benefits the company

Page 7: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Call Center Management

Page 8: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

LEAD• Id• Name• Email• Phone• Status (Available / Follow-up / Closed / Converted)

Page 9: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Name Email Phone Status

Stefan Ivanov [email protected] 03-27243457 Available

Viktor Kovachev [email protected] 08-8332491 Available

Martin Mateev [email protected] 04-8537112 Available

Lilyana Yankov [email protected] 04-6092212 Available

Page 10: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Name Email Phone Status

Stefan Ivanov [email protected] 03-27243457 Available

Viktor Kovachev [email protected] 08-8332491 Available

Martin Mateev [email protected] 04-8537112 Available

Lilyana Yankov [email protected] 04-6092212 Available

Page 11: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Page 12: ISTA 2016: Event Sourcing

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

}

Page 13: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Page 14: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

SELECT * FROM leads

WHERE [name] LIKE ‘%name%’ OR

[phone] LIKE ‘%phone%’ OR

[email] LIKE ‘%email%’;

Page 15: ISTA 2016: Event Sourcing

WTF???

ISTACon.org

Page 16: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Lead #1410

Name Viktor Radkov

Email [email protected]

Phone 09-9801298

Status Available

Page 17: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Page 18: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

PM

Page 19: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

…1 month later

Page 20: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Page 21: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

PM

Page 22: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

…2 months later

Page 23: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Page 24: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

PM

Page 25: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Search on stale data

Status change dates

Audit log for BI

Page 26: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

PM

Page 27: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

PM

Page 28: ISTA 2016: Event Sourcing

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

}

Page 29: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Name Email Phone Status Follow-up

Stefan Ivanov [email protected] 03-27243457 Available

Page 30: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Name Email Phone Status Follow-up Date

Stefan Ivanov [email protected] 050-8139904 Follow-up 23/11/2016

Page 31: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Name Email Phone Status Follow-up Date

Stefan Ivanov [email protected] 050-8139904 Converted

Page 32: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Name Email Phone Status Follow-up Date

Stefan Ivanov [email protected] 050-8139904 Converted

Page 33: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Page 34: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Event Sourcing

Page 35: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Changes=

First class citizens=

Events

Page 36: ISTA 2016: Event Sourcing

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)

Page 37: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Lead: Stefan Ivanov• Lead Was Initialized (1410)• Status Changed (Available)• Contact Information Changed ([email protected], 03-27243457)• Status Changed (Followup)• Followup Set (23/11/2016)• Contact Information Changed ([email protected], 050-8139904)• Status Changed (Converted)

Page 38: ISTA 2016: Event Sourcing

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) {…}

}

Page 39: ISTA 2016: Event Sourcing

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;

}

Page 40: ISTA 2016: Event Sourcing

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;

}

Page 41: ISTA 2016: Event Sourcing

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;

}

Page 42: ISTA 2016: Event Sourcing

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;

}

Page 43: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Lead: Stefan IvanovLead Was Initialized (1410)Status Changed (Available)

Contact Information Changed ([email protected],03-27243457)

Status Changed (Converted)

Status Changed (Followup)Followup Set (23/11/2016)

Contact Information Changed ([email protected], 050-8139904)

Page 44: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Name Email Phone Status Follow-up Date

Stefan Ivanov [email protected] 050-8139904 Converted

Page 45: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Lead: Stefan IvanovLead Was Initialized (1410)Status Changed (Available)

Contact Information Changed ([email protected],03-27243457)

Status Changed (Converted)

Status Changed (Followup)Followup Set (23/11/2016)

Contact Information Changed ([email protected], 050-8139904)

Page 46: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Lead: Stefan IvanovLead Was Initialized (1410)Status Changed (Available)

Contact Information Changed ([email protected],03-27243457)

Page 47: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Lead: Stefan IvanovLead Was Initialized (1410)Status Changed (Available)

Contact Information Changed ([email protected],03-27243457)

Status Changed (Followup)Followup Set (23/11/2016)

Contact Information Changed ([email protected], 050-8139904)

Page 48: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Lead: Stefan IvanovLead Was Initialized (1410)Status Changed (Available)

Contact Information Changed ([email protected],03-27243457)

Status Changed (Converted)

Status Changed (Followup)Followup Set (23/11/2016)

Contact Information Changed ([email protected], 050-8139904)

Page 49: ISTA 2016: Event Sourcing

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

}

Page 50: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Lead: Stefan IvanovLead Was Initialized (1410)Status Changed (Available)

Contact Information Changed ([email protected],03-27243457)

Status Changed (Converted)

Status Changed (Followup)Followup Set (23/11/2016)

Contact Information Changed ([email protected], 050-8139904)

Page 51: ISTA 2016: Event Sourcing

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);

}

Page 52: ISTA 2016: Event Sourcing

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);

);

Page 53: ISTA 2016: Event Sourcing

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;…

}

Page 54: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Lead: Stefan IvanovLead Was Initialized (1410)Status Changed (Available)

Contact Information Changed ([email protected],03-27243457)

Status Changed (Converted)

Status Changed (Followup)Followup Set (23/11/2016)

Contact Information Changed ([email protected], 050-8139904)

Page 55: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Events = Source of Truth

Event Sourcing

Page 56: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Page 57: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Page 58: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Page 59: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Storage: Event Store

Page 60: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Event Store API: The Good News

Append(Entity Id + New events)

GetEvents(Entity Id)Event1,Event2,Event3,

….

Page 61: ISTA 2016: Event Sourcing

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”)

Page 62: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Event Store API: The Good News

Append(Entity Id + New events)

GetEvents(Entity Id)Event1,Event2,Event3,

….

Page 63: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Event Store API: The Bad News

Append(Entity Id + New events)

GetEvents(Entity Id)Event1,Event2,Event3,

….

Page 64: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

CQRSCommand Query Responsibility Segregation

Page 65: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

CQRS• Command - write data• Query - read data• A use case can be either Command or Query. Never both

Page 66: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Queries

Read Model

Read DB

Commands

Write Model

Writes DBProjection engine

Page 67: ISTA 2016: Event Sourcing

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 …

Page 68: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Queries

Read Model

Read DB

Commands

Write Model

Writes DBProjection engine

Page 69: ISTA 2016: Event Sourcing

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

Page 70: ISTA 2016: Event Sourcing

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;

}

Page 71: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Queries

Read Model

Read DB

Commands

Event Sourcing

Event StoreProjection engine

Page 72: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Queries

Read Model

Read DB

Commands

Write Model

Writes DBProjection

engine

Projection• RDBMS• Documents• Graphs• Files

Page 73: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Queries

Read Model

Commands

Write Model

Writes DBProjection

engine

Projection• RDBMS• Documents• Graphs• Files• Multiple DBs

Read DBs

Page 74: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Queries

Read Model

Commands

Write Model

Writes DBProjection

engine

Read DBs

Concurrency• Pessimistic• Optimistic• Optimistic on steroids

Page 75: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

SetFollowupCommand + StatusChangeEvent => Collision

ChangeNameCommand + StatusChangeEvent => OK

Page 76: ISTA 2016: Event Sourcing

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.

Page 77: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Page 78: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

CQRS: When?

Event Sourcing ➡ CQRS

Non-functional benefits

Page 79: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Event Sourcing

CQRS

Page 80: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Event Sourcing: When?

?

Page 81: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

SubdomainsGeneric, Supporting, Core

Page 82: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Page 83: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

SubdomainsGeneric, Supporting, Core

Page 84: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

CREATIVE CATALOG

CAMPAIGN PUBLISHING

BILLING USERS MANAGEMENT

LEAD MANAGEMENTCOMMISSIONS CALCULATION

DESK MANAGEMENTVOIP MANAGEMENT

EMAIL CAMPAIGNS

Page 85: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Generic Subdomains

Page 86: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

CREATIVE CATALOG

CAMPAIGN PUBLISHING

BILLING USERS MANAGEMENT

LEAD MANAGEMENTCOMMISSIONS CALCULATION

DESK MANAGEMENTVOIP MANAGEMENT

EMAIL CAMPAIGN

Page 87: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

CREATIVE CATALOG

CAMPAIGN PUBLISHING

BILLING USERS MANAGEMENT

LEAD MANAGEMENTCOMMISSIONS CALCULATION

DESK MANAGEMENTVOIP MANAGEMENT

EMAIL CAMPAIGN

Page 88: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Supporting Subdomains

Page 89: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

CREATIVE CATALOG

CAMPAIGN PUBLISHING

BILLING USERS MANAGEMENT

LEAD MANAGEMENTCOMMISSIONS CALCULATION

DESK MANAGEMENTVOIP MANAGEMENT

EMAIL CAMPAIGN

Page 90: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

CREATIVE CATALOG

CAMPAIGN PUBLISHING

BILLING USERS MANAGEMENT

LEAD MANAGEMENTCOMMISSIONS CALCULATION

DESK MANAGEMENTVOIP MANAGEMENT

EMAIL CAMPAIGN

Page 91: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Core Domains

Page 92: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

CREATIVE CATALOG

CAMPAIGN PUBLISHING

BILLING USERS MANAGEMENT

LEAD MANAGEMENTCOMMISSIONS CALCULATION

DESK MANAGEMENTVOIP MANAGEMENT

EMAIL CAMPAIGN

Page 93: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Event Sourcing: When?

Core business domains

Page 94: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Before you try this at home

Page 95: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Available Event Stores

• http://GetEventStore.com

• NEventStore

• Akka Persistence

• Elastic Event Store (Coming soon)

Page 96: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Page 97: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

€100 Discount for attendees of ISTA 2016https://ti.to/webengineers/ddd17/discount/istacon2016

Page 98: ISTA 2016: Event Sourcing

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?

Page 99: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Page 100: ISTA 2016: Event Sourcing

15 – 16 November, SofiaISTACon.org

Thank you!

@vladikk

vladikk.com

vladikkhononov

[email protected]