a year with event sourcing and cqrs

149
Hello.

Upload: steve-pember

Post on 07-Jan-2017

913 views

Category:

Software


2 download

TRANSCRIPT

Page 1: A year with event sourcing and CQRS

Hello.

Page 2: A year with event sourcing and CQRS

An example scenario

Page 3: A year with event sourcing and CQRS

Imagine you’re a happy developer

Page 4: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 5: A year with event sourcing and CQRS

Need those Reports…

Page 6: A year with event sourcing and CQRS

You got it.

Page 7: A year with event sourcing and CQRS

You work furiously…

Page 8: A year with event sourcing and CQRS

Simple Cart Schema

Page 9: A year with event sourcing and CQRS

A Quick, Simple, Task

Page 10: A year with event sourcing and CQRS

Except, There’s No Data

Page 11: A year with event sourcing and CQRS
Page 12: A year with event sourcing and CQRS

A Year With Event Sourcing and CQRS

Steve Pember CTO, ThirdChannel

[email protected]

Software Architecture Conf, 2016

THIRDCHANNEL @svpember

Page 13: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Agenda• CQRS

• Event Sourcing

• This Tech @ ThirdChannel

• Demo

Page 14: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

CQRS… ?

Page 15: A year with event sourcing and CQRS

Command - Query Responsibility Segregation

Page 16: A year with event sourcing and CQRS

CRUD vs CQRS

Page 17: A year with event sourcing and CQRS

CRUD / MVC

Page 18: A year with event sourcing and CQRS

Command Query Responsibility Segregation

Page 19: A year with event sourcing and CQRS

It all started with…

Page 20: A year with event sourcing and CQRS

–Eric Evans

“Some objects are not defined primarily by their attributes. They represent a thread of identity that runs through time and often

across distinct representations. Sometimes such an object must be matched with another object even though attributes differ.

An object must be distinguished from other objects even though they might have the same attributes.”

Page 21: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Domain Driven Design• Ubiquitous Language

Page 22: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Domain Driven Design• Ubiquitous Language

• Entities / Value Objects

Page 23: A year with event sourcing and CQRS

Entity: when you care about identity

Page 24: A year with event sourcing and CQRS

Value Objects: don’t care about individual

Page 25: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Domain Driven Design• Ubiquitous Language

• Entities / Value Objects

• Aggregates

Page 26: A year with event sourcing and CQRS

Group of Entities with one Root

Page 27: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 28: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Domain Driven Design• Ubiquitous Language

• Entities / Value Objects

• Aggregates

• Bounded Context

Page 29: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 30: A year with event sourcing and CQRS

Objects inside an Aggregate may hold refs to other Roots

Page 31: A year with event sourcing and CQRS

Deleting an Aggregate also deletes dependents

Page 32: A year with event sourcing and CQRS

Isolate related objects into Modules

Page 33: A year with event sourcing and CQRS

Furthermore, No direct comms across boundaries

Page 34: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Domain Driven Design• Ubiquitous Language

• Entities / Value Objects

• Aggregates

• Bounded Context

• Domain Events

Page 35: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 36: A year with event sourcing and CQRS

Events are Transactionally Safe

Page 37: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Back to CQRS…

Page 38: A year with event sourcing and CQRS

CQRS is an Evolution on DDD

Page 39: A year with event sourcing and CQRS

Basis: Two Objects where there was One

Page 40: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 41: A year with event sourcing and CQRS

But allows for Interesting and efficient Architectures

Page 42: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 43: A year with event sourcing and CQRS
Page 44: A year with event sourcing and CQRS

Multiple Query Caches derived from events

Page 45: A year with event sourcing and CQRS

–Johnny Appleseed

“Type a quote here.”

Page 46: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

When should you use CQRS?

Page 47: A year with event sourcing and CQRS

With Microservices

Page 48: A year with event sourcing and CQRS

Complex Domains

Page 49: A year with event sourcing and CQRS

High Performant Systems

Page 50: A year with event sourcing and CQRS

