a gentle introduction to event databases · 2016-05-16 · in the real world people carry out...

31
A gentle introduction to event databases Dino Esposito JetBrains dino.esposito@jetbrains.com @despos facebook.com/naa4e

Upload: others

Post on 04-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

A gentle introduction to eventdatabases

Dino EspositoJetBrains

[email protected]@despos

facebook.com/naa4e

Page 2: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

In the real world people carry out actions.

In software, we love to have models.

Actions alter the state and generate events.

In the real world, people observe events.

Page 3: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

MODELING BASED ON BEHAVIOR

MODELING BASED ON DATA

Page 4: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

ImmutableCan be

replayedActivity

log

EVENTS are the description of

something that happened.

Page 5: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

YOU DON’T MISS A THING

for the entire lifetime of the system

Page 6: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

Logging the list of

commands lets you

keep track of the

latest without

forgetting anything

that happened in

the past.

PS: if you really want to forget … go with plain CRUD.

Page 7: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

DATA MODEL

USER EXPERIENCE USER EXPERIENCE

REQUIRED ACTIONS

DATA MODEL

REQUIRED ACTIONS

Page 8: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

A DATA MODEL

AT THE FOUNDATION OF A

SOFTWARE SYSTEM

is cumbersome and coercing

Page 9: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

BehaviorExpected UX

PersistencePolyglot data

Page 10: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

POLYGLOT

DATA?To be best of my knowledge, I’m not biased

Page 11: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

Sparse Thoughts

NoSQL came out to replace SQL but it only took rigor and diligence away from the data design process

• Which is, from the bottom of data upNoSQL didn’t change the way we envision systems

• None of works alone

• If one works alone is SQLTechnically speaking, NoSQL and SQL specializes in different areas

Polyglot means use the data model (aka, the tool) right for the job

Polyglot data pushes a task-oriented view of the system

Page 12: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

Both SQL and NoSQL can be used, under

the hood, to save and read event data.

What’s event data, exactly?

Page 13: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

Event Sourcing

All changes made to the application state during the entire lifetime of the system are stored as a sequence of events.

Events are your native data; hence, your data source.

Page 14: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

Events are not common in the vast majority of today’s applications.

Today’s applications mostly store the current state and use it to process business transactions.

Page 15: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

From CRUD to Historical CRUD

DEMO

Page 16: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

Event-based

representation of data Event

Sourcing

CRUDCurrent State Storage

Page 17: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

Event-based

representation of data

Projection

of dataProjection

of dataProjection

of data

Page 18: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

Event-based

representation of data

Projection

of data

Projection

of data

Projection

of data

EVENT DATABASES

INTERNAL STORAGE

Page 19: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

Aspects of an Event Database

Allows you to log application events

Requested, Created, Denied, Deleted, Postponed, …

Allows you to query application events

Last N events, all events since T1, all events from T2

More advanced features

Data streaming, subscriptions

Page 20: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

CREATE Operations

CREATEDAGGREGATE

IDTIMESTAMP DETAILS

CREATEDAGGREGATE

IDTIMESTAMP DETAILS

CREATEDAGGREGATE

IDTIMESTAMP DETAILS

Request

Append new record

Global event ID

Some code describing the operation

Timestamp

Entity identifier

Information associated with the event

Information

Transparent storage: relational, NoSQL, graph

Page 21: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

UPDATE Operations

CREATED 888998TIMESTAMP DETAILS

CREATED 123456TIMESTAMP DETAILS

UPDATED 123456TIMESTAMP DELTA

Request

Append new record with DELTA

Global event ID

Some code describing the operation

Timestamp

Entity identifier

Changes applied

Information

Transparent storage: relational, NoSQL, graph

Page 22: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

In some cases, you might want to store

the full state of the entity along with the

specific event information.

Event recordCurrent state of the

entity

Page 23: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

DELETE Operations

CREATED 888998TIMESTAMP DETAILS

UPDATED 123456TIMESTAMP DELTA

DELETED 123456TIMESTAMP REASON

Request

Append new record to mark logical deletion

Global event ID

Code for the operation

Timestamp

Entity identifier

Optionally reasons of deletion

Information

Transparent storage: relational, NoSQL, graph

Page 24: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

Quick Facts

Append-only working mode

Updates saved as the delta

Logical deletion Physical deletion exceptionally allowed but not in the

middle of the stream

Page 25: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

REPLAY Operations

Grab all events by

entity IDCREATED 123456TIMESTAMP DETAILS

UPDATED 123456TIMESTAMP DELTA

64

92

Replay through all

events

Page 26: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

public static T Replay<T>(String id, IEnumerable<BaseEvent> events){

var aggregate = new T();foreach (var e in events){

if (e is AggregateCreatedEvent)aggregate.Create(e.Data);

if (e is AggregateUpdatedvent)aggregate.Update(e.Data);

if (e is AggregateDeletedEvent)aggregate.Delete(e.Data);

}return aggregate;

}}

Page 27: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

Replay just selected events

Perform different calculations

Apply different forms of business logic

Page 28: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

SCALABILITY POTENTIAL

Page 29: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

Serialize the state of

aggregates to a given

point in time

SNAPSHOT

Replay events from a

given snapshot

Page 30: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

TOOLS?

Page 31: A gentle introduction to event databases · 2016-05-16 · In the real world people carry out actions. In software, ... Event Sourcing All changes made to the application state during

FOLLOW

That’s All Folks!

facebook.com/naa4e

software2cents.wordpress.com

[email protected]

@despos