![Page 1: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/1.jpg)
@Ramtop
Functional CQRSFunctional CQRS
The double engine badass
![Page 2: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/2.jpg)
@Ramtop
Have you ever
considered Event Sourcing
architecture?
![Page 3: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/3.jpg)
@Ramtop
CQRS: When and Why
Very Good for✔ Complex mutable state✔ Separation of logic✔ Auditing required✔ Orders, Trades, Editors
![Page 4: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/4.jpg)
@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)
![Page 5: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/5.jpg)
@Ramtop
Implementation Gradient
Command Pattern
Event Source
CQRS Monolith
CQRS Microservices
![Page 6: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/6.jpg)
@Ramtop
Command pattern
✔ Allow replay to recreate State✔ Separation of logic from infrastructure✗ No automatic persistence✗ Limited Scalability
![Page 7: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/7.jpg)
@Ramtop
Event Source
✔ Allow replay✔ Separation of logic✔ Automatic persistence✗ Limited Scalability
![Page 8: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/8.jpg)
@Ramtop
Separation of Query Model
✔ Allow replay✔ Separation of logic✔ Automatic persistence✔ High Scalability✗ Complexity
![Page 9: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/9.jpg)
@Ramtop
Split to Microservices
![Page 10: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/10.jpg)
@Ramtop
Domain Driven Design
![Page 11: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/11.jpg)
@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
![Page 12: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/12.jpg)
@Ramtop
Immutability
Purity
Higher order functions
No exceptions
Transformations which preserse properties
Functional Programming
![Page 13: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/13.jpg)
@Ramtop
DDD + Event Sourcing + Functional Style
There is no silver bullet but...
![Page 14: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/14.jpg)
@Ramtop
![Page 15: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/15.jpg)
@Ramtop
Protagonists
![Page 16: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/16.jpg)
@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
![Page 17: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/17.jpg)
@Ramtop
What is a Fold?
Specialized type of recursionIn functional programming, fold refers to a family of higher-
order functions that through use of a given combining operation, recombine the results of recursively processing its constituent
parts, building up a return value. Wikipedia
![Page 18: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/18.jpg)
@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
![Page 19: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/19.jpg)
@Ramtop
Events
Events are the “atoms” of System state change
Nothing can change without an event, every event can change only one transactional aggregateState + Event => State
![Page 20: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/20.jpg)
@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
![Page 21: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/21.jpg)
@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
![Page 22: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/22.jpg)
@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
![Page 23: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/23.jpg)
@Ramtop
Actors
More powerful and easy to use concurrency model than Threads/Locks
They communicate asynchronously and potentially remotely
Useful for Bounded Contexts and ServicesRemote Actors works like Microservices
![Page 24: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/24.jpg)
@Ramtop
Event Store
Store tuples (timestamp, type, uuid, event, version)
Query by index fast
Dynamic query
In memory implementation can be enough
![Page 25: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/25.jpg)
@Ramtopgithub.com/uberto/anticapizzeria
![Page 26: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/26.jpg)
@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.
![Page 27: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/27.jpg)
@Ramtop
Events Tree
![Page 28: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/28.jpg)
@Ramtop
Commands
![Page 29: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/29.jpg)
@Ramtop
Events
![Page 30: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/30.jpg)
@Ramtop
Queries
![Page 31: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/31.jpg)
@Ramtop
Queries listen to Events(to keep up with the changes)
![Page 32: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/32.jpg)
@Ramtop
Let's fold Events(to create Entities)
![Page 33: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/33.jpg)
@Ramtop
Entities as EventComposable
![Page 34: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/34.jpg)
@Ramtop
Commands emit Events
![Page 35: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/35.jpg)
@Ramtop
Logic is called by Commands
![Page 36: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/36.jpg)
@Ramtop
Actors(where we can put all this stuff)
![Page 37: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/37.jpg)
@Ramtop
Application
![Page 38: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/38.jpg)
@Ramtop
Tests can process C&Q
![Page 39: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/39.jpg)
@Ramtop
Add a new State to represent Dispatchwhen a pizza left theshop.Keep track of delivery person.
![Page 40: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/40.jpg)
@Ramtop
Live Code
![Page 41: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/41.jpg)
@Ramtop
Will you consider Event Sourcing in
the future?
![Page 42: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/42.jpg)
@Ramtop
QAUberto Barbini
@Ramtop
github.com/uberto/anticapizzeria
![Page 43: Functional CQRS - JetBrains@Ramtop CQRS: When and Why Very Good for Complex mutable state Separation of logic Auditing required Orders, Trades, Editors @Ramtop Domain Driven Design](https://reader035.vdocuments.mx/reader035/viewer/2022062923/5f0a94a07e708231d42c554e/html5/thumbnails/43.jpg)
@Ramtop