UPDATE your VIEW on DELETE The benefits of Event Sourcing - - PowerPoint PPT Presentation

update your view on delete
SMART_READER_LITE
LIVE PREVIEW

UPDATE your VIEW on DELETE The benefits of Event Sourcing - - PowerPoint PPT Presentation

UPDATE your VIEW on DELETE The benefits of Event Sourcing Sebastian von Conrad - @envato - @vonconrad Sebastian von Conrad - @envato - @vonconrad Sebastian von Conrad - @envato - @vonconrad Sebastian von Conrad - @envato -


slide-1
SLIDE 1

Sebastian von Conrad - @envato - @vonconrad

UPDATE your VIEW on DELETE

The benefits of Event Sourcing

slide-2
SLIDE 2

Sebastian von Conrad - @envato - @vonconrad

  

slide-3
SLIDE 3

Sebastian von Conrad - @envato - @vonconrad

slide-4
SLIDE 4

Sebastian von Conrad - @envato - @vonconrad

slide-5
SLIDE 5

Sebastian von Conrad - @envato - @vonconrad

In the top 300 of the world’s largest websites.

slide-6
SLIDE 6

Sebastian von Conrad - @envato - @vonconrad

Event Sourcing.

slide-7
SLIDE 7

Sebastian von Conrad - @envato - @vonconrad

(With an appearance

  • f CQRS architecture.)
slide-8
SLIDE 8

Sebastian von Conrad - @envato - @vonconrad

What is Event Sourcing?

slide-9
SLIDE 9

Sebastian von Conrad - @envato - @vonconrad

What is an Event?

slide-10
SLIDE 10

Sebastian von Conrad - @envato - @vonconrad

This poor bugger is horrendously

  • verloaded.
slide-11
SLIDE 11

Sebastian von Conrad - @envato - @vonconrad

An event is a business fact…

slide-12
SLIDE 12

Sebastian von Conrad - @envato - @vonconrad

…that happened at a particular time.

slide-13
SLIDE 13

Sebastian von Conrad - @envato - @vonconrad

  • Appointment Scheduled
  • Appointment Rescheduled
  • Appointment Location Moved
  • Appointment Cancelled
  • Invitation Extended
  • Invitation Notification Sent
  • Invitation Accepted
  • Invitation Declined
slide-14
SLIDE 14

Sebastian von Conrad - @envato - @vonconrad

  • Item Version Submitted
  • Item Version Approved
  • Item Added To Cart
  • Item Removed From Cart
  • Item Licence Purchased
  • Item Support Purchased
  • Withdrawal Request Submitted
  • Withdrawal Completed
slide-15
SLIDE 15

Sebastian von Conrad - @envato - @vonconrad

Event Sourcing, in this context.

slide-16
SLIDE 16

Sebastian von Conrad - @envato - @vonconrad

Standard practice is store the current state

  • f an object in a

database using an ORM.

slide-17
SLIDE 17

Sebastian von Conrad - @envato - @vonconrad

When state changes, the DB representation changes.

slide-18
SLIDE 18

Sebastian von Conrad - @envato - @vonconrad

John wants to take his partner’s last name.

slide-19
SLIDE 19

Sebastian von Conrad - @envato - @vonconrad

ORM

#<User id: 216, first_name: “John”, last_name: “Reed” ...> + --- + ---------- + --------- + --- + | id | first_name | last_name | ... | | --- | ---------- | --------- | --- | | 216 | John | Reed | ... | | ... | ... | ... | ... | + --- + ---------- + --------- + --- +

slide-20
SLIDE 20

Sebastian von Conrad - @envato - @vonconrad

ORM

@user.update(last_name: “Hill”) + --- + ---------- + --------- + --- + | id | first_name | last_name | ... | | --- | ---------- | --------- | --- | | 216 | John | Hill | ... | | ... | ... | ... | ... | + --- + ---------- + --------- + --- +

slide-21
SLIDE 21

