Slide 1

Slide 1 text

The Dark Side of Event Sourcing Managing Data Conversion Michiel Overeem, Marten Spoor, Slinger Jansen

Slide 2

Slide 2 text

Lead Software Architect @ AFAS Software PhD Candidate @ University Utrecht @michielovereem Software Architect @ AFAS Software @martenspoor

Slide 3

Slide 3 text

AFAS Software 400 employees 100 million euro revenue

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

x 10.000

Slide 6

Slide 6 text

x 10.000 DEV.AFAS.NL

Slide 7

Slide 7 text

Adaptable Model-based and User-specific Software Ecosystems

Slide 8

Slide 8 text

Copyright Nasa Goddard

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

How can an event sourced system be upgraded efficiently when the (implicit) event schema changes? Operations Techniques Strategies

Slide 11

Slide 11 text

Event sourcing Event BankAccountCreated (accountId: 1234567, balance: 0, owner: Michiel); accountId balance owner 1234567 0 Michiel Current state Event sourcing

Slide 12

Slide 12 text

Event sourcing Event BankAccountCreated (accountId: 1234567, balance: 0, owner: Michiel); DepositPerformed (accountId : 1234567, amount: 100, balance: 100); accountId balance owner 1234567 100 Michiel Current state Event sourcing

Slide 13

Slide 13 text

Event sourcing Event BankAccountCreated (accountId: 1234567, balance: 0, owner: Michiel); DepositPerformed (accountId : 1234567, amount: 100, balance: 100); WithdrawalPerformed (accountId: 1234567, amount: 50, balance: 50); accountId balance owner 1234567 50 Michiel Current state Event sourcing

Slide 14

Slide 14 text

Event sourcing Event BankAccountCreated (accountId: 1234567, balance: 0, owner: Michiel); DepositPerformed (accountId : 1234567, amount: 100, balance: 100); WithdrawalPerformed (accountId: 1234567, amount: 50, balance: 50); OwnerChanged (accountId: 1234567, newOwner: Marten); accountId balance owner 1234567 50 Marten Current state Event sourcing

Slide 15

Slide 15 text

