+ All Categories
Home > Documents > An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth...

An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth...

Date post: 23-Jun-2020
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
288
An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering, Culture Amp @vonconrad
Transcript
Page 1: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

An In-Depth Look at Event Sourcing with CQRS

Sebastian von Conrad Director of Engineering, Culture Amp @vonconrad

Page 2: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

👋

Page 3: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,
Page 4: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Agenda ‣ Event Sourcing

‣ Event Sourcing with CQRS

‣ With a lil’ dose of DDD

‣ Upside

‣ Downside

‣ Eventual Consistency

Page 5: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing

Page 6: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

What is Event Sourcing?

Page 7: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Many systems log “events.”

Page 8: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Logging ≠

Event Sourcing

Page 9: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing forces current state to be sourced from all past history.

Page 10: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Greg Young

Page 11: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing isn’t new.

Page 12: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

At least 500 years old.

Allegedly tracked back to Ancient Mesopotamia.

Page 13: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Most mature industries are event sourced.

Page 14: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Finance, accounting, insurance, medical, legal, etc.

Page 15: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Bank account.

Page 16: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Do you think your bank account balance is simply a column in a

current state database?

Page 17: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

What if you disagree with your bank?

Page 18: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The account balance is an equation.

Page 19: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

balance = sum(transaction.amount)

Page 20: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

You can manually calculate this.

Page 21: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing works the same way.

Page 22: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

All current state is transient.

Page 23: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Your source of truth are events, not state.

Page 24: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

In this context, events describe things that have happened that are of

significant interest to you.

Page 25: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Events are business facts.

Page 26: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Events are expressed in past tense, because they have happened.

Page 27: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Bank domain events

‣ New Account Application Received

‣ Customer Identity Verified

‣ Account Opened

‣ Funds Deposited

‣ Funds Withdrawn

‣ Funds Transferred

‣ Account Statement Issued

Page 28: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Calendar domain events

‣ Appointment Scheduled

‣ Appointment Rescheduled

‣ Invitation Extended

‣ Invitation Sent

‣ Invitation Accepted

‣ Invitation Declined

‣ Appointment Canceled

Page 29: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Culture Amp domain events

‣ Employee Added

‣ Question Description Adjusted

‣ Survey Launched

‣ Question Answered

‣ Additional Comment Added

‣ Survey Closed

‣ Report Shared

‣ Action Item Committed

Page 30: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

With Event Sourcing, events are the centre of your domain.

Modelling them well is crucial.

Page 31: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Shopping Cart modelled as “current state”

CartShipping Address

Line ItemsLine ItemsLine Items

Could be a document, a table in a relational database, a document in a document database, or something else.

Page 32: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We’ve been taught that the shape of this data is the most important thing.

Page 33: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

But it can also be expressed in events.

Page 34: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Shopping Cart as Events

Cart Emptied

Shipping Address Provided

Item Added To Cart

Item Added To Cart

Page 35: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Shopping Cart “current state”

CartShipping Address

Line ItemsLine ItemsLine Items

Page 36: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Cart Emptied

Shipping Address Provided

Item Added To Cart

Item Added To Cart

What we end up storing, however, are the events.

Page 37: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

With Event Sourcing, all current state is disposable.

Page 38: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Current state is a cache.

A cache can be deleted and regenerated.

Page 39: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing just a different way of storing information.

Page 40: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Though it has fundamental consequences.

Page 41: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

With Event Sourcing, data is append-only.

Page 42: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

With Event Sourcing, data is immutable.

Page 43: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

So how do we deal with mistakes?

Page 44: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Well, how does an accountant deal with mistakes?

Page 45: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We call these correction events.

Page 46: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Shopping Cart “correction” event

Cart Emptied

Shipping Address Provided

Item Added To Cart

Item Added To Cart

Item Removed From Cart

Page 47: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Are these the same thing?

Cart Emptied

Shipping Address Provided

Item Added To Cart

