akka persistence and eventuate

134
Akka Persistence and Eventuate A CQRS/ES tool comparison Martin Krasser (@mrt1nz) Freelance software developer/architect

Upload: martin-krasser

Post on 18-Jan-2017

9.020 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Akka Persistence and Eventuate

Akka Persistence and EventuateA CQRS/ES tool comparison

Martin Krasser (@mrt1nz) Freelance software developer/architect

Page 2: Akka Persistence and Eventuate

History

Eventsourced Akka Persistence

Eventuate

7.2012 9.2013 1.2015

Page 3: Akka Persistence and Eventuate

Similarities

Page 4: Akka Persistence and Eventuate

Basis technologies

Scala

Akka actors

Akka streams

(Pluggable) storage backends

Page 5: Akka Persistence and Eventuate

Akka actor

Actor

State

Behavior

Mailboxtransient

non-durable

Page 6: Akka Persistence and Eventuate

Akka Persistence and Eventuate

Persist actor state via event sourcing

Page 7: Akka Persistence and Eventuate

Event sourcing

Capture all changes to application state as sequence of events

Store events in event log (append-only)

Replay events to recover application state

Page 8: Akka Persistence and Eventuate

Event sourcing

Commands - actions, can fail

Events - immutable facts

Page 9: Akka Persistence and Eventuate

Event-sourced actor

Actor

State

Behavior

Mailbox

Command handler

Event handler

Page 10: Akka Persistence and Eventuate

Command processingEvent-sourced actor

State

Command handler

Event handler

Event log

Page 11: Akka Persistence and Eventuate

Command processingEvent-sourced actor

State

Command handler

Event handler

Command

Event log

Page 12: Akka Persistence and Eventuate

Command processingEvent-sourced actor

State

Command handler

Event handler

Command

Event log

Events

Page 13: Akka Persistence and Eventuate

Command processingEvent-sourced actor

State

Command handler

Event handler

Command

Event log

Events

Events

Page 14: Akka Persistence and Eventuate

Command processingEvent-sourced actor

State

Command handler

Event handler

Command

Event log

Events

Events

Page 15: Akka Persistence and Eventuate

Command processingEvent-sourced actor

State

Command handler

Event handler

Command

Reply

Event log

Events

Events

Page 16: Akka Persistence and Eventuate

State recoveryEvent-sourced actor

State

Command handler

Event handler

Event log

Page 17: Akka Persistence and Eventuate

State recoveryEvent-sourced actor

State

Command handler

Event handler

Event log

Events (replay)

Page 18: Akka Persistence and Eventuate

State recoveryEvent-sourced actor

State

Command handler

Event handler

Event log

Events (replay)

Page 19: Akka Persistence and Eventuate

Akka Persistence and Eventuate

Separate command processing from query processing

Page 20: Akka Persistence and Eventuate

CQRS

Command QueryResponsibility Segregation

Different data models for command processing and query processing

Page 21: Akka Persistence and Eventuate

CQRS

Write model for command processing

Read model for query processing

Page 22: Akka Persistence and Eventuate

CQRS

Can be well combined with event sourcing (CQRS/ES)

Page 23: Akka Persistence and Eventuate

CQRS/ES

Write model

Event log

Command Query

Events (r/w) Events (r)

Command side Query side

Read model

Page 24: Akka Persistence and Eventuate

Abstractions

Command side Query side

AkkaPersistence

PersistentActor PersistentView Source[E,M]

Eventuate EventsourcedActorEventsourcedView

EventsourcedProcessorSource[E,M]

*) work in progress

Page 25: Akka Persistence and Eventuate

PersistentActor EventsourcedActor

Page 26: Akka Persistence and Eventuate

PersistentActor EventsourcedActor

State

Page 27: Akka Persistence and Eventuate

PersistentActor EventsourcedActor

Command handler

Page 28: Akka Persistence and Eventuate

PersistentActor EventsourcedActor

Event handler

Page 29: Akka Persistence and Eventuate

PersistentActor EventsourcedActor

Snapshot handler

Page 30: Akka Persistence and Eventuate

DifferencesCommand side

Page 31: Akka Persistence and Eventuate

At a glance

