Top Banner
THIRDCHANNEL @svpember Hi.
146

Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Aug 17, 2015

Download

Software

Steve Pember
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Hi.

Page 2: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

First, A Quick Scenario

Page 3: Gr8conf US 2015 - Intro to Event Sourcing with Groovy
Page 4: Gr8conf US 2015 - Intro to Event Sourcing with Groovy
Page 5: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Balance: $100

Page 6: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Balance: -$100

Page 7: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 8: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 9: Gr8conf US 2015 - Intro to Event Sourcing with Groovy
Page 10: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 11: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 12: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Richer Data History With Event Sourcing

Steve PemberCTO, ThirdChannelgr8Conf US 2015

Page 13: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Agenda• Event Sourcing

• Event Sourcing Challenges

• Don’t Worry, ES Is Worth It

• Querying

• Implementation (Theory)

• Implementation (For Real)

• Any Tools Available?

• Demo (Hopefully)

Page 14: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

What Is Event Sourcing?

Page 15: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Event Sourcing• Alternative Storage Pattern

Page 16: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 17: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Instead, Store Facts

Page 18: Gr8conf US 2015 - Intro to Event Sourcing with Groovy
Page 19: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 20: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Event Sourcing• Alternative Storage Pattern

• Series of Facts

• Aggregates << Events

Page 21: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Event: Something Which Has Occurred In The System

Page 22: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Aggregate: The Object of Events’ Affections

Page 23: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Event Sourcing• Alternative Storage Pattern

• Series of Facts

• Aggregates << Events

• Purely Additive

Page 24: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

There is No Delete

Page 25: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 26: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Full History of the Aggregates

Page 27: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 28: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Event Sourcing• Alternative Storage Pattern

• Series of Facts

• Aggregates << Events

• Purely Additive

• Optimization: Snapshots

Page 29: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

• snapshot photo

Page 30: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 31: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Event Sourcing• Alternative Storage Pattern

• Series of Facts

• Aggregates << Events

• Purely Additive

• Optimization: Snapshots

• Example: Shopping Cart

Page 32: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 33: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 34: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 35: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Event Sourcing• Alternative Storage Pattern

• Series of Facts

• Aggregates << Events

• Purely Additive

• Optimization: Snapshots

• Example: Shopping Cart

• Transient Objects

Page 36: Gr8conf US 2015 - Intro to Event Sourcing with Groovy
Page 37: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Objects are Transient Derivatives of Your Events

Page 38: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Event Sourcing• Alternative Storage Pattern

• Series of Facts

• Aggregates << Events

• Purely Additive

• Optimization: Snapshots

• Example: Shopping Cart

• Transient Objects

• Natural

Page 39: Gr8conf US 2015 - Intro to Event Sourcing with Groovy
Page 40: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 41: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 42: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Questions So Far?• Events & Aggregates

• Snapshots

• Additive, loss-less model

• Transient Derivatives

Page 43: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Agenda• Event Sourcing

• Event Sourcing Challenges

Page 44: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

“I Think I Get What You’re Laying Down, But It Still

Seems Bananas”

Page 45: Gr8conf US 2015 - Intro to Event Sourcing with Groovy
Page 46: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

So much work!

Page 47: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

“That’s gotta be a ton of Resource overhead”

Page 48: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

“Where’d my Models go? I can’t live without my MVC!”

Page 49: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Event Sourcing Challenges• Additional Work To Apply

• Additional CPU Processing

• Non-Traditional Concept of Models

Page 50: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Event Sourcing Challenges• Additional Work To Apply

• Additional CPU Processing

• Non-Traditional Concept of Models

• More Storage Required VS non-ES

Page 51: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 52: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Event Sourcing Challenges• Additional Work To Apply

• Additional CPU Processing

• Non-Traditional Concept of Models

• More Storage Required VS non-ES

• Reduced Database Level Constraints

Page 53: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Make Use of @Transactional Code

Page 54: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Event Sourcing Challenges• Additional Work To Apply

• Additional CPU Processing

• Non-Traditional Concept of Models

• More Storage Required VS non-ES