Item Added To Cart

Item Removed From Cart

Cart Emptied

Shipping Address Provided

Item Added To Cart

Page 48: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The current state is the same!

CartShipping Address

Line ItemsLine ItemsLine Items

Page 49: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

In your system:

Can you think of two use cases that result in the same current state?

Page 50: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

If you can, you’re losing data.

Page 51: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Follow-up question:

How do you determine what data you’re happy to lose?

Page 52: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

I try to never lose data.

Because I don’t know how to value it.

Page 53: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

This is the main benefit of Event Sourcing.

Page 54: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

CEO: “Let’s sell people some things they were interested in

but didn’t end up buying.”

Page 55: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Adding to current state model

Cart Shipping Address

Line ItemsLine ItemsLine Items

Line ItemsLine ItemsRemoved Line Items

Page 56: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Shopping Cart “correction” event

Cart Emptied

Shipping Address Provided

Item Added To Cart

Item Added To Cart

Item Removed From Cart

Page 57: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We can write a projection. (More on this later.)

Page 58: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We’re not losing data.

And we can come up with new and interesting ways to interpret it.

Page 59: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We also get a time machine.

Page 60: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Want to know what that report looked like at 2016-11-30 16:56:09 UTC?

No problem.

Page 61: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

An event sourced system is deterministic.

Page 62: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Sure, sure, that’s all well and good but it can’t possibly scale!

Page 63: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

What if I have a lot of events?

Page 64: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing with CQRS

Page 65: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Command Query Responsibility Segregation

Page 66: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Effectively, separating reads from writes.

Page 67: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

“Asking a question should not change the answer.

- Bertrand Meyer

Page 68: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

It’s possible to use Event Sourcing without CQRS.

Page 69: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

It’s possible to use CQRS without Event Sourcing.

Page 70: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

They just go really well together.

Page 71: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Four major building blocks to the architecture

‣ Event Store

‣ Reading with Queries

‣ Writing with Commands

‣ (Reactors)

Page 72: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Store.

Page 73: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Events are kept in a database called the “Event Store.”

Page 74: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The Event Store is usually considered a sub-system in its own right.

Page 75: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

There are commercial and open-source options available.

Or you can roll your own.

Page 76: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The simplest event store has a single event “stream.”

Page 77: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The simplest Event Store has but three features.

These are all we’ll make use of.

‣Put event.

‣Get all events, in order, from an offset.

‣Get all events, in order, scoped by <something>.

Page 78: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Reading with Queries.

Page 79: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Clients need to read (and display) information.

Page 80: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Clients are usually interested in current state.

Page 81: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The Event Store is not suitable for presenting current state.

Client

Event Store

x

Page 82: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

So we need something that Clients can query for current state.

Page 83: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We call this a Query Handler.

Page 84: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

It’s a little detour. Client

Event Store

Query Handler

Page 85: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The Query Handler “tails” the event stream from the Event Store.

Event Store

Query Handler

Page 86: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

…and builds up a projection.

Event Store

Query Handler

Page 87: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

When a Client wants to know something, the query handler use the projection. Query

Handler

Client

Page 88: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

And returns the data to the Client.

Query Handler

Client

Page 89: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

There are many (N) projections, each optimised for a particular “query.”

Page 90: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The Query Handler will pick a projection for a query from a client.

Client

Query Handler

Page 91: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Each of which has their own “tailing” process called a projector that read from the stream.

Event Store

Page 92: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

This is not using the Observer pattern.

Page 93: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We use the following Event Store feature(s):

‣Put event.

‣Get all events, in order, from an offset.

‣Get all events, in order, scoped by <something>.

Page 94: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The projectors keep a “pointer” or “bookmark” for where in the event stream they’re at.

51 56 55 49 57 55

Event Store

Page 95: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The projections are disposable and can be rebuilt by going back to 0.

Event Store

0

Page 96: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The projectors and projections are completely decoupled from each other.