https://twitter.com/mrt1nz/status/573382831889653760

Page 32: Akka Persistence and Eventuate

Consistency

Akka Persistence and Eventuate support strong consistency

Eventuate additionally supports relaxation to causal consistency

Page 33: Akka Persistence and Eventuate

Akka Persistence

Enforces strong consistency on command side

PersistentActors must be global singletons

No actor/state replication

Page 34: Akka Persistence and Eventuate

Eventuate

EventsourcedActors can be replicated

State replication via reliable, asynchronous event replication

Replication across locations (availability zones)

Page 35: Akka Persistence and Eventuate

Replication

Location ALocation B

Location C

Page 36: Akka Persistence and Eventuate

Replication

Location A

Application state

Location B

Location C

Page 37: Akka Persistence and Eventuate

Replication

Events

Events

Location A

Application state

Location B

Location C

Page 38: Akka Persistence and Eventuate

Replication

Events

Events

Location A

Application state

Location B

Location C

Page 39: Akka Persistence and Eventuate

Replication

Command

Location ALocation B

Location C

Page 40: Akka Persistence and Eventuate

Replication

Command XLocation A

Location B

Location C

Page 41: Akka Persistence and Eventuate

Replication

Command X

Events

Events

Location ALocation B

Location C

Page 42: Akka Persistence and Eventuate

Replication

Command X

X

X

Events

Events

Location ALocation B

Location C

Page 43: Akka Persistence and Eventuate

Eventuate

Replicas writeable at all locations = multi-master

Write-availability during network partitions

Page 44: Akka Persistence and Eventuate

EventuateWrite conflicts

detection

tracking

automated resolution

interactive resolution

Page 45: Akka Persistence and Eventuate

Partition

Location ALocation B

Location C

Page 46: Akka Persistence and Eventuate

Partition

Command

Location ALocation B

Location C

Page 47: Akka Persistence and Eventuate

Partition

Command XLocation A

Location B

Location C

Page 48: Akka Persistence and Eventuate

Partition

Command XEvents

Location ALocation B

Location C

Page 49: Akka Persistence and Eventuate

Partition

Command X XEvents

Location ALocation B

Location C

Page 50: Akka Persistence and Eventuate

Partition

Command

Command

X XEvents

Location ALocation B

Location C

Page 51: Akka Persistence and Eventuate

Partition

Command

Command

X X

Y

Events

Location ALocation B

Location C

Page 52: Akka Persistence and Eventuate

Partition

Command

Command

X X

Y

Events

Location ALocation B

Location C

Page 53: Akka Persistence and Eventuate

Partition

Command

Command

X X

Y

EventsEvents

Events

Location ALocation B

Location C

Page 54: Akka Persistence and Eventuate

Conflict

XY XY

XY

Location ALocation B

Location C

Page 55: Akka Persistence and Eventuate

Resolution

XY XY

XY

Interactive resolution

XY => Z

Location ALocation B

Location C

Page 56: Akka Persistence and Eventuate

Resolution

XY

XY

Interactive resolution

XY => ZZ

Location ALocation B

Location C

Page 57: Akka Persistence and Eventuate

Resolution

XY

XY

Interactive resolution

XY => ZZ

Events

Events

Location ALocation B

Location C

Page 58: Akka Persistence and Eventuate

Resolution

Interactive resolution

XY => ZZ Z

Z

Events

Events

Location ALocation B

Location C

Page 59: Akka Persistence and Eventuate

Eventuate

Strong consistency within location*)

Causal consistency across locations

Causality is tracked with vector clocks

*) relaxation to causal consistency also possible within location

Page 60: Akka Persistence and Eventuate

Causal consistency example

Distributed chat application

FIFO reliable broadcast vs.

Causal reliable broadcast

Page 61: Akka Persistence and Eventuate

Inspired by ACM article “Don’t Settle for Eventual Consistency”

http://queue.acm.org/detail.cfm?id=2610533

Causal consistency example

Page 62: Akka Persistence and Eventuate

FIFO rel. broadcastDC1

DC2

DC3

Page 63: Akka Persistence and Eventuate

FIFO rel. broadcast

- Alice: Lost my wedding ring

Alice