The structure of an Event Store type Attribute = (string, System.Object) type Event = (string, Attribute[]) type EventStream = (string, (DateTime, Event[])[] type EventStore = EventStream[]

Slide 16

Slide 16 text

But there is also a schema Which streams are known in the system? Which events are allowed in which streams? Which attributes are allowed in which events?

Slide 17

Slide 17 text

Example schema: webshop Webshop EventStore Customer Stream ShoppingCart Stream CustomerSignedUp PriviligeUpgraded … AddedToChart ChangedQuantity …

Slide 18

Slide 18 text

Add attribute to event CustomerSignedUp Newsletter: bool We add newsletter functionality, and users can opt-in at signup

Slide 19

Slide 19 text

Add event to stream OptInOnNewsLetter Or opt-in at a later moment

Slide 20

Slide 20 text

Add stream to store And of course management of newsletters Newsletter Stream Create Publish …

Slide 21

Slide 21 text

Split attribute in event CustomerSignedUp FullName: string We chose the wrong attributes for name

Slide 22

Slide 22 text

Split attribute in event CustomerSignedUp FullName: string We chose the wrong attributes for name CustomerSignedUp FirstName: string SurName: string

Slide 23

Slide 23 text

Split event in stream CustomerSignedUp We created events that were to large

Slide 24

Slide 24 text

Split event in stream We created events that were to large OptInOnNewsLetter CustomerSignedUp

Slide 25

Slide 25 text

Split streams We found new streams Customer Stream CustomerSignedUp PriviligeUpgraded … Newsletter subscribers Stream OptInOnNewsLetter … …

Slide 26

Slide 26 text

Store level operations Add / remove streams Merge streams / split / … streams Stream level operations Add / remove events Merge events / split / … events Event level operations Add / update / remove attributes Merge attributes / split / … attributes How can we change it?

Slide 27

Slide 27 text

How to execute these operations? Data upgrade Application upgrade Executed by Basic & Complex Event Basic & Complex Stream Basic & Complex Store Techniques Strategies

Slide 28

Slide 28 text

Multiple versions: always extend Webshop EventStore Customer Stream ShoppingCart Stream CustomerSignedUp PriviligeUpgraded … AddedToChart ChangedQuantity …

Slide 29

Slide 29 text

Multiple versions: always extend Webshop EventStore Customer Stream ShoppingCart Stream CustomerSignedUp PriviligeUpgraded … AddedToChart ChangedQuantity … CustomerSignedUp_V2

Slide 30

Slide 30 text

Is this maintainable? Becomes the codebase to complex?

Slide 31

Slide 31 text

Weakening the schema Which attributes can occure in which events? Which events can occure in which streams? Which streams can originate in the system?

Slide 32

Slide 32 text

Weak schema’s Webshop EventStore Customer Stream ShoppingCart Stream CustomerSignedUp PriviligeUpgraded … AddedToChart ChangedQuantity …

Slide 33

Slide 33 text

Weak schema’s Webshop EventStore Customer Stream ShoppingCart Stream CustomerSignedUp PriviligeUpgraded … AddedToChart ChangedQuantity … CustomerSignedUp

Slide 34

Slide 34 text

Explosion of code paths? And still not all operations are possible.

Slide 35

Slide 35 text

Webshop EventStore Customer Stream ShoppingCart Stream CustomerSignedUp PriviligeUpgraded … Upcasters: Evolve the events AddedToChart ChangedQuantity …

Slide 36

Slide 36 text

Webshop EventStore Customer Stream ShoppingCart Stream CustomerSignedUp PriviligeUpgraded … Upcasters: Evolve the events AddedToChart ChangedQuantity … CustomerSignedUp_V2

Slide 37

Slide 37 text

What is the run-time overhead? Are all operations possible?

Slide 38

Slide 38 text

Stream boundaries: sharding Webshop EventStore Customer Stream ShoppingCart Stream CustomerSignedUp PriviligeUpgraded … AddedToChart ChangedQuantity …

Slide 39

Slide 39 text

Webshop EventStore 2 Stream boundaries: sharding Webshop EventStore 1 Customer Stream ShoppingCart Stream CustomerSignedUp PriviligeUpgraded … AddedToChart ChangedQuantity …

Slide 40

Slide 40 text

How to execute these other operations? Techniques Data upgrade Application upgrade Executed by Executed by Basic & Complex Event Basic & Complex Stream Basic Store Complex Store Lazy transformation Upcasting Multiple versions Weak schema Strategies

Slide 41

Slide 41 text

Copyright Stephen Cairns Technical versus functional immutability

Slide 42

Slide 42 text

Lazy transformation

Slide 43

Slide 43 text

But when is this finished? Mixed appends and updates.

Slide 44

Slide 44 text

In place transformation Just find the events and fix them.

Slide 45

Slide 45 text

Not for the faint of heart!

Slide 46

Slide 46 text

Copy and transformation

Slide 47

Slide 47 text

Change all the things! Auto backup. Could be very slow.

Slide 48

Slide 48 text

Multiple versions Weak schema Upcasters Lazy transformation In place transformation Copy and transformation +/- - +/- +/- + + Operation completeness

Slide 49

Slide 49 text

Multiple versions Weak schema Upcasters Lazy transformation In place transformation Copy and transformation - +/- +/- +/- + + Maintainability

Slide 50

Slide 50 text

Multiple versions Weak schema Upcasters Lazy transformation In place transformation Copy and transformation + + + +/- +/- - Performance efficiency

Slide 51

Slide 51 text

Multiple versions Weak schema Upcasters Lazy transformation In place transformation Copy and transformation + + + - - + Reliability

Slide 52

Slide 52 text

Copyright SpaceX Data upgrade Application upgrade Executed by Executed by Basic & Complex Event Basic & Complex Stream Basic Store Complex Store Lazy transformation Upcasting Multiple versions Weak schema In place transfor- mation Copy and transfor- mation But how to deploy them? Strategies

Slide 53

Slide 53 text

Big flip

Slide 54

Slide 54 text

Big flip

Slide 55

Slide 55 text

Big flip

Slide 56

Slide 56 text

Big flip

Slide 57

Slide 57 text

Big flip

Slide 58

Slide 58 text

Big flip

Slide 59

Slide 59 text

Blue-Green

Slide 60

Slide 60 text

Blue-Green

Slide 61

Slide 61 text

Blue-Green

Slide 62

Slide 62 text

Blue-Green

Slide 63

Slide 63 text

Blue-Green

Slide 64

Slide 64 text

Rolling upgrade

Slide 65

Slide 65 text

Rolling upgrade

Slide 66

Slide 66 text

Rolling upgrade

Slide 67

Slide 67 text

Rolling upgrade

Slide 68

Slide 68 text

Rolling upgrade

Slide 69

Slide 69 text

But for database deployment we need another strategy.

Slide 70

Slide 70 text

Blue-Green

Slide 71

Slide 71 text

Blue-Green

Slide 72

Slide 72 text

Blue-Green

Slide 73

Slide 73 text

Blue-Green

Slide 74

Slide 74 text

Expand- Contract

Slide 75

Slide 75 text

Expand- Contract

Slide 76

Slide 76 text

Expand- Contract

Slide 77

Slide 77 text

Expand- Contract

Slide 78

Slide 78 text

Combining everything into a framework Data upgrade Deployed with Executed by Executed by Data upgrade Application upgrade Lazy transformation Upcasting In place transfor- mation Multiple versions Copy and transfor- mation Basic & Complex Event Basic & Complex Stream Basic Store Complex Store Big Flip Rolling Upgrade Blue-Green Expand- Contract Blue- Green Deployed with Combined with Weak schema

Slide 79

Slide 79 text

Beware of the projectors! Order of upgrading components still matters!

Slide 80

Slide 80 text

Beware of the projectors! Order of upgrading components still matters!

Slide 81

Slide 81 text

Beware of the projectors! Order of upgrading components still matters!

Slide 82

Slide 82 text

Beware of the projectors! Order of upgrading components still matters!

Slide 83

Slide 83 text

But what about the query side?

Slide 84

Slide 84 text

What’s next? 1. Do interviews. Interested? Contact me! https://goo.gl/forms/7tuBKW39EPkWnZLG3 2. Write a follow up paper with more information. 3. Publish the experiences in some form.

Slide 85

Slide 85 text

Early interview results

Slide 86

Slide 86 text

Do you read your events ever again? No? Then just change the snapshot.

Slide 87

Slide 87 text

Move out all events belonging to old aggregates.

Slide 88

Slide 88 text

Nobody uses lazy transformation.

Slide 89

Slide 89 text

Domain driven design helps in preventing changes.

Slide 90

Slide 90 text

There is a lack of experience sharing.

Slide 91

Slide 91 text

Your context determines the best upgrade strategy. m.overeem@afas.nl / m.spoor@afas.nl @michielovereem / @martenspoor