Page 97: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

This allows different data storage options to solve different problems.

Page 98: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

This allows different data storage options to solve different problems.

Relational DB

Document DB

Search Engine

Graph DB

Cache

Data Warehouse

Flat files

Page 99: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We can optimise for how we want to read the data.

Page 100: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

“Current” current state read models tend to be normalised, which is

optimised for writing data.

Page 101: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Projections can be (and often are) denormalised.

Page 102: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Projections are cheap.

Page 103: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

New interpretation? New projection!

Event Store

Query Handler

Page 104: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

When caught up, we can swap to reading from the new projection.

Event Store

Query Handler

Page 105: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

And then we can decommission the old projection.

Event Store

Query Handler

xx

Page 106: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

No more database migrations on production tables.

We simply replace the tables.

Page 107: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Summary: Reading with Queries

Page 108: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Summary: Reading with Queries

‣ Clients never query the events directly.

Page 109: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Summary: Reading with Queries

‣ Clients never query the events directly.

‣ Clients query denormalised projections that are optimised for querying.

Page 110: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Summary: Reading with Queries

‣ Clients never query the events directly.

‣ Clients query denormalised projections that are optimised for querying.

‣ Projections are built up by projectors that process the event stream.

Page 111: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Summary: Reading with Queries

‣ Clients never query the events directly.

‣ Clients query denormalised projections that are optimised for querying.

‣ Projections are built up by projectors that process the event stream.

‣ Projectors are decoupled from each other and don’t share any state.

Page 112: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Summary: Reading with Queries

‣ Clients never query the events directly.

‣ Clients query denormalised projections that are optimised for querying.

‣ Projections are built up by projectors that process the event stream.

‣ Projectors are decoupled from each other and don’t share any state.

‣ Projections are cheap and easy to build and rebuild.

Page 113: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Writing with Commands.

Page 114: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

In addition to reading, Clients also want to write.

Page 115: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We established that Clients should not query the Event Store directly.

Page 116: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Nor should Clients write directly to the Event Store.

Page 117: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The Event Store is not responsible for validation, and so giving Clients

direct access is dangerous.

Page 118: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Instead, Clients express their intentions in the form of Commands.

Page 119: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Just like we had something responsible for handling queries, we need something responsible for handling commands.

Page 120: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

It’s called a Command Handler.

Client

Event Store

Command Handler

Page 121: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Commands express intent.

Page 122: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event and command naming

Events (past tense)

‣ Account Opened

‣ Funds Deposited

‣ Funds Withdrawn

‣ Funds Transferred

‣ Account Statement Issued

Page 123: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event and command naming

Events (past tense)

‣ Account Opened

‣ Funds Deposited

‣ Funds Withdrawn

‣ Funds Transferred

‣ Account Statement Issued

Commands (imperative)

‣ Open Account

‣ Deposit Funds

‣ Withdraw Funds

‣ Transfer Funds

‣ Issue Account Statement

Page 124: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Commands represent intent and can be rejected.

Events are facts and cannot.

Page 125: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

A Command that is accepted results in an event.

Page 126: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

So how does the Command Handler validate Commands?

Page 127: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

This is where we start borrowing from DDD.

Page 128: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,
Page 129: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Events happen to “something.”

Page 130: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We call that “something” an Aggregate.

This is a DDD term.

Page 131: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The Aggregates are the nouns in our system.

Page 132: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Bank domain events and aggregates

Events

‣ New Account Application Received

‣ Customer Identity Verified

‣ Account Opened

‣ Funds Deposited

‣ Funds Withdrawn

‣ Funds Transferred

‣ Account Statement Issued

Page 133: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Bank domain events and aggregates

Events

‣ New Account Application Received

‣ Customer Identity Verified

‣ Account Opened

‣ Funds Deposited

‣ Funds Withdrawn

‣ Funds Transferred

‣ Account Statement Issued

Aggregates

Account Application

Customer

Account

Transfer

