Persistence that tells the truth: event sourcing

17
Persistence that tells the truth Ryan Platte [email protected]

Transcript of Persistence that tells the truth: event sourcing

Page 1: Persistence that tells the truth: event sourcing

Persistence that tells the truthRyan [email protected]

Page 2: Persistence that tells the truth: event sourcing

Your model lies

Page 3: Persistence that tells the truth: event sourcing

Your model lies

“Customer 123 ordered the inexpensive product”

Page 4: Persistence that tells the truth: event sourcing

The truth is what actually happens

Page 5: Persistence that tells the truth: event sourcing

The truth is what actually happens

“Customer 123 ordered the expensive product and the cheaper one, then removed the expensive one after checking shipping”

Page 6: Persistence that tells the truth: event sourcing

So why not persist the whole truth?

Page 7: Persistence that tells the truth: event sourcing

So why not persist the whole truth?

Added Product

Customer 123

Product 1

Computed Shipping

Added Product

Customer 123

Product 2

Removed Product

Customer 123

Product 2

Page 8: Persistence that tells the truth: event sourcing

Past history is immutable

Page 9: Persistence that tells the truth: event sourcing

Ask your accountant whether it’s OK to change historical records

Page 10: Persistence that tells the truth: event sourcing

Append-only writes scalehttps://flic.kr/p/obgzhM

Page 11: Persistence that tells the truth: event sourcing

This is how many databases work internally anyway

Page 12: Persistence that tells the truth: event sourcing

Working from the whole truth supports future features

Page 13: Persistence that tells the truth: event sourcing

How to create event-sourced software

Page 14: Persistence that tells the truth: event sourcing

Command-event separation

Imperative vs. past tense

Command is an attempt or request

Event is a change that definitely registered

Page 15: Persistence that tells the truth: event sourcing

Persisting events in a database

Minimal implementation

Events

Aggregate_id (GUID)Data (blob or JSON)Version (integer)

Aggregates, or Event ProviderAggregate_id (GUID)Type (string)Version (integer)

Page 16: Persistence that tells the truth: event sourcing

Persisting events in a database

Events

Aggregate_id (GUID)Data (blob or JSON)Version (integer)TimestampIP addressPermission level

Aggregates, or Event ProviderAggregate_id (GUID)Type (string)Version (integer)

Page 17: Persistence that tells the truth: event sourcing

Read model: go nuts

Order openedItem addedItem added

Item removedOrder placed

Order approvedOrder shipped

Order delivered

Objects in memory

Mutable DB model

Graph DB

Work queues