70
State or Events? Kenny Bastani Jakub Pilimon

State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

© 2017 Pivotal Software, Inc. All rights reserved.@kennybastani @JakubPilimon

State or events?

!1

State or Events? Kenny Bastani Jakub Pilimon

Page 2: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Kenny Bastani

@kennybastani

Global CTO @Pivotal

Page 3: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also
Page 4: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

© 2017 Pivotal Software, Inc. All rights reserved.@kennybastani @JakubPilimon

State or events?

!4

SZCZEBRZESZYNCHRZĄSZCZYŻEWOSZYCE

Page 5: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

© 2017 Pivotal Software, Inc. All rights reserved.@kennybastani

The agenda• Event sourcing, a history

• Why event sourcing?

• Apache Kafka and the immutable log

• Event-driven microservices

• Reference architecture

!5

Page 6: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

© 2017 Pivotal Software, Inc. All rights reserved.@kennybastani @JakubPilimon

Event sourcing

A mystery, a history…

!6

Page 7: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Origin story

Page 8: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also use the event log to reconstruct past states.

We can determine the application state at any point in time. We do this by starting with a blank state and rerunning the events up to a particular time. –Martin Fowler

Page 9: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Did Martin Fowler invent event sourcing?

Page 10: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Edgar Allan Poe (1804-1849)

We appreciate time by events alone. For this reason we define time (somewhat improperly) as the succession of events; but the fact itself--that events are our sole means of appreciating time-- [leads to] the erroneous idea that events are time--that the more numerous the events, the longer the time; and the converse.

Page 11: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Did Edgar Allan Poe invent event sourcing?

Lol no…

Page 12: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Ship

Chip Log15th-16th Century

Drag

Knots

Ocean

Log ReelLog-line

Page 13: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

© 2017 Pivotal Software, Inc. All rights reserved.@kennybastani

The Poe connection• Poe was obsessed with the idea of event sourcing for much of his life. • But in his time, things were commonly measured using a form of event

sourcing, like the knots on the log-line for measuring the speed of ships.

• Most software is built on metaphors. The problem is, we’ve forgotten the metaphors have meaning.

!13

Page 14: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Marginalia, Edgar Allan Poe, 1849

Page 15: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Marginalia, Edgar Allan Poe, 1849

Page 16: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Ship

Chip Log15th-16th Century

Events (Objects)

Aggregation takes causal events across space and time andsummarizes it as a single object that’s easier to store in memory

TimeSpace

Page 17: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

All great software is built on metaphors

Desktop

Cloud

Web

Infrastructure

Virtual MachineState

Events

Services

Logs

Kernel

Tablets

Application

Instance

SpaceTimestamp

Memory

Containers

Database

Platform

Computer

RefactorFiles Pages

Documents

Page 18: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

The metaphors mean something• There’s an underlying abstraction to our own humanity and it has an

intimate connection to the virtual world we are building • The desktop computer replaced actual desktops by replacing the

abstract functionality of the desktop • The virtual machine replaced the server by virtualizing the abstract

functionality of the hardware

Page 19: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

But ultimately, it’s hard to keep track of all the layers.

Page 20: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Eventually we get lost in abstraction.

Page 21: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Serverless

Microservices

NoSQLSOA

Middleware

All confusing software is built on nonsense

DevOps

ESB

Page 22: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

I’m not saying microservices, DevOps, and serverless are nonsense.

Page 23: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

I’m saying that metaphors make good ideas more durable to nonsensical interpretation.

Page 24: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Ship

Chip Log15th-16th Century

Drag

Knots

Ocean

Log ReelLog-line

LOG!

Page 25: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

LOG!

Page 26: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also
Page 27: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

© 2017 Pivotal Software, Inc. All rights reserved.@kennybastani @JakubPilimon

Apache Kafka

The immutable log as the source of truth

!27

Page 28: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Time-ordered events

Page 29: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Store state as a log of events

https://www.confluent.io/blog/messaging-single-source-truth/

Page 30: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Ship

Chip Log15th-16th Century

Drag

Knots

Ocean

Log ReelLog-line

It’s the same thing, but way before software

Page 31: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also
Page 32: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

© 2017 Pivotal Software, Inc. All rights reserved.@kennybastani @JakubPilimon