Account Statement

Page 134: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Calendar domain events

‣ Calendar

‣ Appointment

‣ Invitation

Page 135: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Culture Amp domain aggregates

‣ Employee

‣ Demographic

‣ Survey

‣ Question

‣ Response

‣ Comment

‣ Report

‣ Action Item

Page 136: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Aggregates have identity that we need to track over time.

Often, they have lifecycles.

Page 137: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Aggregates can refer to each other.

E.g. Response Survey.

Page 138: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

When a Command is received, it is executed against an Aggregate.

Page 139: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The Aggregate determines whether to accept or reject the Command.

Page 140: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

To do that, the Aggregate needs to know its current state.

Page 141: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

events = EventStore.load(aggregate_id)

Page 142: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

events = EventStore.load(aggregate_id)

aggregate = Aggregate.new

Page 143: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

events = EventStore.load(aggregate_id)

aggregate = Aggregate.new

aggregate.replay_events(events)

Page 144: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

events = EventStore.load(aggregate_id)

aggregate = Aggregate.new

aggregate.replay_events(events)

aggregate.attempt_command(payload)

Page 145: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

events = EventStore.load(aggregate_id)

aggregate = Aggregate.new

aggregate.replay_events(events)

aggregate.attempt_command(payload) # => true

Page 146: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

events = EventStore.load(aggregate_id)

aggregate = Aggregate.new

aggregate.replay_events(events)

aggregate.attempt_command(payload) # => false

Page 147: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We use the following Event Store feature(s):

‣Put event.

‣Get all events, in order, from an offset.

‣Get all events, in order, scoped by <something>.

Page 148: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We use the following Event Store feature(s):

‣Put event.

‣Get all events, in order, from an offset.

‣Get all events, in order, scoped by aggregate_id.

Page 149: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Isn’t all of this replaying slow?

Page 150: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

It can be.

Page 151: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

But it’s only on write.

Page 152: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Keep the number of events per Aggregate low.

Page 153: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

An Aggregate that accumulates an unbounded number of events

over time is a smell.

Page 154: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Wherever possible, have fixed life Aggregates.

Page 155: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We would not put OrderPlaced events under the Customer Aggregate.

Instead we have an Order Aggregate.

Page 156: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Domain modelling is hard. Event sourcing doesn’t change that.

Page 157: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Summary: Writing with Commands

Page 158: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Summary: Writing with Commands

‣ Clients never write the events directly.

Page 159: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Summary: Writing with Commands

‣ Clients never write the events directly.

‣ Clients express an intent to do something via commands.

Page 160: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Summary: Writing with Commands

‣ Clients never write the events directly.

‣ Clients express an intent to do something via commands.

‣ Commands are validated by Aggregates, which is a concept borrowed from DDD.

Page 161: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Summary: Writing with Commands

‣ Clients never write the events directly.

‣ Clients express an intent to do something via commands.

‣ Commands are validated by Aggregates, which is a concept borrowed from DDD.

‣ Aggregates fetch events from the Event Store, and replay them to reconstitute their current state.

Page 162: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Summary: Writing with Commands

‣ Clients never write the events directly.

‣ Clients express an intent to do something via commands.

‣ Commands are validated by Aggregates, which is a concept borrowed from DDD.

‣ Aggregates fetch events from the Event Store, and replay them to reconstitute their current state.

‣ If the Aggregate accepts the Command, it results in an event.

Page 163: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Where does this leave us?

Page 164: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The Circular Architecture

Client

Event Store

Command Handler

Query Handler

Page 165: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Clients express intent through Commands.

CommandClient

Event Store

Command Handler

Query Handler

Page 166: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Commands that are accepted result in Events persisted to the Event Store.

CommandClient

Event Store

Command Handler

Query Handler

Event

Page 167: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Projectors tail the Event Store and builds up projections.

CommandClient

Event Store

Command Handler

Query Handler

EventProjector

Page 168: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Clients retrieve data from the projections via Queries.

