Share
Embed
akka-persistence
Introduction to the akka-persistence Open Kitchen hands-on workshop at Wehkamp
Arnout Engelen
April 16, 2015
Transcript
24 slides
-
AKKA-PERSISTENCE included since Akka 2.3.0 (march 2014, as 'experimental') changed
considerably in Akka 2.3.4 by Martin Krasser (of fame) @mrt1nz Eventsourced -
AKKA-PERSISTENCE Eventsourcing+Scala Mutable state, but immutable events Eventsourcing+Akka Actors handle
messages sequentially Clustering, continuous delivery -
AKKA-PERSISTENCE BASICS P e r s i s t e
n t A c t o r P e r s i s t e n t V i e w A t L e a s t O n c e D e l i v e r y -
c l a s s M y B u s
i n e s s P r o c e s s ( p r o c e s s I d : I d ) e x t e n d s P e r s i s t e n t A c t o r { . . . / / S t a b l e a c r o s s r e s t a r t s o v e r r i d e d e f p e r s i s t e n c e I d = s " m y P r o c e s s $ p r o c e s s I d " o v e r r i d e d e f r e c e i v e C o m m a n d = ? ? ? o v e r r i d e d e f r e c e i v e R e c o v e r = ? ? ? } -
c l a s s M y B u s
i n e s s P r o c e s s ( p r o c e s s I d : I d ) e x t e n d s P e r s i s t e n t A c t o r { . . . o v e r r i d e d e f r e c e i v e C o m m a n d = { c a s e R e q u e s t M o r t g a g e ( p e r s o n , a m o u n t ) = > p e r s i s t ( M o r t g a g e R e q u e s t e d ( p e r s o n , a m o u n t ) ) { e v e n t = > s e n d e r ( ) ! A c c e p t e d M o r t g a g e R e q u e s t } } o v e r r i d e d e f r e c e i v e R e c o v e r = ? ? ? } -
c l a s s M y B u s
i n e s s P r o c e s s ( p r o c e s s I d : I d ) e x t e n d s P e r s i s t e n t A c t o r { . . . o v e r r i d e d e f r e c e i v e C o m m a n d = { c a s e R e q u e s t M o r t g a g e ( p e r s o n , a m o u n t ) = > p e r s i s t ( M o r t g a g e R e q u e s t e d ( p e r s o n , a m o u n t ) ) { e v e n t = > s e n d e r ( ) ! A c c e p t e d M o r t g a g e R e q u e s t u p d a t e S t a t e ( e v e n t ) } } o v e r r i d e d e f r e c e i v e R e c o v e r = { c a s e e v e n t : E v e n t = > u p d a t e S t a t e ( e v e n t ) } d e f u p d a t e S t a t e : E v e n t = > U n i t = { c a s e M o r t g a g e R e q u e s t e d ( p e r s o n , a m o u n t ) = > / / S t a t e c h a n g e s , i n c l u d i n g ' b e c o m e ' / ' u n b e c o m e ' e t c } -
c l a s s M y B u s
i n e s s P r o c e s s ( p r o c e s s I d : I d ) e x t e n d s P e r s i s t e n t A c t o r { . . . o v e r r i d e d e f r e c e i v e C o m m a n d = { c a s e e v e n t : E v e n t = > p e r s i s t ( e v e n t ) { e v e n t = > s e n d e r ( ) ! A c k u p d a t e S t a t e ( e v e n t ) } } o v e r r i d e d e f r e c e i v e R e c o v e r = { c a s e e v e n t : E v e n t = > u p d a t e S t a t e ( e v e n t ) } d e f u p d a t e S t a t e : E v e n t = > U n i t = { c a s e M o r t g a g e R e q u e s t e d ( p e r s o n , a m o u n t ) = > / / S t a t e c h a n g e s , i n c l u d i n g ' b e c o m e ' / ' u n b e c o m e ' e t c } -
RECOVERY On actor (re)start Crash / system restart / upgrade
Moving to another cluster node r e c e i v e R e c o v e r receives: All persisted events R e c o v e r y C o m p l e t e d /R e c o v e r y F a i l u r e Available during replay r e c o v e r y R u n n i n g : B o o l e a n -
RECOVERY Advantages: No need to persist the 'current' state So
it can change: next version can keep more/less state in memory After fixes, crashing processes will automatically continue where they left off -
SNAPSHOTTING Optional, used when replaying all events becomes prohibitive E.g.
'all non-historic processes' -
PERSISTENT VIEWS c l a s s M y B
u s i n e s s P r o c e s s V i e w ( p r o c e s s I d : I d ) e x t e n d s P e r s i s t e n t V i e w { / / C o r r e s p o n d e s t o P e r s i s t e n t A c t o r t h a t w e ' r e v i e w i n g o v e r r i d e d e f p e r s i s t e n c e I d = s " m y P r o c e s s $ p r o c e s s I d " / / E a c h v i e w s h o u l d h a v e i t s o w n i d o v e r r i d e d e f v i e w I d = s " m y P r o c e s s v i e w $ p r o c e s s I d " d e f r e c e i v e = { c a s e e v e n t : E v e n t = > / / u p d a t e v i e w s t a t e c a s e S o m e Q u e r y ( . . . ) = > / / r e s p o n d t o q u e r y } } -
PERSISTENT VIEWS Updated asynchonously, at a configurable interval Or only
explicitly by sending an U p d a t e message Have their own snapshots (well, typically) -
STORAGE Pluggable, separate for the journal and for snapshots Cassandra
Kafka Serialization also pluggable Ties into standard Akka serialization infra Caution: default is Java serialization! No standard yet, e.g. protobuf, kryo, ... -
AT LEAST ONCE DELIVERY c l a s s M
y B u s i n e s s P r o c e s s ( p r o c e s s I d : I d ) e x t e n d s P e r s i s t e n t A c t o r w i t h A t L e a s t O n c e D e l i v e r y { . . . d e l i v e r ( b k r , d e l i v e r y I d = > C r e d i t C h e c k ( d e l i v e r y I d , p e r s o n ) ) . . . c o n f i r m D e l i v e r y ( d e l i v e r y I d ) . . . } -
AT LEAST ONCE DELIVERY unconfirmed messages can be included in
the Snapshot redelivery interval, maximum # of unconfirmed deliveries configurable warning event after 'n' unconfirmed attempts -
SIDE EFFECTS Approach #1: post-persist c l a s s
M y B u s i n e s s P r o c e s s ( p r o c e s s I d : I d ) e x t e n d s P e r s i s t e n t A c t o r { . . . o v e r r i d e d e f r e c e i v e C o m m a n d = { c a s e R e q u e s t M o r t g a g e ( p e r s o n , a m o u n t ) = > p e r s i s t ( M o r t g a g e R e q u e s t e d ( p e r s o n , a m o u n t ) ) { e v e n t = > s e n d e r ( ) ! A c c e p t e d M o r t g a g e R e q u e s t b k r ! C r e d i t C h e c k ( p e r s o n ) u p d a t e S t a t e ( e v e n t ) } } d e f u p d a t e S t a t e : E v e n t = > U n i t = { c a s e M o r t g a g e R e q u e s t e d ( p e r s o n , a m o u n t ) = > / / S t a t e c h a n g e s , i n c l u d i n g ' b e c o m e ' / ' u n b e c o m e ' e t c } } -
SIDE EFFECTS Approach #2: channels o v e r r
i d e d e f r e c e i v e C o m m a n d = { c a s e R e q u e s t M o r t g a g e ( p e r s o n , a m o u n t ) = > p e r s i s t ( M o r t g a g e R e q u e s t e d ( p e r s o n , a m s e n d e r ( ) ! A c c e p t e d M o r t g a g e R e q u e s t ; u p d a t e S t a t e ( e v e n t ) } } o v e r r i d e d e f r e c e i v e R e c o v e r = { c a s e R e c o v e r y C o m p l e t e d = > b k r C h a n n e l . e n a b l e ( ) c a s e e v e n t : E v e n t = > u p d a t e S t a t e ( e v e n t ) } o v e r r i d e d e f p r e S t a r t ( ) = { b k r C h a n n e l . d i s a b l e ( ) } d e f u p d a t e S t a t e : E v e n t = > U n i t = { c a s e M o r t g a g e R e q u e s t e d ( p e r s o n , a m o u n t ) = > / / S t a t e c h a n g e s , i n c l u d i n g ' b e c o m e ' / ' u n b e c o m e ' e t c b k r C h a n n e l . s e n d ( C r e d i t C h e c k ( p e r s o n ) ) } } -
SIDE EFFECTS Approach #3: AtLeastOnceDelivery o v e r r
i d e d e f r e c e i v e C o m m a n d = { c a s e R e q u e s t M o r t g a g e ( p e r s o n , a m o u n t ) = > p e r s i s t ( M o r t g a g e R e q u e s t e d ( p e r s o n , a m s e n d e r ( ) ! A c c e p t e d M o r t g a g e R e q u e s t u p d a t e S t a t e ( e v e n t ) } c a s e C r e d i t O k ( d e l i v e r y I d ) = > p e r s i s t ( C r e d i t O k ( d e l i v e r y I d ) ) ( u p d a t e S t a t e ) } d e f u p d a t e S t a t e : E v e n t = > U n i t = { c a s e M o r t g a g e R e q u e s t e d ( p e r s o n , a m o u n t ) = > / / S t a t e c h a n g e s , i n c l u d i n g ' b e c o m e ' / ' u n b e c o m e ' e t c d e l i v e r ( b k r , d e l i v e r y I d = > C r e d i t C h e c k ( d e l i v e r y I d , p e r s o n ) ) c a s e C r e d i t O k ( d e l i v e r y I d ) = > c o n f i r m D e l i v e r y ( d e l i v e r y I d ) } }