@Ramtop @Ramtop
Functional CQRS Functional CQRS
The double engine badass
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
@Ramtop @Ramtop
The double engine badass
@Ramtop @Ramtop
Have you ever
@Ramtop @Ramtop
CQRS: When and Why
Very Good for
✔ Complex mutable state ✔ Separation of logic ✔ Auditing required ✔ Orders, Trades, Editors
@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)
@Ramtop @Ramtop
Implementation Gradient
Command Pattern Event Source CQRS Monolith CQRS Microservices
@Ramtop @Ramtop
Command pattern
✔ Allow replay to recreate State ✔ Separation of logic from infrastructure ✗ No automatic persistence ✗ Limited Scalability
@Ramtop @Ramtop
Event Source
✔ Allow replay ✔ Separation of logic ✔ Automatic persistence ✗ Limited Scalability
@Ramtop @Ramtop
Separation of Query Model
✔ Allow replay ✔ Separation of logic ✔ Automatic persistence ✔ High Scalability ✗ Complexity
@Ramtop @Ramtop
Split to Microservices
@Ramtop @Ramtop
Domain Driven Design
@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
@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
@Ramtop @Ramtop
DDD D + Even ent t Sourci Sourcing + Fun uncti tion
al St Style
There is no silver bullet but...
@Ramtop @Ramtop
@Ramtop @Ramtop
Protagonists
@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
@Ramtop @Ramtop
What is a Fold?
Specialized type of recursion
In functional programming, fold refers to a family of higher-
recombine the results of recursively processing its constituent parts, building up a return value. Wikipedia
@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
@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
@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
@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
@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
@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
@Ramtop @Ramtop
Event Store
Store tuples
(timestamp, type, uuid, event, version)
Query by index fast Dynamic query In memory implementation can be enough
@Ramtop @Ramtop
github.com/uberto/anticapizzeria
@Ramtop @Ramtop
Antica Pizzeria
but a pizzeria has a surprising similar domain
ChatBot that will assist booking and enquires about orders.
@Ramtop @Ramtop
Events Tree
@Ramtop @Ramtop
Commands
@Ramtop @Ramtop
Events
@Ramtop @Ramtop
Queries
@Ramtop @Ramtop
Queries listen to Events
(t (to k keep ep u up wi with th the chan the changes ges)
@Ramtop @Ramtop
Let's fold Events
(t (to cr creat ate E Ent ntiti ties es)
@Ramtop @Ramtop
Entities as EventComposable
@Ramtop @Ramtop
Commands emit Events
@Ramtop @Ramtop
Logic is called by Commands
@Ramtop @Ramtop
Actors
(whe (where we e we can p can put al all thi this stu tuff)
@Ramtop @Ramtop
Application
@Ramtop @Ramtop
Tests can process C&Q
@Ramtop @Ramtop
Add a new State to represent Dispatch when a pizza left the shop. Keep track of delivery person.
@Ramtop @Ramtop
@Ramtop @Ramtop
@Ramtop @Ramtop
Uberto Barbini @Ramtop github.com/uberto/anticapizzeria
@Ramtop @Ramtop