• Reduced Transactions / Database Level Constraints

• May Be Confusing For Junior Engineers

Page 55: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

So Why Use ES?

Page 56: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Agenda• Event Sourcing

• Event Sourcing Challenges

• Don’t Worry, ES Is Worth It

Page 57: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Why Event Sourcing• More Than an Audit Log

Page 58: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Audit Logs Tell the History

Events Tell the Intent of History

Page 59: Gr8conf US 2015 - Intro to Event Sourcing with Groovy
Page 60: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

ES is also a perfect Audit Log

Page 61: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Why Event Sourcing• More Than an Audit Log

• Data Storage is Inexpensive

Page 62: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Why Event Sourcing• More Than an Audit Log

• Data Storage is Inexpensive

• Used By All Long-Running Businesses

Page 63: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 64: Gr8conf US 2015 - Intro to Event Sourcing with Groovy
Page 65: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

All Business Problems Can Be Modeled With Event Sourcing

Page 66: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Why Event Sourcing• More Than an Audit Log

• Data Storage is Inexpensive

• Used By All Long-Running Businesses

• Only Structural Model That Does Not Lose Information

Page 67: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Remember: There Is No Delete

Page 68: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Why Event Sourcing• More Than an Audit Log

• Data Storage is Inexpensive

• Used By All Long-Running Businesses

• Only Structural Model That Does Not Lose Information

• Simplified Testing and Debugging

Page 69: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Unit Test Events, Assert In Integration Tests

Page 70: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Time Travel Eases Debugging

Page 71: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 72: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 73: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 74: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 75: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 76: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 77: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Why Event Sourcing• More Than an Audit Log

• Data Storage is Inexpensive

• Used By All Long-Running Businesses

• Only Structural Model That Does Not Lose Information

• Simplified Testing and Debugging

• Ideal for Business Analysis

Page 78: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Future-Proof your Data

Page 79: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 80: Gr8conf US 2015 - Intro to Event Sourcing with Groovy
Page 81: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

But With Event Sourcing…

Page 82: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

–Johnny Appleseed

“Type a quote here.”

Page 83: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 84: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Current State is a Projection

Page 85: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

A 3C Example

Page 86: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 87: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Still…

Page 88: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 89: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Have you ever built?• Versioning of objects

• An ‘undo’ action

• Audit Log

• Tracking object value changes over time?

• Time series?

Page 90: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Agenda• Event Sourcing

• Event Sourcing Challenges

• Don’t Worry, ES Is Worth It

• Querying

Page 91: Gr8conf US 2015 - Intro to Event Sourcing with Groovy
Page 92: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Querying Events• All Queries are Projections, including Current State

Page 93: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Querying Events• All Queries are Projections, including Current State

• Returning Current State is Easy

Page 94: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 95: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Querying Events• All Queries are Projections, including Current State

• Returning Current State is Easy

• Other Projections Can Be Tough, use a Read Model

Page 96: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 97: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Querying Events• All Queries are Projections, including Current State

• Returning Current State is Easy

• Other Projections Can Be Tough

• Try Reactive Streams

Page 98: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 99: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Use Streams to Filter Events in Code

Page 100: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 101: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Querying Events• All Queries are Projections, including Current State

• Returning Current State is Easy

• Other Projections Can Be Tough

• Try Reactive Streams

• Initial Projection Work May Require Dev Time

Page 102: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 103: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Agenda• Event Sourcing

• Event Sourcing Challenges

• Don’t Worry, ES Is Worth It

• Querying

• Implementation (Theory)

Page 104: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Implementation (Theory)• Simple Base Objects

Page 105: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Three Base Objects

Page 106: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 107: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 108: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Past-Tense Event Names

Page 109: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 110: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Implementation (Theory)• Simple Base Objects

• Aggregate and Event SubClasses Have Transient Properties

Page 111: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 112: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 113: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Implementation (Theory)• Simple Base Objects

• Aggregate and Event SubClasses Have Transient Properties

• Aggregates Receive and Play Events

Page 114: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Events Modify Aggregates

