Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

26
EVENT SOURCING AND CQRS IN SCALA

Transcript of Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

Page 1: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

EVENT SOURCING AND CQRS IN SCALA

Page 2: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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

Page 3: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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

Page 4: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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

Page 5: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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..

Page 6: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017
Page 7: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken

EVENT SOURCING

RETHINK

▸ Forget the database

▸ Forget the data model

▸ Unlearn RDBMS

Page 8: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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

Page 9: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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

Page 10: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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'

Page 11: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

SHOW ME THE CODE HTTPS://GITHUB.COM/REACTIVESYSTEMS-EU/EVENTSOURCING-INTRO/

Page 12: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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

Page 13: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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

Page 14: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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

Page 15: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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

Page 16: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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

Page 17: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken

CQRS

BUT HOW DO WE READ?

▸ Separate writing and reading completely

▸ Command Query Responsibility Segregation (CQRS)

Page 18: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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

Page 19: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

SHOW ME THE CODE HTTPS://GITHUB.COM/REACTIVESYSTEMS-EU/EVENTSOURCING-INTRO/

Page 20: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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)

Page 21: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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

Page 22: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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/

Page 23: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

ScalaDays Copenhagen 2017 http://reactivesystems.eu @lutzhuehnken

EVENT SOURCING

THINGS TO THINK ABOUT

▸ Backwards Compatibility

▸ Delayed Consistency

▸ Idempotency / Failure Scenarios

Page 24: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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“

Page 25: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

ASK ME ANYTHING

Page 26: Introduction to Event Sourcing and CQRS in Scala - ScalaDays Copenhagen 2.6.2017

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