Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017
-
Upload
lutz-huehnken -
Category
Software
-
view
515 -
download
0
Transcript of Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017
EVENT SOURCING AND CQRS IN SCALA
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
RDBMS
A MODEL FOR A LEAGUE TABLE
▸ CREATE TABLE BUNDESLIGA (TEAM, GAMES_PLAYED, POINTS)
▸ UPDATE TABLE BUNDESLIGA SET POINTS = POINTS + 3 WHERE TEAM = SVW
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
RDBMS CHALLENGES
IMPEDANCE MISMATCH
▸ Conceptual differences between the application model (determined by implementation language / paradigm) and relational algebra.
▸ ORMs are leaky abstractions
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
RDBMS CHALLENGES
SCALING
▸ Reads can be scaled out through replicas
▸ But writes need coordination (e.g. Master)
▸ Sharding is difficult
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
RDBMS CHALLENGES
KEEPING HISTORY
▸ SQL employs destructive operations (DELETE, UPDATE)
▸ Often business requires some log of changes
▸ Add deletion markers, lifecycle columns, audit tables, Envers..
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
EVENT SOURCING
RETHINK
▸ Forget the database
▸ Forget the data model
▸ Unlearn RDBMS
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
EVENT SOURCING
EVENTS
▸ Don’t start with the static structure (entities, tables), start with the dynamic (domain events)
▸ Boundaries should emerge from flows
▸ Tip for further exploration: Event Storming
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
EVENT SOURCING
STATE IS THE RESULT OF EVENTS
▸ An event is an observable, undisputed fact
▸ Just like in the real world: Events occur, irrevocably
▸ The „state“ of the world is the consequence of events
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
LAGOM PERSISTENCE
IMPLEMENTATION
▸ We send messages (commands) to A (which is, depending on vocabulary, Actor, Aggregate, PersistentEntity)
▸ A accomplishes events and changes its state (becomes A’)
▸ In Event Sourcing, we persist these events in a journal
Event
Event
Event
Command A A'
SHOW ME THE CODE HTTPS://GITHUB.COM/REACTIVESYSTEMS-EU/EVENTSOURCING-INTRO/
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
LAGOM PERSISTENCE
CLUSTER SHARDING
▸ Instead of actorOf, get ActorRef by key from ShardRegion
▸ Will place actor in shard
▸ Actors are unique
▸ Shards can be dynamically redistributed
Node A Node B
Node C
XB
A
Z
XY
NM
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
LAGOM PERSISTENCE
PASSIVATION, SNAPHOTS
▸ Lagom/Akka ClusterSharding also manages lifecycle: activation, passivation
▸ In combination with Lagom/Akka Persistence, events will be replayed on activation
▸ Snapshots are stored to speed up restoration
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
CHALLENGES REVISITED
IMPEDANCE MISMATCH
▸ Commands are actor messages
▸ Events are events
▸ Aggregates are actors
▸ Good alignment, no mismatch
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
CHALLENGES REVISITED
SCALING OUT
▸ Add and remove cluster nodes as you wish, any time
▸ No master, coordinator, SPOF
▸ Highly elastic
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
CHALLENGES REVISITED
KEEPING HISTORY
▸ All events are stored and can be kept until the end of time
▸ Immutable events, append-only journal
▸ No destructive operations
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
CQRS
BUT HOW DO WE READ?
▸ Separate writing and reading completely
▸ Command Query Responsibility Segregation (CQRS)
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
CQRS
BUILDING READ SIDES
▸ Subscribe to events
▸ Convert events to statements for read side database
▸ Lagom provides ReadSideProcessor
SHOW ME THE CODE HTTPS://GITHUB.COM/REACTIVESYSTEMS-EU/EVENTSOURCING-INTRO/
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
FRAMEWORK ALTERNATIVES
AKKA PERSISTENCE
▸ Is used by Lagom as the underlying engine
▸ Less type checking
▸ receive / receiveRecover instead of Command/EventHandlers
▸ Need to write ReadSideProcessor yourself (e.g. using Persistence Query)
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
FRAMEWORK ALTERNATIVES
FUN.CQRS
▸ Abstraction Layer (a bit like Lagom, but predates it) on top of different backends (including Akka)
▸ Scala only
▸ Focusses on functional programming paradigm and type safety
▸ http://www.funcqrs.io
▸ Room A2+A3, next slot
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
FRAMEWORK ALTERNATIVES
EVENTUATE
▸ Not based on Akka Persistence, competing model
▸ Same capabilities in terms of ES, different approach
▸ Allows actor state replication across nodes
▸ https://rbmhtechnology.github.io/eventuate/
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
EVENT SOURCING
THINGS TO THINK ABOUT
▸ Backwards Compatibility
▸ Delayed Consistency
▸ Idempotency / Failure Scenarios
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
EVENT SOURCING
SUMMARY
▸ Unlearn, rethink
▸ ES is well aligned with Actor Model
▸ Scalability, Information Preservation, Flexibility
▸ Lagom is good „starter pack“
ASK ME ANYTHING
ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken
ADDENDUM
IMAGE CREDITS
Oracle
https://www.flickr.com/photos/oracleopenworld09/4013685670usage granted under https://creativecommons.org/licenses/by/2.0/
Unlearn and Rethink
https://www.flickr.com/photos/jonasb/447678323 usage granted under https://creativecommons.org/licenses/by/2.0/
Event Storming
https://commons.wikimedia.org/wiki/File:Event_Storming_example_process.jpgusage granted under https://creativecommons.org/licenses/by-sa/4.0/deed.en
Stack of Books
https://clipartfest.com/categories/view/8ffb240e0b9baa070017fcd5d619892a278cbc6c/pictures-of-stack-of-books.html
Wake up
https://www.flickr.com/photos/126337928@N05/26566890932https://creativecommons.org/licenses/by/2.0/
Stream
By Eleassar - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=21827540