CommandClient

Event Store

Command Handler

Query Handler

EventProjector

Query

Page 169: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Reads (queries) and writes (commands)

are decoupled.

Page 170: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Reads and writes can scale independently.

Page 171: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Writes are usually fast, and always consistent.

Page 172: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Reads are fast, and eventually consistent.

(We’ll talk more about this later.)

Page 173: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Reactors.

Page 174: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Reactors act like projectors, but instead of building projections, they react to events.

(Hence the name.)

Client

Event Store

Command Handler

Query Handler

Reactor

Page 175: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Either through triggering external behaviour…

Client

Event Store

Command Handler

Query Handler

ReactorExternal Behaviour Trigger

Page 176: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

…or by emitting new events back to the Event Store.

Client

Event Store

Command Handler

Query Handler

Reactor

Event

Page 177: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

…or both!Client

Event Store

Command Handler

Query Handler

Reactor

Event

External Behaviour Trigger

Page 178: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Reactors have single responsibilities and encourage a business-oriented

decomposition of the system.

Page 179: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Recap

Page 180: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing with CQRS

Page 181: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing with CQRS

‣ Event Sourcing makes you store business facts as the source of truth. These facts are immutable.

Page 182: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing with CQRS

‣ Event Sourcing makes you store business facts as the source of truth. These facts are immutable.

‣ Event Sourcing makes the system deterministic.

Page 183: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing with CQRS

‣ Event Sourcing makes you store business facts as the source of truth. These facts are immutable.

‣ Event Sourcing makes the system deterministic.

‣ CQRS and the Circular Architecture works well with Event Sourcing.

Page 184: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing with CQRS

‣ Event Sourcing makes you store business facts as the source of truth. These facts are immutable.

‣ Event Sourcing makes the system deterministic.

‣ CQRS and the Circular Architecture works well with Event Sourcing.

‣ Clients express intent via commands, which if accepted become events and are appended to an event stream.

Page 185: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing with CQRS

‣ Event Sourcing makes you store business facts as the source of truth. These facts are immutable.

‣ Event Sourcing makes the system deterministic.

‣ CQRS and the Circular Architecture works well with Event Sourcing.

‣ Clients express intent via commands, which if accepted become events and are appended to an event stream.

‣ Asynchronous projectors process the event stream and build up denormalised projections.

Page 186: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing with CQRS

‣ Event Sourcing makes you store business facts as the source of truth. These facts are immutable.

‣ Event Sourcing makes the system deterministic.

‣ CQRS and the Circular Architecture works well with Event Sourcing.

‣ Clients express intent via commands, which if accepted become events and are appended to an event stream.

‣ Asynchronous projectors process the event stream and build up denormalised projections.

‣ Clients query the projections when they want know something.

Page 187: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing with CQRS

‣ Event Sourcing makes you store business facts as the source of truth. These facts are immutable.

‣ Event Sourcing makes the system deterministic.

‣ CQRS and the Circular Architecture works well with Event Sourcing.

‣ Clients express intent via commands, which if accepted become events and are appended to an event stream.

‣ Asynchronous projectors process the event stream and build up denormalised projections.

‣ Clients query the projections when they want know something.

‣ Asynchronous reactors process the event stream and reacts to events according to business logic, outputting more events.

Page 188: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Upside

Page 189: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Upside ‣ Treats the core of your business with respect.

‣ Compatibility with reality.

‣ Rapid iteration and reinterpretation.

‣ Encourages Good Practices™.

‣ Avoids common pain points.

‣ Lets you use the best tool for the job.

‣ You get a free time machine.

Page 190: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Treats the core of your business with respect.

Page 191: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

You can place no more respect to your data than placing it at the core of your

system and then never change it.

Page 192: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We’re not losing data we can’t value.

Page 193: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We can’t predict the future.

Page 194: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Compatibility with reality.

Page 195: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing seeks to emulate the real world.