Page 115: Gr8conf US 2015 - Intro to Event Sourcing with Groovy
Page 116: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Implementation (Theory)• Simple Base Objects

• Aggregate and Event SubClasses Have Transient Properties

• Aggregates Receive and Play Events

• Aggregates Require Distinction Between New and Historical Changes

Page 117: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 118: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Implementation (Theory)• Simple Base Objects

• Aggregate and Event SubClasses Have Transient Properties

• Aggregates Receive and Play Events

• Aggregates Require Distinction Between New and Historical Changes

• Event Service Layer Required

Page 119: Gr8conf US 2015 - Intro to Event Sourcing with Groovy
Page 120: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Agenda• Event Sourcing

• Event Sourcing Challenges

• Don’t Worry, ES Is Worth It

• Querying

• Implementation (Theory)

• Implementation (Reality)

Page 121: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Implementation (Reality)• Snapshot Rarely

Page 122: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Implementation (Reality)• Snapshot Rarely

• ‘Pure’ Event Sourcing is tough to work with

Page 123: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 124: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Implementation (Reality)• Snapshot Rarely

• ‘Pure’ Event Sourcing is tough to work with

• 2 Alternatives

Page 125: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Sync

Page 126: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 127: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Plus so much more (I could talk all day about ES)

Page 128: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Agenda• Event Sourcing

• Event Sourcing Challenges

• Don’t Worry, ES Is Worth It

• Querying

• Implementation (Theory)

• Implementation (For Real)

• Any Tools Available?

Page 129: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Event Source Libraries• No Real Need for an ORM

Page 130: Gr8conf US 2015 - Intro to Event Sourcing with Groovy
Page 131: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Event Sourcing Libraries• No Real Need for an ORM

• Akka Persistence (JVM)

Page 132: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Persistence & Event Storage• No Real Need for an ORM

• Akka Persistence (JVM)

• Database Agnostic

Page 133: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 134: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 135: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 136: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Questions?

Page 137: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Agenda• Event Sourcing

• Event Sourcing Challenges

• Don’t Worry, ES Is Worth It

• Querying

• Implementation (Theory)

• Implementation (For Real)

• Any Tools Available?

• Demo?

Page 138: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Demo Time!

Page 139: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Page 140: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

In Summary

Page 141: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Event Sourcing -> Additive Only, Lossless. Can Be Tricky To Work

With

Page 142: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Not for your Mom’s Blog

Page 143: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

Thank You!

Page 144: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Any Questions?

Steve Pember@svpember

www.thirdchannel.com

Page 145: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

More on Event Sourcing• ThirdChannel Event Source Library (Java): https://github.com/RSV2/eventsource• Demo App: https://github.com/spember/event-source-demo• “Event Sourcing” (Martin Fowler): http://martinfowler.com/eaaDev/EventSourcing.html• Event Store & “Event Sourcing Basics”: http://docs.geteventstore.com/introduction/event-sourcing-basics/• Event Sourcing presentation by Greg Young: https://www.youtube.com/watch?v=JHGkaShoyNs

Page 146: Gr8conf US 2015 - Intro to Event Sourcing with Groovy

THIRDCHANNEL @svpember

Image Credits• BOA: http://marketmadhouse.com/wp-content/uploads/2014/11/BofA.jpg• Khaaaaan:

http://d1oi7t5trwfj5d.cloudfront.net/38/0d9b80ae5311e1bcc4123138165f92/file/5-things-you-might-not-know-about-star-trek-ii-wrath-of-khan-30th-anniversary.jpg

• tribbles: http://de.memory-alpha.org/wiki/Tribble• bank ledger: http://www.iowall.net/accounting-ledger-book.html• ORM: https://guides.codepath.com/android/ActiveAndroid-Guide• hoarders: http://www.aetv.com/hoarders• snapshot stream: https://geteventstore.com/• office space bobs: http://galleryhip.com/office-space-bobs.html• Nye, Obama, Tyson photo: http://en.wikipedia.org/wiki/Selfie• Printing Press: http://www.directindustry.com/prod/koenig-bauer-ag/offset-printing-presses-high-volume-40462-602574.html