Event Sourcing Cameron Fletcher. Agenda What is an Event? Events as a Storage Mechanism ...

27
Event Sourcing Cameron Fletcher

Transcript of Event Sourcing Cameron Fletcher. Agenda What is an Event? Events as a Storage Mechanism ...

Page 1: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Event Sourcing

Cameron Fletcher

Page 2: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Agenda

What is an Event?

Events as a Storage Mechanism

Versioning

Testing with Events

Performance and Scalability

Demonstration

Page 3: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

What is an Event?

Page 4: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

An Event

An event is something that has happened in the past as a result of a change in state of our domain

Fill glassRequest to change state

Glass has been filledEvent

Glass is already full!

Glass is broken!Exception

Page 5: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Events as aStorage Mechanism

Page 6: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.
Page 7: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.
Page 8: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Carts

CartId HasCheckedOut

1 False

CartItems

CartId ItemId Name

1 1 Domain Driven Design

1 2 Enterprise Integration Patterns

1 3 Event Centric

Item Added

Item Added

Item Added

Checkout Completed

Shopping Cart Created

True

Page 9: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Item Added

Item Added

Item Added

Checkout Completed

Shopping Cart Created

Domain Object

Page 10: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Item Added

Item Added

Item Added

Item Removed

Shopping Cart Created

Checkout Completed

Page 11: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Versioning

Page 12: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

7

6

5

4

3

2

1

snapshot

Page 13: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Cart Domain Object Example// request to mutate statepublic void Add(Item item){

if (this.itemCount > 5)throw new CartFullException(this.id);

ApplyChange(new ItemAdded(this.id, item.Name));

}

// mutate stateprivate void Apply(ItemAdded @event){

this.itemCount++;}

Page 14: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Domain Object Base

protected void ApplyChange(Event @event){

this.ApplyChange(@event, true);}

private void ApplyChange(Event @event, bool isNew){

this.AsDynamic().Apply(@event);

if (isNew)this.uncommittedChanges.Add(@event);

}

Page 15: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Domain Object Reconstitutionpublic void LoadFromHistory(IEnumerable<Event> history){

foreach (var @event in history)this.ApplyChange(@event, false);

}

Page 16: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Domain Object Persistencepublic IEnumerable<Event> GetUncommittedChanges() {

return this.uncommittedChanges;}

public void MarkChangesAsCommitted(){

this.uncommittedChanges.Clear();}

Page 17: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Testing with Events

Page 18: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Overdraw Attempts are Rejected

Given

An account with a balance of 100

When

A debit is requested for 101

Then

An InsufficientBalanceException is thrown

Page 19: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Overdraw Attempts are Rejected

Given

An account is created

A deposit was made for 100

When

A debit is requested for 101

Then

An InsufficientBalanceException is thrown

Page 20: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Overdraw Attempts are Rejected

Given

A series of events

When

A command

Then

Zero or more events, or an exception is thrown

Page 21: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Overdraw Attempts are Rejected

Given

An account is created

A deposit was made for 100

A debit is requested for 101

When

A debit is requested for 20

Then

An InsufficientBalanceException is thrown

Page 22: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Performance and Scalability

Page 23: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Performance and Scalability Benefits

Append-only model

Partitioning (Sharding)

Persisting Objects

Reconstituting Objects

Page 24: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Demonstration

Page 25: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Summary

Page 26: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.

Questions?

Page 27: Event Sourcing Cameron Fletcher. Agenda  What is an Event?  Events as a Storage Mechanism  Versioning  Testing with Events  Performance and Scalability.