DC1DC2

DC3

Page 64: Akka Persistence and Eventuate

FIFO rel. broadcast

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

Alice

DC1DC2

DC3

Page 65: Akka Persistence and Eventuate

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

Alice

- Alice: Whew, found it upstairs

DC1DC2

DC3

FIFO rel. broadcast

Page 66: Akka Persistence and Eventuate

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

Alice

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs

DC1DC2

DC3

FIFO rel. broadcast

Page 67: Akka Persistence and Eventuate

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

Alice

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs

DC1DC2

DC3

FIFO rel. broadcast

Page 68: Akka Persistence and Eventuate

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

Bob

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs- Bob: I’m glad to hear that

DC1DC2

DC3

FIFO rel. broadcast

Page 69: Akka Persistence and Eventuate

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

Bob

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs

- Bob: I’m glad to hear that- Bob: I’m glad to hear that

DC1DC2

DC3

FIFO rel. broadcast

Page 70: Akka Persistence and Eventuate

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs

- Bob: I’m glad to hear that- Bob: I’m glad to hear that

- Bob: I’m glad to hear that

DC1DC2

DC3

Bob

FIFO rel. broadcast

Page 71: Akka Persistence and Eventuate

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs

- Bob: I’m glad to hear that- Bob: I’m glad to hear that

- Bob: I’m glad to hear that

DC1DC2

DC3

FIFO rel. broadcast

Causality violation!

Page 72: Akka Persistence and Eventuate

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs

- Bob: I’m glad to hear that- Bob: I’m glad to hear that

- Bob: I’m glad to hear that- Alice: Whew, found it upstairs

DC1DC2

DC3

FIFO rel. broadcast

Retry

Causality violation!

Page 73: Akka Persistence and Eventuate

Causal rel. broadcast

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs

DC1DC2

DC3

Page 74: Akka Persistence and Eventuate

Causal rel. broadcast

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

Bob

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs- Bob: I’m glad to hear that

DC1DC2

DC3

Page 75: Akka Persistence and Eventuate

Causal rel. broadcast

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

Bob

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs- Bob: I’m glad to hear that

Causalitycheck

DC1DC2

DC3

Page 76: Akka Persistence and Eventuate

Causal rel. broadcast

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

Bob

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs

- Bob: I’m glad to hear that- Bob: I’m glad to hear that

Causalitycheck

DC1DC2

DC3

Page 77: Akka Persistence and Eventuate

Causal rel. broadcast

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

Bob

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs

- Bob: I’m glad to hear that- Bob: I’m glad to hear that

Causalitycheck

Causalitycheck

DC1DC2

DC3

Page 78: Akka Persistence and Eventuate

Causal rel. broadcast

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

Bob

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs

- Bob: I’m glad to hear that- Bob: I’m glad to hear that

- Bob: I’m glad to hear that

Buffer Causalitycheck

Causalitycheck

DC1DC2

DC3

Page 79: Akka Persistence and Eventuate

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs

- Bob: I’m glad to hear that- Bob: I’m glad to hear that

- Bob: I’m glad to hear that

Buffer Causalitycheck

Retry

DC1DC2

DC3

Causal rel. broadcast

Page 80: Akka Persistence and Eventuate

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs

- Bob: I’m glad to hear that- Bob: I’m glad to hear that

- Bob: I’m glad to hear that

Buffer Causalitycheck

- Alice: Whew, found it upstairs

Retry

DC1DC2

DC3

Causal rel. broadcast

Page 81: Akka Persistence and Eventuate

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs

- Bob: I’m glad to hear that- Bob: I’m glad to hear that

- Bob: I’m glad to hear that

Buffer Causalitycheck

- Bob: I’m glad to hear that- Alice: Whew, found it upstairs

Retry

DC1DC2

DC3

Causal rel. broadcast

Page 82: Akka Persistence and Eventuate

- Alice: Lost my wedding ring- Alice: Lost my wedding ring

- Alice: Lost my wedding ring

- Alice: Whew, found it upstairs- Alice: Whew, found it upstairs

- Bob: I’m glad to hear that- Bob: I’m glad to hear that

- Bob: I’m glad to hear that- Alice: Whew, found it upstairsCausal re-ordering