Page 196: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Just like in the real world, time is a first class citizen.

Page 197: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

In the real world, the past is done and if you don’t like it, all you can

do is compensate for it.

Page 198: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The real world is eventually consistent because there is no central control.

Page 199: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The real world is made up of independent actors that do the best

with the information they have.

Page 200: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Rapid iteration and reinterpretation.

Page 201: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We can separate recording of what happened from interpreting what it means.

Page 202: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Interpretations and experiments are easy to get rid of when no

longer relevant.

Page 203: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Encourages Good Practices™.

Page 204: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Separation of concerns through isolated, asynchronous components.

Page 205: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Encourages code that is expressed in business and user terms.

Page 206: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Removes abstractions. Forms ubiquitous language.

Page 207: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Business owners love this.

Page 208: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Avoids common pain points.

Page 209: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Lets you use the best tool for the job.

Page 210: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

You get a free time machine.

Page 211: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Downside

Page 212: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Downside ‣ Mistakes are forever.

‣ Opacity.

‣ Complexity.

‣ It’s not mainstream.

‣ The J curve of productivity.

‣ Eventual consistency.

Page 213: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Mistakes are forever.

Page 214: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Natural evolution of event schemas do happen.

Page 215: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

You can’t change events after-the-fact.

Page 216: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

You have to create compensating events rather than quick “fixups.”

This is both an advantage and a disadvantage.

Page 217: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

There are strategies but they can be painful.

Some are not for the faint of heart.

Page 218: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Opacity.

Page 219: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Traditional relational models means any developer can poke around in the data to

answer questions.

Page 220: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

An Event Store renders all of those tools and ubiquitous access irrelevant.

This is a big deal.

Page 221: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Your data is more opaque in an Event Store.

Page 222: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

You have to create projections to make sense of the data.

Page 223: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Complexity.

Page 224: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Don’t conflate inherent necessary domain complexity with accidental complexity.

Page 225: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

We can’t avoid complexity, but we can choose how to implement it.

Page 226: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Monoliths are (meant to be):

a simple arrangement of complex things.

Page 227: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing with CQRS is (meant to be):

a complex arrangement of simple things.

Page 228: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Event Sourcing with CQRS has lots of moving parts.

Page 229: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Beware devolving into a complex arrangement

of complex things.

Page 230: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

This is the number one issue I’ve encountered.

(YMMV.)

Page 231: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

It’s easy to get here with the cognitive load introduced by a fundamental paradigm shift.

Page 232: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Many developers will struggle.

Page 233: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

It’s not mainstream.

(But gaining traction.)

Page 234: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The J curve of productivity.

Page 235: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The J curve of productivity.

Time

-ive

+ive

Page 236: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Things will get worse before they get better.

Page 237: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Don’t impose Event Sourcing on a team that:

‣Lacks buy-in to try it.

‣Lacks stakeholder support.

‣Lacks intestinal fortitude.

Page 238: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Eventual consistency.

Page 239: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Commands are synchronous.

Client

Event Store

Command Handler

Query Handler

Page 240: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Clients issue a command and receives an ACK/NACK.

Page 241: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Queries are synchronous.

Client

Event Store

Command Handler

Query Handler

Page 242: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Clients make a query and receive data.

Page 243: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Projectors are asynchronous.

Client

Event Store

Command Handler

Query Handler

Page 244: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

It can take some time for changes to show up in projections.

Page 245: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

💩

Page 246: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Reactors are asynchronous too.

Client

Event Store

Command Handler

Query Handler

Reactor

Page 247: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

💩💩

Page 248: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

And because they are autonomous, changes can show up at different times.

Page 249: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

💩💩💩

Page 250: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

When not all parts of a system has the same idea of current state, this is called

eventual consistency.

Page 251: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

The real world is eventually consistent.

Page 252: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Independent actors can act without consulting each other.

Page 253: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Example: your car getting stolen

Page 254: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Example: your car getting stolen