Motivations

What’s the benefit of managing state in a distributed system?

!32

Page 33: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Typical way of managing state

Account (pending,

10$)Account (active,

10$)

Account (archived,

10$)

Account (suspended,

10$)

Page 34: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

State machine

Account (pending,

10$)Account (active,

10$)

Account (archived,

10$)

Account (suspended,

10$)

Page 35: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Audit of states and transitions

Account (pending,

10$)Account (active, 10$)

Account (archived,

10$)

Account (active, 15$)

Page 36: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Event SourcingAggregates can be used to generate the consistent state of any object

It provides an audit trail that can be replayed to generate the state of an object from any point in time

It provides the many inputs necessary for analyzing data using event stream processing

It enables the use of compensating transactions to rollback events leading to an inconsistent application state

Page 37: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

So why aren’t we doing it everywhere?

Page 38: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Because storing state as an immutable log of events for domain data didn’t make sense in a time B.C.

Page 39: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

B.C. = Before Cloud

– Andrew Clay Shafer

Page 40: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

https://www.confluent.io/blog/messaging-single-source-truth/

Page 41: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

https://www.confluent.io/blog/messaging-single-source-truth/

Page 42: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

The Immutable Log

https://www.confluent.io/blog/messaging-single-source-truth/

Page 43: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

The Immutable Log

Page 44: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Event Sourcing and GDPR

What if somebody requests to have their immutable data removed from

a log?

Page 45: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

How about event-driven (micro)services?

Page 46: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

There are no foreign key constraints between (micro)services.

Page 47: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Domain Graph

Page 48: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Domain Relationships

Page 49: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

No foreign key constraints across services

ForeignKey

Relationships

PRODUCTSKUS

(Product ID)

Page 50: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Distributed joins are slow (reads)

Page 51: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Distributed transactions are brittle (writes)

Page 52: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

(Micro)service joins

• Services are organized using domain-driven design

• Services cannot own domain data from two different bounded contexts

• Complex joins (reads) are inevitable

• Avoid distributed transactions (write)

Page 53: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

What is CQRS?

CQRS (Command Query Responsibility Segregation) is a pattern for separating handlers for commands and

queries.

Handlers synchronize by turning events into materialized views.

Page 54: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

How do I do CQRS in a distributed system?

Let’s look at a system architecture that uses Spring Boot

Page 55: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

COMMANDSGENERATE

EVENTS

Page 56: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

EVENTSARE BROADCASTED TO OTHER SERVICES

Page 57: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

EVENT LISTENERSBUILD QUERY

MODELS

Page 58: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

API GATEWAYS HIDE THEIMPLEMENTATION DETAILS

API API

Page 59: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

READING YOUR OWN WRITES?

WRITE API READ API

Page 60: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

© 2017 Pivotal Software, Inc. All rights reserved.@kennybastani @JakubPilimon

Reference architectureCreating a social network as microservices

!60

Page 61: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

How do you do friend of a friend queries with microservices?

Page 62: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

User User

UserUser User

Friend

Friend FriendFriendFriend

Page 63: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

User User

UserUser User

Friend

Friend FriendFriendFriend

Day 1 of building microservices

Day 2 of building microservices…

Page 64: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Materialized views

Page 65: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

Create aggregate views from event data

Page 66: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also
Page 67: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

• Manage the storage of domain data that it owns.

• Produce the API contract for the domain data that it owns.

• Produce events when the state of any domain data changes.

• Maintain relationship integrity to domain data owned by other services.

Page 68: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

• Subscribe to domain events emitted by separate domain services.

• Maintain an ordered immutable event log for events it receives.

• Create connected query projections of distributed domain data.

• Provide performant read-access to complex views of domain data.

Page 70: State or Events - GOTO Conference...Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not only can we query these events, we can also

© 2017 Pivotal Software, Inc. All rights reserved.@kennybastani @JakubPilimon

Thanks!

Q/A

!70

https://github.com/kbastani/event-sourcing-microservices-basics

kennybastani.com

Thanks!

Q/A

https://github.com/kbastani/event-sourcing-microservices-basics

https://github.com/ddd-by-examples/all-things-cqrs

https://github.com/ddd-by-examples/event-source-cqrs-sample