71
Event Sourcing Intro & Chaenges

Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Event Sourcing Intro & Challenges

Page 2: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Michael PlödinnoQ

Principal Consultant@bitboss

Page 3: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Most current systems only store the current

state

Page 4: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

IncidentRestController

IncidentBusinessService

IncidentDAO

Incident

ID USER_ID DATE TEXT1 23423 11.03.2014 Maus defekt 2 67454 12.03.2014 EMail Empfang3 93729 12.03.2014 Monitor defekt… … … …

Classical Architecture

Page 5: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

IncidentRestController

IncidentBusinessService

IncidentDAO

Incident

ID USER_ID DATE TEXT1 23423 11.03.2014 Maus ist kaputt2 67454 12.03.2014 EMail Empfang3 93729 12.03.2014 Monitor defekt… … … …

Update

The dataset is directly being changed, no history

Page 6: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

1 Audit Log only with extra effort

2 No replay

3 Snapshots only with extra effort or via backups

Page 7: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

? Issues

Page 8: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Many applications are working well with the classic

approach

Page 9: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

However there are areas where this approach reaches

it’s limitations

Page 10: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

? Reactive

Page 11: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

? Responsive

? Resilient

? Elastic

? Message driven

Page 12: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

IncidentRestController

IncidentBusinessService

IncidentDAO

Incident

Not quite reactive…

Database

Page 13: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Event Sourcing is an architectural pattern in which the state of the

application is being determined by a

sequence of events

Page 14: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Building Blocks

Applications

Event Queue

Applications issues events to a queue

Event Handler

The Event handler is processing the

events

Event Store

The event store is persisting the

events

Page 15: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

An event is something that happened in the past

tjetzt

EventEventEventEventEvent

Page 16: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

The names of the Events are part of the

Ubiquitous Language

D D D

Page 17: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

ShipmentDeliveredEvent

CustomerVerifiedEvent

CartCheckedOutEvent

CreateCustomerEvent WillSaveItemEvent

DoStuffEvent

Page 18: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Code Examplepublic class CustomerVerifiedEvent {

private String eventId;

private Date eventDate;

private CustomerNumber customerNumber;

private String comment;

public CustomerVerifiedEvent(CustomerNumber custNum, String comment) {this.customerNumber = cusNum;this.comment = comment;this.eventDate = new Date();

}

}

Page 19: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Scope your events on the basis of

Aggregates

D D D

Page 20: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

An Event is always immutable!

Page 21: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

The sequence of events in the queue is also called

Event Stream

tnow

EventEventEventEventEvent

Page 22: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

IncidentCreatedEvent incidentNumber: 1 userNumber: 23423timestamp: 11.03.2014 12:23:23 text: „Maus defekt“status: „offen“

Event Stream Example

IncidentTextChangedEvent incidentNumber: 1 text: „Maus ist Kaputt“

IncidentClosedEvent incidentNumber: 1 solution: „Neue Maus“status: „geschlossen“

Page 23: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Let’s reuse the ESB from the failed SOA

project

Page 24: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

NO

Page 25: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

NO

Page 26: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

NO

Page 27: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

!Prefer dumb pipes with smart endpoints as a suitable message broker architecture

Page 28: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

1 Complete rebuild is possible

2 Temporal Queries

3 Event Replay

Page 29: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Well known examples

=Version Control Systems

or Database Transaction Logs

Page 30: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

The Event Store has a very high business value

Page 31: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

There is no delete!!

Page 32: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

A delete is just another

event

IncidentCreatedEvent incidentNumber: 1 userNumber: 23423timestamp: 11.03.2014 12:23:23 text: „Maus defekt“status: „offen“

IncidentChangedEvent incidentNumber: 1 text: „Maus ist Kaputt“

IncidentClosedEvent incidentNumber: 1 solution: „Neue Maus“status: „geschlossen“

IncidentRemovedEvent incidentNumber: 1

Page 33: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Aren’t there performance issues attached to this kind of

data storage?

Page 34: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

YES!

Page 35: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Application State

Think about applciation state

Application

Event Queue

Event Handler

Event Store

Application State

The application queries the pre-processed application state

Page 36: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

CQRS

Page 37: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Command Query Responsibility Separation

Page 38: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

IncidentSOAPEndpoint

IncidentBusinessService

IncidentDAO

IncidentBusiness Model

Client

Incident DTO

Incident View Model

RDBMS

Incident ER-Model

Netzwerk

Netzwerk

Page 39: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

EventHandler EventsEvents

Event Sourcing & CQRSIncidentCommandEndpoint

IncidentCommandService

IncidentCommandDAO

IncidentQueryEndpoint

IncidentQueryService

IncidentQueryDAO

Read Storage

Events

Select * from

Event Store