DC1DC2

DC3

Causal rel. broadcast

Page 83: Akka Persistence and Eventuate

Causal consistency

Causally related events are delivered in same order at all locations

Concurrent events can be delivered in any order at different locations

Page 84: Akka Persistence and Eventuate

DifferencesEvent log

Page 85: Akka Persistence and Eventuate

“Local” event log

Eventuate

- LevelDB - Cassandra

Location ALocation B

Location C

Page 86: Akka Persistence and Eventuate

“Local” event log

“Replicated” event log

Eventuate

- LevelDB - Cassandra

Location ALocation B

Location C

- LevelDB - Cassandra

EventsEvents

Events

Page 87: Akka Persistence and Eventuate

Local event log

Storage order consistent withcausal order

Given the potential causality relation ➞ of events

Local event logs are linear extensions of ➞

Page 88: Akka Persistence and Eventuate

Replicated event log

Same causal event storage order at different locations

Different total event storage order at different locations

Page 89: Akka Persistence and Eventuate

Akka Persistence- LevelDB - Cassandra - EventStore - MongoDB - Kafka - … Location A

Location B

Location C

Page 90: Akka Persistence and Eventuate

DifferencesEvent collaboration

Page 91: Akka Persistence and Eventuate

Akka Persistence

PA

PA

PAPA

Event log:

private for writing

shared for reading (query side)

Page 92: Akka Persistence and Eventuate

Eventuate

EA

EAEA

Event log:

shared for writing

shared for reading

EA

Page 93: Akka Persistence and Eventuate

Event routing

EA

EA

EA

Page 94: Akka Persistence and Eventuate

Event routing

EA

EA

EAe1

Page 95: Akka Persistence and Eventuate

Event routing

EA

EA

EAe1

e1

Page 96: Akka Persistence and Eventuate

Event routing

EA

EA

EAe1

e1

All

Topic

Aggregate id

Page 97: Akka Persistence and Eventuate

Event routing

EA

EA

EAe1

e1

EAEA

e1

EA

e1

Location A

Location B

Location C

Page 98: Akka Persistence and Eventuate

Event collaboration

State replication

Service interaction

Page 99: Akka Persistence and Eventuate

Event collaboration

S2

S1

S1

S3

Location A

Location B

Location C

Page 100: Akka Persistence and Eventuate

Event collaboration

S2

S1e1

S1

S3

Location A

Location B

Location C

Page 101: Akka Persistence and Eventuate

Event collaboration

S2

S1e1

S1

e1

S3

Location A

Location B

Location C

Page 102: Akka Persistence and Eventuate

Event collaboration

S2

S1e1

e1

S1

e1

S3

e1

Location A

Location B

Location C

Page 103: Akka Persistence and Eventuate

Event collaboration

S2

S1e1

e1

S1

e1

S3

e1

Location A

Location B

Location Ce2

Page 104: Akka Persistence and Eventuate

Event collaboration

S2

S1e1

e1

S1

e1

S3

e1

Location A

Location B

Location Ce2

e2

Page 105: Akka Persistence and Eventuate

Event collaboration

Reliable, distributed and partition-tolerant business processes

Event-driven (micro-)service architectures

Page 106: Akka Persistence and Eventuate

Event collaboration

Reliable event delivery

Causal event delivery

De-duplicated event stream

Page 107: Akka Persistence and Eventuate

DifferencesQuery side

Page 108: Akka Persistence and Eventuate

Akka Persistence 2.3PA

PA

PV

PV

PV

PersistentViews on single PersistentActor (default)

Page 109: Akka Persistence and Eventuate

Akka Persistence 2.4PA

PA

S

S

S

Stream Sources for multiple PersistentActors(requires storage plugin support)

Page 110: Akka Persistence and Eventuate

Eventuate

EV

EV

EventsourcedViews on multiple EventsourcedActors(default, shared local log)

EA

EA

EA

Page 111: Akka Persistence and Eventuate

Eventuate

EV

EV

EventsourcedViews on multiple EventsourcedActors(default, shared replicated log)

EA

EA

EA

EA

EAEA

Location A

Location B

Location C