Sebastian von Conrad - @envato - @vonconrad

Event Sourcing doesn’t do that.

slide-22
SLIDE 22

Sebastian von Conrad - @envato - @vonconrad

An append-only set

  • f immutable events

as source of truth.

slide-23
SLIDE 23

Sebastian von Conrad - @envato - @vonconrad

Derive everything else from the events.

slide-24
SLIDE 24

Sebastian von Conrad - @envato - @vonconrad

Source the current state by replaying events.

slide-25
SLIDE 25

Sebastian von Conrad - @envato - @vonconrad

{ user_id: 216, event_type: “signed_up”, body: { first_name: “John”, last_name: “Reed” } } #<User id: 216, first_name: “John”, last_name: “Reed”, ...>

Event Sourcing

slide-26
SLIDE 26

Sebastian von Conrad - @envato - @vonconrad

@user.change_name(last_name: “Hill”) { user_id: 216, event_type: “name_changed”, body: { last_name: “Hill” } }

Event Sourcing

slide-27
SLIDE 27

Sebastian von Conrad - @envato - @vonconrad

{ event_type: “sign_up”, body: { first_name: “John”, last_name: “Reed” } }, { event_type: “name_changed”, body: { last_name: “Hill” } } #<User id: 216, first_name: “John”, last_name: “Hill”, ...>

Event Sourcing

slide-28
SLIDE 28

Sebastian von Conrad - @envato - @vonconrad

Make everything else completely disposable.

slide-29
SLIDE 29

Sebastian von Conrad - @envato - @vonconrad

Including current state.

slide-30
SLIDE 30

Sebastian von Conrad - @envato - @vonconrad

Language agnostic.

slide-31
SLIDE 31

Sebastian von Conrad - @envato - @vonconrad

Why Event Sourcing?

slide-32
SLIDE 32

Sebastian von Conrad - @envato - @vonconrad

DELETE is evil.

slide-33
SLIDE 33

Sebastian von Conrad - @envato - @vonconrad

Every UPDATE is a DELETE.

slide-34
SLIDE 34

Sebastian von Conrad - @envato - @vonconrad

…so UPDATE is evil too.

slide-35
SLIDE 35

Sebastian von Conrad - @envato - @vonconrad

Business concepts at the heart of the system.

slide-36
SLIDE 36

Sebastian von Conrad - @envato - @vonconrad

It’s tried and tested. (For centuries.)

slide-37
SLIDE 37

Sebastian von Conrad - @envato - @vonconrad

You’re using it already and would refuse to do it any

  • ther way.
slide-38
SLIDE 38

Sebastian von Conrad - @envato - @vonconrad

Current state is hard to distribute.

slide-39
SLIDE 39

Sebastian von Conrad - @envato - @vonconrad

Append-only streams

  • f events are easy

to distribute.

slide-40
SLIDE 40

Sebastian von Conrad - @envato - @vonconrad

Last but not least: free time machine!

slide-41
SLIDE 41

Sebastian von Conrad - @envato - @vonconrad

CQRS

slide-42
SLIDE 42

Sebastian von Conrad - @envato - @vonconrad

CQS: methods can read (queries) or write (commands) but not both.

slide-43
SLIDE 43

Sebastian von Conrad - @envato - @vonconrad

CQRS: objects have queries or commands but not both.

slide-44
SLIDE 44

Sebastian von Conrad - @envato - @vonconrad

In our world we take it one step further.

slide-45
SLIDE 45

Sebastian von Conrad - @envato - @vonconrad

Client Command Handler Query Handler Event Store

slide-46
SLIDE 46

Sebastian von Conrad - @envato - @vonconrad

Client Command Handler Query Handler Event Store

Command Event

slide-47
SLIDE 47

Sebastian von Conrad - @envato - @vonconrad

Commands represent user intent.

slide-48
SLIDE 48

Sebastian von Conrad - @envato - @vonconrad

Commands can be rejected.