‣ Your car may get stolen right now while you’re listening to me.

Page 255: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Example: your car getting stolen

‣ Your car may get stolen right now while you’re listening to me.

‣ Your “copy” of the world is not in lockstep with the real world.

Page 256: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Example: your car getting stolen

‣ Your car may get stolen right now while you’re listening to me.

‣ Your “copy” of the world is not in lockstep with the real world.

‣ In your "copy" of the world, your car will be exactly where you left it.

Page 257: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Example: your car getting stolen

‣ Your car may get stolen right now while you’re listening to me.

‣ Your “copy” of the world is not in lockstep with the real world.

‣ In your "copy" of the world, your car will be exactly where you left it.

‣ Hours from now, you find out that it’s gone.

Page 258: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Example: your car getting stolen

‣ Your car may get stolen right now while you’re listening to me.

‣ Your “copy” of the world is not in lockstep with the real world.

‣ In your "copy" of the world, your car will be exactly where you left it.

‣ Hours from now, you find out that it’s gone.

‣ That’s when your “copy” of the world is made consistent.

Page 259: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Example: your car getting stolen

‣ Your car may get stolen right now while you’re listening to me.

‣ Your “copy” of the world is not in lockstep with the real world.

‣ In your "copy" of the world, your car will be exactly where you left it.

‣ Hours from now, you find out that it’s gone.

‣ That’s when your “copy” of the world is made consistent.

‣ This is eventual consistency.

Page 260: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Your system is already eventually consistent.

Page 261: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

You can narrow the time window, but never eliminate it.

Page 262: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

If we can’t eliminate eventual consistency, we have to manage it.

Page 263: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Modelling systems based on lessons from the real world is very helpful when dealing

with eventual consistency.

Page 264: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Risk is a function of time.

Page 265: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Is 1 nanosecond okay?

What about 1 month?

Page 266: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Your threshold is contextual.

Page 267: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Showing order status to a user? Threshold: seconds.

Page 268: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Sales report? Threshold: hours.

Page 269: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

It’s a business risk, not a technical one.

Page 270: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Ask: what’s the risk of making decisions with out-of-date information?

Page 271: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

There are ways to manage eventual consistency.

Page 272: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Managing eventual consistency

Page 273: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Managing eventual consistency

‣ Educate your users.

Page 274: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Managing eventual consistency

‣ Educate your users.

‣ Lie to your users.

Page 275: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Managing eventual consistency

‣ Educate your users.

‣ Lie to your users.

‣ Read your own writes.

Page 276: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Managing eventual consistency

‣ Educate your users.

‣ Lie to your users.

‣ Read your own writes.

‣ Build new user interfaces.

Page 277: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Managing eventual consistency

‣ Educate your users.

‣ Lie to your users.

‣ Read your own writes.

‣ Build new user interfaces.

‣ Slow down the user.

Page 278: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Managing eventual consistency

‣ Educate your users.

‣ Lie to your users.

‣ Read your own writes.

‣ Build new user interfaces.

‣ Slow down the user.

‣ Speed up the backend.

Page 279: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Though it is manageable, it can be an absolute blocker.

Page 280: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

At the end of the day, this is a pattern.

Page 281: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

It’s not appropriate for every use case.

Page 282: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

It’s not a silver bullet.

Page 283: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

It has lots of awesome benefits, but also significant disadvantages.

Page 284: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

If you can overcome the challenges, it can be incredibly powerful.

Page 285: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

“Event Sourcing is a multiplier.

Done well, it gives you magical powers in terms of flexibility and scalability.

Done poorly, it becomes worse than the crappiest balls of mud.

Page 286: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Page 287: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

If you look for eventual consistency, you will find it everywhere.

Page 288: An In-Depth Look at Event Sourcing with CQRS › yow2017 › VonConrad-EventSourc… · An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad Director of Engineering,

Thank you!Sebastian von Conrad @vonconrad


Recommended