Task-Based UIs

Page 51: A year with event sourcing and CQRS
Page 52: A year with event sourcing and CQRS

–Johnny Appleseed

“Type a quote here.”

Page 53: A year with event sourcing and CQRS

However…

Page 54: A year with event sourcing and CQRS

The creators caution against using CQRS

Page 55: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Ok, so how about Event Sourcing?

Page 56: A year with event sourcing and CQRS

What do we do with our Domain Events?

Page 57: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Event Sourcing• Alternative Storage Pattern - Save Deltas

Page 58: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 59: A year with event sourcing and CQRS

Store Deltas instead of Current State

Page 60: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 61: A year with event sourcing and CQRS

Objects are backed - ‘sourced’ - by events

Page 62: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Event Sourcing• Alternative Storage Pattern - Save Deltas

• Event vs Aggregate

Page 63: A year with event sourcing and CQRS

Event: Something which has occurred in the system

Page 64: A year with event sourcing and CQRS

Aggregate: from DDD!

Page 65: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Event Sourcing• Alternative Storage Pattern - Save Deltas

• Event vs Aggregate

• Purely Additive

Page 66: A year with event sourcing and CQRS

There is no Delete

Page 67: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Event Sourcing• Alternative Storage Pattern - Save Deltas

• Event vs Aggregate

• Purely Additive

• It’s Old

Page 68: A year with event sourcing and CQRS
Page 69: A year with event sourcing and CQRS
Page 70: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Event Sourcing• Alternative Storage Pattern - Save Deltas

• Event vs Aggregate

• Purely Additive

• It’s Old

• Event Stream -> Transient Models

Page 71: A year with event sourcing and CQRS
Page 72: A year with event sourcing and CQRS

All Objects are Transient Derivatives of the Event Stream

Page 73: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Some Caveats

Page 74: A year with event sourcing and CQRS

I’m sure you have some doubts.

Page 75: A year with event sourcing and CQRS

Wouldn’t it take up a large amount of storage space?

Page 76: A year with event sourcing and CQRS
Page 77: A year with event sourcing and CQRS

Wouldn’t ES add overhead?

Page 78: A year with event sourcing and CQRS
Page 79: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 80: A year with event sourcing and CQRS

Try to avoid ‘heavy’ persistence mechanisms

Page 81: A year with event sourcing and CQRS

The toughest thing, though:

Page 82: A year with event sourcing and CQRS

ES Can be difficult for junior developers

Page 83: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

So Why Would I Want Event Sourcing?

Page 84: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

So Why ES?• More than the Perfect Audit Log

Page 85: A year with event sourcing and CQRS

Audit logs tell the History

Events tell the intent of History

Page 86: A year with event sourcing and CQRS
Page 87: A year with event sourcing and CQRS

Also, You’re now a Time Traveller

Page 88: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

So Why ES?• More than the Perfect Audit Log

• Only Structural Model That Does Not Lose Information

Page 89: A year with event sourcing and CQRS

NEVER DELETE!!!

Page 90: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

So Why ES?• More than the Perfect Audit Log

• Only Structural Model That Does Not Lose Information

• Ideal for Business Analysis

Page 91: A year with event sourcing and CQRS

Analysis of the Event Stream is a Projection

Page 92: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 93: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Current State is Boring• Mine ALL ProductRemoved events

• Find ALL ProductAdded events followed by RemovedEvents for same product within 5 minutes

• Find Average Duration between ProductAdd and OrderPlaced

Page 94: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 95: A year with event sourcing and CQRS

Future Proof your Data!

Page 96: A year with event sourcing and CQRS

Remember the Sad Developer?

Page 97: A year with event sourcing and CQRS

With Event Sourcing…

Page 98: A year with event sourcing and CQRS

You can see everything

Page 99: A year with event sourcing and CQRS

Business Folks Love A Good Report

Page 100: A year with event sourcing and CQRS

Still…

Page 101: A year with event sourcing and CQRS

Businesses depend on Time Series Data

