akka persistence and eventuate
TRANSCRIPT
Akka Persistence and EventuateA CQRS/ES tool comparison
Martin Krasser (@mrt1nz) Freelance software developer/architect
History
Eventsourced Akka Persistence
Eventuate
7.2012 9.2013 1.2015
Similarities
Basis technologies
Scala
Akka actors
Akka streams
(Pluggable) storage backends
Akka actor
Actor
State
Behavior
Mailboxtransient
non-durable
Akka Persistence and Eventuate
Persist actor state via event sourcing
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
Event sourcing
Commands - actions, can fail
Events - immutable facts
Event-sourced actor
Actor
State
Behavior
Mailbox
Command handler
Event handler
Command processingEvent-sourced actor
State
Command handler
Event handler
Event log
Command processingEvent-sourced actor
State
Command handler
Event handler
Command
Event log
Command processingEvent-sourced actor
State
Command handler
Event handler
Command
Event log
Events
Command processingEvent-sourced actor
State
Command handler
Event handler
Command
Event log
Events
Events
Command processingEvent-sourced actor
State
Command handler
Event handler
Command
Event log
Events
Events
Command processingEvent-sourced actor
State
Command handler
Event handler
Command
Reply
Event log
Events
Events
State recoveryEvent-sourced actor
State
Command handler
Event handler
Event log
State recoveryEvent-sourced actor
State
Command handler
Event handler
Event log
Events (replay)
State recoveryEvent-sourced actor
State
Command handler
Event handler
Event log
Events (replay)
Akka Persistence and Eventuate
Separate command processing from query processing
CQRS
Command QueryResponsibility Segregation
Different data models for command processing and query processing
CQRS
Write model for command processing
Read model for query processing
CQRS
Can be well combined with event sourcing (CQRS/ES)
CQRS/ES
Write model
Event log
Command Query
Events (r/w) Events (r)
Command side Query side
Read model
Abstractions
Command side Query side
AkkaPersistence
PersistentActor PersistentView Source[E,M]
Eventuate EventsourcedActorEventsourcedView
EventsourcedProcessorSource[E,M]
*) work in progress
PersistentActor EventsourcedActor
PersistentActor EventsourcedActor
State
PersistentActor EventsourcedActor
Command handler
PersistentActor EventsourcedActor
Event handler
PersistentActor EventsourcedActor
Snapshot handler
DifferencesCommand side
At a glance
https://twitter.com/mrt1nz/status/573382831889653760
Consistency
Akka Persistence and Eventuate support strong consistency
Eventuate additionally supports relaxation to causal consistency
Akka Persistence
Enforces strong consistency on command side
PersistentActors must be global singletons
No actor/state replication
Eventuate
EventsourcedActors can be replicated
State replication via reliable, asynchronous event replication
Replication across locations (availability zones)
Replication
Location ALocation B
Location C
Replication
Location A
Application state
Location B
Location C
Replication
Events
Events
Location A
Application state
Location B
Location C
Replication
Events
Events
Location A
Application state
Location B
Location C
Replication
Command
Location ALocation B
Location C
Replication
Command XLocation A
Location B
Location C
Replication
Command X
Events
Events
Location ALocation B
Location C
Replication
Command X
X
X
Events
Events
Location ALocation B
Location C
Eventuate
Replicas writeable at all locations = multi-master
Write-availability during network partitions
EventuateWrite conflicts
detection
tracking
automated resolution
interactive resolution
Partition
Location ALocation B
Location C
Partition
Command
Location ALocation B
Location C
Partition
Command XLocation A
Location B
Location C
Partition
Command XEvents
Location ALocation B
Location C
Partition
Command X XEvents
Location ALocation B
Location C
Partition
Command
Command
X XEvents
Location ALocation B
Location C
Partition
Command
Command
X X
Y
Events
Location ALocation B
Location C
Partition
Command
Command
X X
Y
Events
Location ALocation B
Location C
Partition
Command
Command
X X
Y
EventsEvents
Events
Location ALocation B
Location C
Conflict
XY XY
XY
Location ALocation B
Location C
Resolution
XY XY
XY
Interactive resolution
XY => Z
Location ALocation B
Location C
Resolution
XY
XY
Interactive resolution
XY => ZZ
Location ALocation B
Location C
Resolution
XY
XY
Interactive resolution
XY => ZZ
Events
Events
Location ALocation B
Location C
Resolution
Interactive resolution
XY => ZZ Z
Z
Events
Events
Location ALocation B
Location C
Eventuate
Strong consistency within location*)
Causal consistency across locations
Causality is tracked with vector clocks
*) relaxation to causal consistency also possible within location
Causal consistency example
Distributed chat application
FIFO reliable broadcast vs.
Causal reliable broadcast
Inspired by ACM article “Don’t Settle for Eventual Consistency”
http://queue.acm.org/detail.cfm?id=2610533
Causal consistency example
FIFO rel. broadcastDC1
DC2
DC3
FIFO rel. broadcast
- Alice: Lost my wedding ring
Alice
DC1DC2
DC3
FIFO rel. broadcast
- Alice: Lost my wedding ring- Alice: Lost my wedding ring
- Alice: Lost my wedding ring
Alice
DC1DC2
DC3
- 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
- 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
- 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
- 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
- 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
- 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
- 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!
- 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!
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
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
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
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
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
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
- 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
- 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
- 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
- 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
Causal consistency
Causally related events are delivered in same order at all locations
Concurrent events can be delivered in any order at different locations
DifferencesEvent log
“Local” event log
Eventuate
- LevelDB - Cassandra
Location ALocation B
Location C
“Local” event log
“Replicated” event log
Eventuate
- LevelDB - Cassandra
Location ALocation B
Location C
- LevelDB - Cassandra
EventsEvents
Events
Local event log
Storage order consistent withcausal order
Given the potential causality relation ➞ of events
Local event logs are linear extensions of ➞
Replicated event log
Same causal event storage order at different locations
Different total event storage order at different locations
Akka Persistence- LevelDB - Cassandra - EventStore - MongoDB - Kafka - … Location A
Location B
Location C
DifferencesEvent collaboration
Akka Persistence
PA
PA
PAPA
Event log:
private for writing
shared for reading (query side)
Eventuate
EA
EAEA
Event log:
shared for writing
shared for reading
EA
Event routing
EA
EA
EA
Event routing
EA
EA
EAe1
Event routing
EA
EA
EAe1
e1
Event routing
EA
EA
EAe1
e1
All
Topic
Aggregate id
…
Event routing
EA
EA
EAe1
e1
EAEA
e1
EA
e1
Location A
Location B
Location C
Event collaboration
State replication
Service interaction
Event collaboration
S2
S1
S1
S3
Location A
Location B
Location C
Event collaboration
S2
S1e1
S1
S3
Location A
Location B
Location C
Event collaboration
S2
S1e1
S1
e1
S3
Location A
Location B
Location C
Event collaboration
S2
S1e1
e1
S1
e1
S3
e1
Location A
Location B
Location C
Event collaboration
S2
S1e1
e1
S1
e1
S3
e1
Location A
Location B
Location Ce2
Event collaboration
S2
S1e1
e1
S1
e1
S3
e1
Location A
Location B
Location Ce2
e2
Event collaboration
Reliable, distributed and partition-tolerant business processes
Event-driven (micro-)service architectures
Event collaboration
Reliable event delivery
Causal event delivery
De-duplicated event stream
DifferencesQuery side
Akka Persistence 2.3PA
PA
PV
PV
PV
PersistentViews on single PersistentActor (default)
Akka Persistence 2.4PA
PA
S
S
S
Stream Sources for multiple PersistentActors(requires storage plugin support)
Eventuate
EV
EV
EventsourcedViews on multiple EventsourcedActors(default, shared local log)
EA
EA
EA
Eventuate
EV
EV
EventsourcedViews on multiple EventsourcedActors(default, shared replicated log)
EA
EA
EA
EA
EAEA
Location A
Location B
Location C
Consistency in CQRS
Eventual consistency
Causal consistency
AkkaPersistence
usually per PA(plugin-specific)
Eventuate
Causal consistency
Single EA/EV: default
Across EA/EV: conditional requests
Example scenario
Update a write model (EA)
Query a read model (EV)
Query result should include effect that was caused by the update
Conditional request
EA
Location A
EV
Location B
Conditional request
EA
Location A
EV
Location B
cmd
Conditional request
EA
Location A
EV
Location B
evt
cmd
Conditional request
EA
Location A
EV
Location B
evtevt(vts)
cmd
vts = vector timestamp (a “condition”)
Conditional request
EA
Location A
EV
Location B
evtevt(vts)
cmd reply(vts)
vts = vector timestamp (a “condition”)
Conditional request
EA
Location A
EV
Location B
evtevt(vts)
cmd CR(query, vts)reply(vts)
vts = vector timestamp (a “condition”)
Conditional request
EA
Location A
EV
Location B
evtevt(vts)
cmd CR(query, vts)reply(vts)
vts = vector timestamp (a “condition”)
delay
Conditional request
EA
Location A
EV
Location B
evtevt(vts)
cmd CR(query, vts)reply(vts)
vts = vector timestamp (a “condition”)
delay
Conditional request
EA
Location A
EV
Location B
evtevt(vts)evt(vts)
cmd CR(query, vts)reply(vts)
vts = vector timestamp (a “condition”)
delay
Conditional request
EA
Location A
EV
Location B
evtevt(vts)evt(vts)
cmd CR(query, vts)reply(vts)
vts = vector timestamp (a “condition”)
deliver
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
Akka Distributed Data and Eventuate
CRDTs
CRDTs
Conflict-free Replicated Data Types
Automated resolution of write conflicts
CRDTs
Akka distributed data: CvRDTs (convergent or state-based)
Eventuate: CmRDTs(commutative or operation-based)
CRDTs
CvRDTs CmRDTs
Message payload Current state Operations
Communication middleware No guarantees
Reliablebroadcast
(idempotent)
Change history CvRDT intern Persistent log
Specification
“A comprehensive study of Convergent and Commutative Replicated Data Types”
http://hal.upmc.fr/docs/00/55/55/88/PDF/techreport.pdf
Akka CvRDTs
10 implemented from specification(incl. counters, registers, sets, maps)
In-memory only (non-durable)
Custom data types can be defined
Eventuate CmRDTs
4 implemented from specification(Counter, MV/LWW-register, OR-Set)
Durable via event sourcing
Eventuate CRDT framework
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
Thank you!