Functional CQRS Functional CQRS The double engine badass @Ramtop - - PowerPoint PPT Presentation

functional cqrs functional cqrs
SMART_READER_LITE
LIVE PREVIEW

Functional CQRS Functional CQRS The double engine badass @Ramtop - - PowerPoint PPT Presentation

Functional CQRS Functional CQRS The double engine badass @Ramtop @Ramtop Have you ever considered Event Sourcing architecture? @Ramtop @Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing


slide-1
SLIDE 1

@Ramtop @Ramtop

Functional CQRS Functional CQRS

The double engine badass

slide-2
SLIDE 2

@Ramtop @Ramtop

Have you ever

considered Event Sourcing architecture?

slide-3
SLIDE 3

@Ramtop @Ramtop

CQRS: When and Why

Very Good for

✔ Complex mutable state ✔ Separation of logic ✔ Auditing required ✔ Orders, Trades, Editors

slide-4
SLIDE 4

@Ramtop @Ramtop

CQRS: When and Why

Very Good for

✔ Complex mutable state ✔ Separation of logic ✔ Auditing required ✔ Orders, Trades, Editors

Not good for

✗ Read only aggregations (e.g. Reports) ✗ Stateless calculations (e.g. risk) ✗ Very High performance (e.g. exchange) ✗ Trivial CRUD (e.g. TODO lists)

slide-5
SLIDE 5

@Ramtop @Ramtop

Implementation Gradient

Command Pattern Event Source CQRS Monolith CQRS Microservices

slide-6
SLIDE 6

@Ramtop @Ramtop

Command pattern

✔ Allow replay to recreate State ✔ Separation of logic from infrastructure ✗ No automatic persistence ✗ Limited Scalability

slide-7
SLIDE 7

@Ramtop @Ramtop

Event Source

✔ Allow replay ✔ Separation of logic ✔ Automatic persistence ✗ Limited Scalability

slide-8
SLIDE 8

@Ramtop @Ramtop

Separation of Query Model

✔ Allow replay ✔ Separation of logic ✔ Automatic persistence ✔ High Scalability ✗ Complexity

slide-9
SLIDE 9

@Ramtop @Ramtop

Split to Microservices

slide-10
SLIDE 10

@Ramtop @Ramtop

Domain Driven Design

slide-11
SLIDE 11

@Ramtop @Ramtop

Domain Driven Design

DDD is a creative collaboration between technical and domain experts. Domain language must be reflected in the code. Code must let the business intention show up

slide-12
SLIDE 12

@Ramtop @Ramtop

Immut mutab abili lity Pur urity Hi Higher order fun unct ctions ns No exceptions Trans ansformat mations s whi which h prese serse se properties

Functional Programming

slide-13
SLIDE 13

@Ramtop @Ramtop

DDD D + Even ent t Sourci Sourcing + Fun uncti tion

  • nal

al St Style

There is no silver bullet but...

slide-14
SLIDE 14

@Ramtop @Ramtop

slide-15
SLIDE 15

@Ramtop @Ramtop

Protagonists

slide-16
SLIDE 16

@Ramtop @Ramtop

What is a ADT?

Represent entities that can change state in an immutable world.

An algebraic data type is a kind of composite type, i.e. a type formed by combining other types. Wikipedia

slide-17
SLIDE 17

@Ramtop @Ramtop

What is a Fold?

Specialized type of recursion

In functional programming, fold refers to a family of higher-

  • rder functions that through use of a given combining operation,

recombine the results of recursively processing its constituent parts, building up a return value. Wikipedia

slide-18
SLIDE 18

@Ramtop @Ramtop

Commands

A Command is a request for changing the internal state of the system A Command can “fail” if is not congruent with the current state of the System Each Command is executed in an atomic context

slide-19
SLIDE 19

@Ramtop @Ramtop

Events

Events are the “atoms” of System state change Nothing can change without an event, every event can change only one transactional aggregate State + Event => State

slide-20
SLIDE 20

@Ramtop @Ramtop

Values and Entities

They represent the state of the domain Values have no identity, Entities are distinct They are all immutables To keep state changes we have Algebraic Data Types

slide-21
SLIDE 21

@Ramtop @Ramtop

Transactional Aggregates

An aggregate is an aggregation of Entities They are composed by Entities and Values It has all the information for a transaction unit Aggregates is what Events fold to

slide-22
SLIDE 22

@Ramtop @Ramtop

Queries

A query ask for a snapshot of the state Queries typically need different data and denormalization from domain model. So we separate the models in CQRS. Queries are only eventually consistent with the domain

slide-23
SLIDE 23

@Ramtop @Ramtop

Actors

More powerful and easy to use concurrency model than Threads/Locks They communicate asynchronously and potentially remotely Useful for Bounded Contexts and Services Remote Actors works like Microservices

slide-24
SLIDE 24

@Ramtop @Ramtop

Event Store

Store tuples

(timestamp, type, uuid, event, version)

Query by index fast Dynamic query In memory implementation can be enough

slide-25
SLIDE 25

@Ramtop @Ramtop

github.com/uberto/anticapizzeria

slide-26
SLIDE 26

@Ramtop @Ramtop

Antica Pizzeria

  • Real application is about finance products booking

but a pizzeria has a surprising similar domain

  • We want to do implement the backend for a

ChatBot that will assist booking and enquires about orders.

slide-27
SLIDE 27

@Ramtop @Ramtop

Events Tree

slide-28
SLIDE 28

@Ramtop @Ramtop

Commands

slide-29
SLIDE 29

@Ramtop @Ramtop

Events

slide-30
SLIDE 30

@Ramtop @Ramtop

Queries

slide-31
SLIDE 31

@Ramtop @Ramtop

Queries listen to Events

(t (to k keep ep u up wi with th the chan the changes ges)

slide-32
SLIDE 32

@Ramtop @Ramtop

Let's fold Events

(t (to cr creat ate E Ent ntiti ties es)

slide-33
SLIDE 33

@Ramtop @Ramtop

Entities as EventComposable

slide-34
SLIDE 34

@Ramtop @Ramtop

Commands emit Events

slide-35
SLIDE 35

@Ramtop @Ramtop

Logic is called by Commands

slide-36
SLIDE 36

@Ramtop @Ramtop

Actors

(whe (where we e we can p can put al all thi this stu tuff)

slide-37
SLIDE 37

@Ramtop @Ramtop

Application

slide-38
SLIDE 38

@Ramtop @Ramtop

Tests can process C&Q

slide-39
SLIDE 39

@Ramtop @Ramtop

Add a new State to represent Dispatch when a pizza left the shop. Keep track of delivery person.

slide-40
SLIDE 40

@Ramtop @Ramtop

Live Code

slide-41
SLIDE 41

@Ramtop @Ramtop

Will you consider Event Sourcing in the future?

slide-42
SLIDE 42

@Ramtop @Ramtop

QA

Uberto Barbini @Ramtop github.com/uberto/anticapizzeria

slide-43
SLIDE 43

@Ramtop @Ramtop