slide-49
SLIDE 49

Sebastian von Conrad - @envato - @vonconrad

Events are created when commands are accepted.

slide-50
SLIDE 50

Sebastian von Conrad - @envato - @vonconrad

Client Command Handler Query Handler Event Store

<= Queries => DTOs Projections

slide-51
SLIDE 51

Sebastian von Conrad - @envato - @vonconrad

Projectors process Events in order.

slide-52
SLIDE 52

Sebastian von Conrad - @envato - @vonconrad

Projectors maintain denormalised Projections.

slide-53
SLIDE 53

Sebastian von Conrad - @envato - @vonconrad

Projectors and Projections are 1:1.

slide-54
SLIDE 54

Sebastian von Conrad - @envato - @vonconrad

One Projection per screen/endpoint.

slide-55
SLIDE 55

Sebastian von Conrad - @envato - @vonconrad

Query Handlers query projections.

slide-56
SLIDE 56

Sebastian von Conrad - @envato - @vonconrad

Query Handlers return DTOs to clients.

slide-57
SLIDE 57

Sebastian von Conrad - @envato - @vonconrad

Client Command Handler Query Handler Event Store

<= Queries => DTOs (Sync) Projections (Async) Command (Sync) Event (Sync)

slide-58
SLIDE 58

Sebastian von Conrad - @envato - @vonconrad

Eventual Consistency.

slide-59
SLIDE 59

Sebastian von Conrad - @envato - @vonconrad

(That thing we’re not supposed to talk about.)

slide-60
SLIDE 60

Sebastian von Conrad - @envato - @vonconrad

The world is eventually consistent.

slide-61
SLIDE 61

Sebastian von Conrad - @envato - @vonconrad

Is a nanosecond okay? What about a month?

slide-62
SLIDE 62

Sebastian von Conrad - @envato - @vonconrad

Risk is always a function of time.

slide-63
SLIDE 63

Sebastian von Conrad - @envato - @vonconrad

Downstream Event Processors (aka DEPs).

slide-64
SLIDE 64

Sebastian von Conrad - @envato - @vonconrad Client Command Handler Query Handler Event Store

DEPs

slide-65
SLIDE 65

Sebastian von Conrad - @envato - @vonconrad Client Command Handler Query Handler Event Store

DEPs

Processes Events Emits Events External Service Triggers Behaviour

slide-66
SLIDE 66

Sebastian von Conrad - @envato - @vonconrad

DEPs process events like projectors.

slide-67
SLIDE 67

Sebastian von Conrad - @envato - @vonconrad

Can react by emitting events back to the Event stream.

slide-68
SLIDE 68

Sebastian von Conrad - @envato - @vonconrad

Can react by triggering external behaviour.

slide-69
SLIDE 69

Sebastian von Conrad - @envato - @vonconrad

Encourages clean separation of concerns.

slide-70
SLIDE 70

Sebastian von Conrad - @envato - @vonconrad

Case study.

slide-71
SLIDE 71

Sebastian von Conrad - @envato - @vonconrad

Country Conflicts.

slide-72
SLIDE 72

Sebastian von Conrad - @envato - @vonconrad

Business problem?

slide-73
SLIDE 73

Sebastian von Conrad - @envato - @vonconrad

Detect and investigate conflicting information regarding a user’s physical location.

slide-74
SLIDE 74

Sebastian von Conrad - @envato - @vonconrad Product A Product B Single Sign-On

Country Conflicts

Evidence Evidence Evidence Product A Product C Conflicts Conflicts

slide-75
SLIDE 75

Sebastian von Conrad - @envato - @vonconrad

Gather evidence and store as events.

slide-76
SLIDE 76

Sebastian von Conrad - @envato - @vonconrad

DEP looks at evidence and raises ConflictDetected.

slide-77
SLIDE 77

Sebastian von Conrad - @envato - @vonconrad

(Another) DEP sees conflict event and emails a notification.

slide-78
SLIDE 78