Page 112: Akka Persistence and Eventuate

Consistency in CQRS

Eventual consistency

Causal consistency

AkkaPersistence

usually per PA(plugin-specific)

Eventuate

Page 113: Akka Persistence and Eventuate

Causal consistency

Single EA/EV: default

Across EA/EV: conditional requests

Page 114: Akka Persistence and Eventuate

Example scenario

Update a write model (EA)

Query a read model (EV)

Query result should include effect that was caused by the update

Page 115: Akka Persistence and Eventuate

Conditional request

EA

Location A

EV

Location B

Page 116: Akka Persistence and Eventuate

Conditional request

EA

Location A

EV

Location B

cmd

Page 117: Akka Persistence and Eventuate

Conditional request

EA

Location A

EV

Location B

evt

cmd

Page 118: Akka Persistence and Eventuate

Conditional request

EA

Location A

EV

Location B

evtevt(vts)

cmd

vts = vector timestamp (a “condition”)

Page 119: Akka Persistence and Eventuate

Conditional request

EA

Location A

EV

Location B

evtevt(vts)

cmd reply(vts)

vts = vector timestamp (a “condition”)

Page 120: Akka Persistence and Eventuate

Conditional request

EA

Location A

EV

Location B

evtevt(vts)

cmd CR(query, vts)reply(vts)

vts = vector timestamp (a “condition”)

Page 121: Akka Persistence and Eventuate

Conditional request

EA

Location A

EV

Location B

evtevt(vts)

cmd CR(query, vts)reply(vts)

vts = vector timestamp (a “condition”)

delay

Page 122: Akka Persistence and Eventuate

Conditional request

EA

Location A

EV

Location B

evtevt(vts)

cmd CR(query, vts)reply(vts)

vts = vector timestamp (a “condition”)

delay

Page 123: Akka Persistence and Eventuate

Conditional request

EA

Location A

EV

Location B

evtevt(vts)evt(vts)

cmd CR(query, vts)reply(vts)

vts = vector timestamp (a “condition”)

delay

Page 124: Akka Persistence and Eventuate

Conditional request

EA

Location A

EV

Location B

evtevt(vts)evt(vts)

cmd CR(query, vts)reply(vts)

vts = vector timestamp (a “condition”)

deliver

Page 125: Akka Persistence and Eventuate

Conditional request

EA

Location A

EV

Location B

evtevt(vts)evt(vts)

cmd CR(query, vts)reply(vts)

vts = vector timestamp (a “condition”)

reply

deliver

Page 126: Akka Persistence and Eventuate

Akka Distributed Data and Eventuate

CRDTs

Page 127: Akka Persistence and Eventuate

CRDTs

Conflict-free Replicated Data Types

Automated resolution of write conflicts

Page 128: Akka Persistence and Eventuate

CRDTs

Akka distributed data: CvRDTs (convergent or state-based)

Eventuate: CmRDTs(commutative or operation-based)

Page 129: Akka Persistence and Eventuate

CRDTs

CvRDTs CmRDTs

Message payload Current state Operations

Communication middleware No guarantees

Reliablebroadcast

(idempotent)

Change history CvRDT intern Persistent log

Page 130: Akka Persistence and Eventuate

Specification

“A comprehensive study of Convergent and Commutative Replicated Data Types”

http://hal.upmc.fr/docs/00/55/55/88/PDF/techreport.pdf

Page 131: Akka Persistence and Eventuate

Akka CvRDTs

10 implemented from specification(incl. counters, registers, sets, maps)

In-memory only (non-durable)

Custom data types can be defined

Page 132: Akka Persistence and Eventuate

Eventuate CmRDTs

4 implemented from specification(Counter, MV/LWW-register, OR-Set)

Durable via event sourcing

Eventuate CRDT framework

Page 133: Akka Persistence and Eventuate

Documentationhttp://rbmhtechnology.github.io/eventuate/

http://doc.akka.io/docs/akka/2.4.0/scala/persistence.html

http://doc.akka.io/docs/akka/2.4.0/scala/persistence-query.html

http://doc.akka.io/docs/akka/2.4.0/scala/distributed-data.html

Page 134: Akka Persistence and Eventuate

Thank you!