what if we stored events instead of state?

106
WHAT IF WE STORED EVENTS INSTEAD OF STATE?

Upload: jef-claes

Post on 14-Jun-2015

4.039 views

Category:

Technology


0 download

DESCRIPTION

Traditional systems typically only store the current state. This paradigm often puts us in rather nasty situations. How do you optimize for reads, without impacting writes? How can you have a clue of what's going in your system? How do you reproduce bugs..? Learn how event sourced systems address these concerns by, instead of storing the current state, storing the sequence of events that lead to the current state - unleashing a bunch of scenarios impossible using traditional systems.

TRANSCRIPT

Page 1: What if we stored events instead of state?

WHAT IF WE STORED EVENTS INSTEAD OF STATE?

Page 2: What if we stored events instead of state?

@JefClaes - jefclaes.be

Page 3: What if we stored events instead of state?
Page 4: What if we stored events instead of state?
Page 5: What if we stored events instead of state?

SCENARIO’S• The user can put up a card. • The user can move the card to the doing section. • …

Page 6: What if we stored events instead of state?

COMMANDS• Put up a card. • Move card to doing section. • …

Page 7: What if we stored events instead of state?
Page 8: What if we stored events instead of state?
Page 9: What if we stored events instead of state?
Page 10: What if we stored events instead of state?
Page 11: What if we stored events instead of state?

State + Behavior

Page 12: What if we stored events instead of state?
Page 13: What if we stored events instead of state?
Page 14: What if we stored events instead of state?
Page 15: What if we stored events instead of state?

Traditional systems store current state.

Page 16: What if we stored events instead of state?

We have no idea what has happened in our system.

Page 17: What if we stored events instead of state?

Audit logs anyone?

Page 18: What if we stored events instead of state?

State Audit log

Page 19: What if we stored events instead of state?
Page 20: What if we stored events instead of state?

Deposit 500 € 500 €

Deposit 200 € 700 €

Withdraw 300 € 400 €

Deposit 1000 € 1400 €

Withdraw 400 € 1000 €

Withdraw 200 € 800 €

… …

Page 21: What if we stored events instead of state?

Deposit 500 € Deposited 500 € 500 €

Deposit 200 € Deposited 200 € 700 €

Withdraw 300 € Withdrawn 300 € 400 €

Deposit 300 € Deposited 1000 € 1400 €

Withdraw 400 € Withdrawn 400 € 1000 €

Withdraw 200 € Withdrawn 200 € 800 €

… …

Page 22: What if we stored events instead of state?

Command - Event - State

Page 23: What if we stored events instead of state?

Something that happened - past tense.

Page 24: What if we stored events instead of state?

Store the sequence of events that led up to the current state, instead of the current state.

Page 25: What if we stored events instead of state?

Command State

Put up a new card“Reset Password”

Card“Reset Password”

“Todo”

Move card to doingCard

“Reset Password”“Doing”

… …

Page 26: What if we stored events instead of state?

Command Event State

Put up a new card“Reset Password”

Card put up“Reset Password”

“Todo”

Card“Reset Password”

“Todo”

Move card to doing Moved card to section “Doing”

Card“Reset Password”

“Doing”

… … …

Page 27: What if we stored events instead of state?
Page 28: What if we stored events instead of state?
Page 29: What if we stored events instead of state?
Page 30: What if we stored events instead of state?
Page 31: What if we stored events instead of state?
Page 32: What if we stored events instead of state?
Page 33: What if we stored events instead of state?
Page 34: What if we stored events instead of state?
Page 35: What if we stored events instead of state?
Page 36: What if we stored events instead of state?
Page 37: What if we stored events instead of state?
Page 38: What if we stored events instead of state?
Page 39: What if we stored events instead of state?
Page 40: What if we stored events instead of state?
Page 41: What if we stored events instead of state?
Page 42: What if we stored events instead of state?

thinkbeforecoding.com

Page 43: What if we stored events instead of state?
Page 44: What if we stored events instead of state?
Page 45: What if we stored events instead of state?
Page 46: What if we stored events instead of state?

An event store

Page 47: What if we stored events instead of state?
Page 48: What if we stored events instead of state?

EventId StreamId Type Payload MetaData

1 card/1 CardPutUp { … } { … }

2 card/1 MovedCard ToSection { … } { … }

3 card/1 MovedCardToSection { … } { … }

… … … …

Page 49: What if we stored events instead of state?
Page 50: What if we stored events instead of state?