Sebastian von Conrad - @envato - @vonconrad

(Another) DEP might auto-resolve the conflict based on more evidence.

slide-79
SLIDE 79

Sebastian von Conrad - @envato - @vonconrad Product A Product B Single Sign-On

Country Conflicts

Evidence Evidence Evidence Product A Product C Conflicts Conflicts Admin UI

slide-80
SLIDE 80

Sebastian von Conrad - @envato - @vonconrad

Admins issue Commands to manually resolve conflicts.

slide-81
SLIDE 81

Sebastian von Conrad - @envato - @vonconrad

After 30 days, GracePeriodExpired is raised.

slide-82
SLIDE 82

Sebastian von Conrad - @envato - @vonconrad

DEP checks whether it is. If not, raises ConflictActivated.

slide-83
SLIDE 83

Sebastian von Conrad - @envato - @vonconrad

  • Evidence Provided
  • Conflict Detected
  • Conflict Notification Email Sent
  • Conflict Automatically Resolved
  • Conflict Manually Resolved
  • Conflict Grace Period Expired
  • Conflict Activated
slide-84
SLIDE 84

Sebastian von Conrad - @envato - @vonconrad

Other systems are querying conflicts through projections.

slide-85
SLIDE 85

Sebastian von Conrad - @envato - @vonconrad

Small, well-defined, and simple.

slide-86
SLIDE 86

Sebastian von Conrad - @envato - @vonconrad

Other systems are substantially larger.

slide-87
SLIDE 87

Sebastian von Conrad - @envato - @vonconrad

So why this CQRS architecture?

slide-88
SLIDE 88

Sebastian von Conrad - @envato - @vonconrad

Encourages Single Responsibilities.

slide-89
SLIDE 89

Sebastian von Conrad - @envato - @vonconrad

Command and Query Handlers can scale independently.

slide-90
SLIDE 90

Sebastian von Conrad - @envato - @vonconrad

Writes are fast.

slide-91
SLIDE 91

Sebastian von Conrad - @envato - @vonconrad

Reads are faster.

slide-92
SLIDE 92

Sebastian von Conrad - @envato - @vonconrad

Projections can be thrown away when no longer needed.

slide-93
SLIDE 93

Sebastian von Conrad - @envato - @vonconrad

Separating recording from interpreting what happened.

slide-94
SLIDE 94

Sebastian von Conrad - @envato - @vonconrad

Limit blast radius

  • f changes.
slide-95
SLIDE 95

Sebastian von Conrad - @envato - @vonconrad

Reduces fear and enables rapid change.

slide-96
SLIDE 96

Sebastian von Conrad - @envato - @vonconrad

Keep the cost of change lower for longer.

slide-97
SLIDE 97

Sebastian von Conrad - @envato - @vonconrad

The heart of a system is far more stable than the edges.

slide-98
SLIDE 98

Sebastian von Conrad - @envato - @vonconrad

Should you use it?

slide-99
SLIDE 99

Sebastian von Conrad - @envato - @vonconrad

Well, maybe.

slide-100
SLIDE 100

Sebastian von Conrad - @envato - @vonconrad

Not appropriate for every problem.

slide-101
SLIDE 101

Sebastian von Conrad - @envato - @vonconrad

Works better for read-heavy systems.

slide-102
SLIDE 102

Sebastian von Conrad - @envato - @vonconrad

Well suited for commerce-oriented domains, for example.

slide-103
SLIDE 103

Sebastian von Conrad - @envato - @vonconrad

Or anywhere where history is important.

slide-104
SLIDE 104

Sebastian von Conrad - @envato - @vonconrad

Competitive advantage Complexity

ES? Buy WTF KISS Strategic Design*

slide-105
SLIDE 105

Sebastian von Conrad - @envato - @vonconrad

It’s a great tool to have in your toolbox.

slide-106
SLIDE 106

Sebastian von Conrad - @envato - @vonconrad

Thank you. Questions?