Page 102: A year with event sourcing and CQRS
Page 103: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Mechanics and Demo

Page 104: A year with event sourcing and CQRS

Pure Event Sourcing

Page 105: A year with event sourcing and CQRS

3 Base Objects

Page 106: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 107: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 108: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 109: A year with event sourcing and CQRS

Successful Commands Result in new Events on the Aggregate

Page 110: A year with event sourcing and CQRS

–Johnny Appleseed

“Type a quote here.”

Page 111: A year with event sourcing and CQRS

Loading a Query builds up Aggregates from the Events

Page 112: A year with event sourcing and CQRS
Page 113: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 114: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Tying It All Together

Page 115: A year with event sourcing and CQRS

CQRS & ES

Page 116: A year with event sourcing and CQRS

–Johnny Appleseed

“Type a quote here.”

Page 117: A year with event sourcing and CQRS

The Event Stream is the Source of Truth.

Page 118: A year with event sourcing and CQRS

Pre-Build Query Objects from the Event Stream

Page 119: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Questions So Far?

Page 120: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

What have we learned @ThirdChannel?

Page 121: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 122: A year with event sourcing and CQRS

There are few available tools …

Page 123: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 124: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Akka & Akka Persistance

Page 125: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 126: A year with event sourcing and CQRS

…and we used none of them

Page 127: A year with event sourcing and CQRS

–Johnny Appleseed

“Type a quote here.”

Page 128: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 129: A year with event sourcing and CQRS

Commands & Queries immediately felt like a perfect fit

Page 130: A year with event sourcing and CQRS

…and make sure you append your class names with ‘Command’ and ‘Query’

Page 131: A year with event sourcing and CQRS

Query - based Views are an efficiency gain

Page 132: A year with event sourcing and CQRS

Query Read Models should be able to be freely trashed and rebuilt

Page 133: A year with event sourcing and CQRS

We’ve still not run into Aggregate write contention

Page 134: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 135: A year with event sourcing and CQRS

Save Every Event

Page 136: A year with event sourcing and CQRS

You really don’t need snapshots (at first)

Page 137: A year with event sourcing and CQRS

Be Wary of Editing an Event

Page 138: A year with event sourcing and CQRS

Small Events > Large Events

Page 139: A year with event sourcing and CQRS

This is Not… Great.

Page 140: A year with event sourcing and CQRS

A Bit Better

Page 141: A year with event sourcing and CQRS

This next bit is important

Page 142: A year with event sourcing and CQRS

Pure Event Sourcing Is Not Always The Best Choice

Page 143: A year with event sourcing and CQRS

Include a ‘dateEffective’ field on the Event table

Page 144: A year with event sourcing and CQRS

Add Current-State, Indexed, Columns to your Aggregates

Page 145: A year with event sourcing and CQRS

And Make Many of Them

Page 146: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Demo Time

Page 147: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Page 148: A year with event sourcing and CQRS

Thank You!

THIRDCHANNEL @svpember

Page 149: A year with event sourcing and CQRS

THIRDCHANNEL @svpember

Image Credits• Mad Developers: Last Week Tonight • CQRS Diagram: http://udidahan.com/2009/12/09/clarified-cqrs/ • Happy Developers: http://www.businessinsider.com/programming-languages-in-highest-demand-2015-6 • Obama Thumbs up: http://amiloszportraits.com/barack-obama-thumbs-up-meme • Who am i?: http://www.catholicismusa.com/want-joy-seek-god-find-your-true-identity/ • Star Trek face palm: http://tvtropes.org/pmwiki/pmwiki.php/FacePalm/LiveActionTV • Sauron’s Eye: http://www.independent.co.uk/arts-entertainment/architecture/russian-orthodox-church-condemns-

moscows-eye-of-sauron-art-installation-celebrating-the-hobbit-the-9915050.html • Grain of Salt: http://www.pxleyes.com/photography-picture/4c3004291f903/Grain-of-Salt.html • Obama, Nye, and Tyson: en.wikipedia.org/wiki/Selfie