You can’t undo the past.

Page 51: What if we stored events instead of state?

Append-only.

Page 52: What if we stored events instead of state?

Forever cacheable.

Page 53: What if we stored events instead of state?

What about queries?

Page 54: What if we stored events instead of state?

Model

Writes Reads

Page 55: What if we stored events instead of state?

Write model

Writes

Page 56: What if we stored events instead of state?

Write model

Writes

Read model

Reads

Page 57: What if we stored events instead of state?

Write model

Writes

Read model

Reads

Events

Page 58: What if we stored events instead of state?
Page 59: What if we stored events instead of state?

We can build a read model from events committed to the write model.

Page 60: What if we stored events instead of state?

Events Dispatcher Projection Read model

Page 61: What if we stored events instead of state?

Events Dispatcher Projection Read model

Projection Read model

Page 62: What if we stored events instead of state?

Events Dispatcher Projection Read model

Projection Read model

Projection Read model

Page 63: What if we stored events instead of state?

We can build read models from events committed to the write model.

Page 64: What if we stored events instead of state?

More than one way to look at your data.

Page 65: What if we stored events instead of state?

BehaviorSearch

ReportingViews

Page 66: What if we stored events instead of state?

ComplexSlow

Fragile

Page 67: What if we stored events instead of state?
Page 68: What if we stored events instead of state?
Page 69: What if we stored events instead of state?
Page 70: What if we stored events instead of state?
Page 71: What if we stored events instead of state?
Page 72: What if we stored events instead of state?
Page 73: What if we stored events instead of state?
Page 74: What if we stored events instead of state?
Page 75: What if we stored events instead of state?
Page 76: What if we stored events instead of state?
Page 77: What if we stored events instead of state?
Page 78: What if we stored events instead of state?
Page 79: What if we stored events instead of state?

What if I mess up my read models?What if I need to update my read models?

What if I need a new read model?

Page 80: What if we stored events instead of state?

Temporal queries.

Page 81: What if we stored events instead of state?

Tell me which day most work gets done.

Page 82: What if we stored events instead of state?

Monday Tuesday Wednesday …

CardMovedCardMovedCardMovedCardMovedCardMoved

CardMovedCardMoved

CardMovedCardMovedCardMoved

Page 83: What if we stored events instead of state?
Page 84: What if we stored events instead of state?

Not just for read models and queries.

Page 85: What if we stored events instead of state?
Page 86: What if we stored events instead of state?

Events Dispatcher Projection Read model

Projection Read model

Projection Read model

Integration

Page 87: What if we stored events instead of state?

Pub-Sub

Page 88: What if we stored events instead of state?

Events Dispatcher

Projection

Projection

Projection

Transaction

Page 89: What if we stored events instead of state?

Events Dispatcher

Projection

Projection

Projection

Page 90: What if we stored events instead of state?

Events

Projection

Projection

Projection

Page 91: What if we stored events instead of state?

Events Dispatcher (Queue)

Projection (Queue)

Projection (Queue)

Projection (Queue)

Page 92: What if we stored events instead of state?

When?

Page 93: What if we stored events instead of state?

When you can’t afford to lose data.

Page 94: What if we stored events instead of state?

Business Intelligence Auditing

Reproducing issues Challenging/changing business models

Page 95: What if we stored events instead of state?

Decouple reads from writes

Page 96: What if we stored events instead of state?

Conceptual puritySimplicity (no monolith)

Polyglot persistancePerformance

High availability

Page 97: What if we stored events instead of state?

Integration with other systemsTesting

Page 98: What if we stored events instead of state?

No silver bullet

Page 99: What if we stored events instead of state?

In conclusion

Page 100: What if we stored events instead of state?

Event sourcing, for when the what and the when are important.

Page 101: What if we stored events instead of state?

… but really, it’s a lot more than that.

Page 102: What if we stored events instead of state?

Experiment!

Page 103: What if we stored events instead of state?

http://neventstore.org/http://geteventstore.com/

Page 104: What if we stored events instead of state?

https://groups.google.com/forum/#!forum/dddcqrs

Page 105: What if we stored events instead of state?

@gregyoung

@abdullin

@randompunter

@jonathan_oliver

@jen20

@yreynhout @mathiasverraes

@thinkb4coding@eulerfx

@ziobrando

@tojans

Page 106: What if we stored events instead of state?

Thank you! @JefClaes

http://jefclaes.be