Page 40: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Event Sourcing is an interesting architectural option. However there are various challanges,

that have to be taken care of

Page 41: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

1 Consistency

2 Validation

3 Parallel Updates

Page 42: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

YES

Page 43: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

!Systems based on CQRS and Event

Sourcing are mostly eventually

consistent

Page 44: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

EventHandler EventsEvents

Eventual ConsistencyIncidentCommandEndpoint

IncidentCommandService

IncidentCommandDAO

IncidentQueryEndpoint

IncidentQueryService

IncidentQueryDAO

Read Storage

Events

Select * from

Event Store

Page 45: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

BUT

Page 46: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

!You can build a fully consistent system which follows Event

Sourcing principles

Page 47: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

EventHandler

EventsEvents

Full ConsistencyIncidentCommandEndpoint

IncidentCommandService

IncidentCommandDAO

IncidentQueryEndpoint

IncidentQueryService

IncidentQueryDAO

Read Storage

EventsSelect * from

Event Store

Page 48: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Your business domain drives the level of consistency not

technologyDeeper Insight

D D D

Page 49: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Increased (but still eventual) consistency

EventHandler

EventsEvents

IncidentCommandEndpoint

IncidentCommandService

IncidentCommandDAO

IncidentQueryEndpoint

IncidentQueryService

IncidentQueryDAO

Read Storage

EventsSelect * from

Event Store

async

Page 50: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

! There is no standard solution

Page 51: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

1 Consistency

2 Validation

3 Parallel Updates

Page 52: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Example DomainUser

Guid idString emailString password

RegisterUserCommand ChangeEmailCommand

UserRegisteredEvent Guid idDate timestampString emailString password

EmailChangedEvent Guid userIdDate timestampString email

Page 53: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

We process 2 million+ registrations per day. A user

can change her email address. However the emails address

must be unique

Page 54: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

?How high is the

probability that a validation fails

Which data is required for the validation

Where is the required data stored

Page 55: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

$What is the business

impact of a failed validation that is not

recognized due to eventual consistency and how high is the

probability of failure

Page 56: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Your business domain drives the level of consistency

Deeper Insight

D D D

Page 57: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

1 Validate from Event Store

2 Validate from Read Store

3 Perform Validation in Event Handler

Page 58: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

EventHandler EventsEvents

ValidationIncidentCommandEndpoint

IncidentCommandService

IncidentCommandDAO

IncidentQueryEndpoint

IncidentQueryService

IncidentQueryDAO

Read Storage

Events

Select * from

Event Store

Page 59: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

1 Consistency

2 Validation

3 Parallel Updates

Page 60: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Example DomainUser

Guid idString emailString password

RegisterUserCommand ChangeEmailCommand

UserRegisteredEvent Guid idDate timestampString emailString password

EmailChangedEvent Guid userIdDate timestampString email

Page 61: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

What happens when Alice and Bob share an

account and both update the email address at the same

time

Page 62: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

? What would we do in a

„classic old school architecture“

Page 63: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

UserRestController

UserBusinessService

UserDAO

User

ID EMAIL PASSWORD… … …

2341 [email protected] lksjdaslkdjas … … …

Update

Pessimistic or optimistic locking

Page 64: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Your business domain drives the locking quality

Deeper Insight

D D D

Page 65: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

!Pessimistic locking on a

data-level will hardly work in event sourcing architectures

Page 66: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

EventHandler

EventsEvents

Where to „pessimistically“ lock?

Read Storage

Events

Event Store

UserRestController

UserBusinessService

UserDAO

User

Commands

Consider a business lock with a UserLockedEvent

Page 67: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

? Do you

REALLY need a full lock

Most „classic architecture“

applications are already running fine with

optimistic locks

Page 68: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Introduce a version field for the domain entity

User

Guid idLong versionString emailString password

RegisterUserCommand ChangeEmailCommand

UserRegisteredEvent Guid idDate timestampString emailString password

EmailChangedEvent Guid userIdDate timestampString emailLong version

Page 69: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Each writing event increases the version

UserRegisteredEvent {guid: 12, version: 0, email: [email protected], password: werwe2343}

EmailChangedEventversion: 0

EmailChangedEventversion: 1

EmailChangedEventversion: 1

{guid: 12, version: 1, email: [email protected], password: werwe2343}

{guid: 12, version: 2, email: [email protected], password: werwe2343}

EmailChangeFailedEvent

Page 70: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Also here:you should be as

consistent as your domain

requires

Page 71: Event Sourcing Reactive English - Meetupfiles.meetup.com/14341692/Event Sourcing... · Event Sourcing is an architectural pattern in ... application is being determined by a sequence

Thanks!Michael Plöd

https://www.innoq.com/de/trainings/microservices-training/

@bitbosshttp://slideshare.net/[email protected]