Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
CQRS 2.0 David Hoerster
ABOUT ME
5-Time .NET (Visual C#) MVP (April 2011)
Sr. Solutions Architect at Confluence
One of the organizers for Pittsbur...
GOALS
What are the principles behind CQRS
How can I apply them in my current development
How Akka(.NET) shares much in com...
PREFACE:
FIRST CONTACT
SOME CQRS BACKGROUND
CQRS BACKGROUND
Command Query Responsibility Segregation
 Coined by Greg Young
Heavily influenced by Domain Driven Design...
WHAT IS CQRS?
Simply: separate paths for reads and writes
Communicate via messages
Commands and Events
 “CreateOrder” and...
CQRS VISUALIZED: BASIC
Client
Backend
Commands Queries
CQRS
VISUALIZED
Controller
Handler
Domain
Persistence
/ Read
Model
Read
Layer
Command
Side
Query
Side
CQRS EXTENDED
Can move from direct references to queued
Decouples handlers
Increased isolation of areas
Leads to single me...
CQRS
VISUALIZED
Controller
Handler
Domain
Persistence
Read
Layer
Handler
Command/Event
Topics
Read Model
Command
Side
Quer...
CQRS HANDLER
CQRS HANDLER
BENEFITS
Message Based
Segregation of Responsibilities – Separate Paths
Smaller, simpler classes
 Albeit more classes
Foc...
CQRS PRINCIPLES
Separate Paths for Command and Queries
Message-Based
Async Friendly
Thin read layer
Don’t fear lots of sma...
A CALL
Is CQRS Dead?
<rant>
"CQRS IS HARD"
Misunderstandings
 Has to be async
 Has to have queueing
 Need a separate read model
 Need to have Even...
HAS CQRS “FAILED”?
Focus on CQRS as architecture over
principles
Search for prescriptive guidance
 Infrastructure/Impleme...
</rant>
POPULAR ARCHITECTURE TOPICS
Microservices
Event driven
Distributed computing
NoSQL
Async
BUT ISN’T CQRS…?
Message Driven
Responsive Read Model
Isolated Handlers
Async Friendly
Group Handlers into Clusterable Gro...
CQRS CAN HELP DRIVE REACTIVE
APPS
CQRS EVOLVED
CQRS & THE REACTIVE MANIFESTO
Message Based
 Core of CQRS
Responsive
 Commands are ack/nack
 Queries are (can be) again...
SIDE NOTE: REACTIVE VS.
PROACTIVE
Isn’t proactive a good thing?
A system taking upon itself to
check state is proactive
 ...
SIDE NOTE: REACTIVE VS.
PROACTIVE
Isn’t proactive a good thing?
A system taking upon itself to
check state is proactive
 ...
CQRS + REACTIVE
Resillient and Elastic core to
Reactive
CQRS’ core is Message-Based
and Responsive
Combining the two is po...
ACTOR MODEL
CQRS lacks prescriptive guidance
Actor Model provides a message-based pattern
Provides prescriptive guidance
M...
ACTOR MODEL
Actor Model is a system made of small units of concurrent
computation
 Formulated in the early 70’s
Each unit...
WHAT IS AN ACTOR?
Lightweight class that encapsulates state and behavior
 State can be persisted (Akka.Persistence)
 Beh...
ACTOR HIERARCHY
baseball
gameCoo
rdinator
gameInfo
-x
gameInfo
-y
playerSup
ervisor
batter-abatter-bbatter-c
c-01 c-11 c-32
AKKA: RESILIENCY
Actors supervise their children
When they misbehave, they are notified
Can punish (fail) all children, or...
AKKA.NET
Akka.NET is an open source framework
Actor Model for .NET
Port of Akka (for JVM/Scala)
http://getakka.net
NuGet –...
STARTING WITH AKKA.NET
Akka.NET is a hierarchical system
Root of the system is ActorSystem
Expensive to instantiate – crea...
CREATING AN ACTOR
An Actor has a unique address
Can be accessed/communicated to like a URI
Call ActorOf off ActorSystem, p...
SENDING A MESSAGE
Messages are basis of actor communication
 Should be IMMUTABLE!!!
“Tell” an actor a message
Async call
...
RECEIVING A MESSAGE
Create your Actor
Derive from ReceiveActor
In constructor, register your
message subscriptions
Looks s...
RECEIVING A MESSAGE
Each actor has a mailbox
Messages are received in the actor’s mailbox (queue)
Actor is notified that t...
DEMO: HELLO AKKA.NET
ELEMENTS OF CQRS IN ACTOR
MODEL
Message-based
Potentially async
Focused code
Actor System is your Command Side
CQRS + ACTORS
Combining the principles of CQRS and the patterns of the Actor
Model
Message-based communication with concur...
CQRS + ACTORS: MESSAGES
Messages in an Actor System include both Commands and Events
Still encapsulate intent
Still should...
CQRS + ACTORS: MESSAGES
CQRS + ACTORS: HANDLERS
Handlers begin to encapsulate your
Actor System
Generally a single Actor System per
handler
Have s...
CQRS + ACTORS: HANDLERS
CQRS + ACTORS: HANDLERS
CQRS + ACTORS: HANDLERS
Handlers act as the entry point
 Web API
 Service subscribed to queue
CQRS  IHandle<T> for thos...
CQRS + ACTORS: SERVICES
Word Counter Handler
super
Count Write Stats
A B G H Y Z
DEMO: HELLO CQRS +
AKKA.NET
Word Counter
Handler
CQRS + ACTORS: SERVICES
super
Count
A B
Word Writer
Handler
super
Write
G H
Word Stats
Handler
super
...
CQRS + ACTORS + SERVICES
Service A Service B Service C
Message Bus
DW / DL /
Read Model
Cmds / Events Cmds / Events
API Ga...
Service A
Service A
Service A
CQRS + ACTORS + SERVICES +
CLUSTER (AKKA.CLUSTER)
Service A Service B Service C
Message Bus
...
CQRS + ACTORS + SERVICES
Actors act as the workers for your handlers
Services encapsulate handler into a bounded context
C...
CONCLUSIONS
CQRS is not dead
Key principles of messaging, separate paths and responsibility
segregation
Apply CQRS to Reac...
CQRS Evolved - CQRS + Akka.NET
Upcoming SlideShare
Loading in …5
×

CQRS Evolved - CQRS + Akka.NET

5,293 views

Published on

CQRS (Command Query Responsibility Segregation) was all the hype in .NET architecture circles a few years back. But has it faded away? Is it old news? I argue that it hasn't, and the concepts of CQRS are alive and well and probably more widely accepted and considered today than a few years ago. From event-driven systems to the Reactive Manifesto, the principles of CQRS are with us and impacting many different tools. In this session, we'll explore those CQRS principles and how they have manifested themselves in the architectures of choice today. You'll come away with a greater appreciation of CQRS and ideas on how to incorporate those principles in your applications today.

Published in: Software
  • Be the first to comment

CQRS Evolved - CQRS + Akka.NET

  1. 1. CQRS 2.0 David Hoerster
  2. 2. ABOUT ME 5-Time .NET (Visual C#) MVP (April 2011) Sr. Solutions Architect at Confluence One of the organizers for Pittsburgh TechFest (http://pghtechfest.com) Organizer of Pittsburgh Reactive Dev Group (http://meetup.com/reactive) Past President of Pittsburgh .NET Users Group Twitter - @DavidHoerster Blog – http://blog.agileways.com Email – david@agileways.com
  3. 3. GOALS What are the principles behind CQRS How can I apply them in my current development How Akka(.NET) shares much in common wth CQRS How CQRS + Akka(.NET) can help with building distributed, reactive applications
  4. 4. PREFACE: FIRST CONTACT
  5. 5. SOME CQRS BACKGROUND
  6. 6. CQRS BACKGROUND Command Query Responsibility Segregation  Coined by Greg Young Heavily influenced by Domain Driven Design (DDD) Evolution of Meyer’s CQS (Command Query Separation) Separation of writes (command) and reads (query) CQS is more at the unit level CQRS is at a higher level  bounded context / service
  7. 7. WHAT IS CQRS? Simply: separate paths for reads and writes Communicate via messages Commands and Events  “CreateOrder” and “OrderCreated” Reads are against a thin read model (preferably optimized)
  8. 8. CQRS VISUALIZED: BASIC Client Backend Commands Queries
  9. 9. CQRS VISUALIZED Controller Handler Domain Persistence / Read Model Read Layer Command Side Query Side
  10. 10. CQRS EXTENDED Can move from direct references to queued Decouples handlers Increased isolation of areas Leads to single message causing several changes  Raising of events and subscriptions to events
  11. 11. CQRS VISUALIZED Controller Handler Domain Persistence Read Layer Handler Command/Event Topics Read Model Command Side Query Side
  12. 12. CQRS HANDLER
  13. 13. CQRS HANDLER
  14. 14. BENEFITS Message Based Segregation of Responsibilities – Separate Paths Smaller, simpler classes  Albeit more classes Focused Approach to Domain  Move away from anemic domain models
  15. 15. CQRS PRINCIPLES Separate Paths for Command and Queries Message-Based Async Friendly Thin read layer Don’t fear lots of small classes
  16. 16. A CALL
  17. 17. Is CQRS Dead?
  18. 18. <rant>
  19. 19. "CQRS IS HARD" Misunderstandings  Has to be async  Has to have queueing  Need a separate read model  Need to have Event Sourcing Perceived as overly prescriptive, but little prescription Confusing  Command Handler vs. Event Handler vs. Domain  Command vs. Event
  20. 20. HAS CQRS “FAILED”? Focus on CQRS as architecture over principles Search for prescriptive guidance  Infrastructure/Implementation debates  Should domains have getters??  “…there are times a getter can be pragmatic. The trick is learning where to use them.” – Greg Young (DDD/CQRS Group) “This list has its heads up its own [butts] about infrastructure. What business problems are you working on?” -- Greg Young (DDD/CQRS Group)
  21. 21. </rant>
  22. 22. POPULAR ARCHITECTURE TOPICS Microservices Event driven Distributed computing NoSQL Async
  23. 23. BUT ISN’T CQRS…? Message Driven Responsive Read Model Isolated Handlers Async Friendly Group Handlers into Clusterable Groups NoSQL Friendly (Flattened Read Models)
  24. 24. CQRS CAN HELP DRIVE REACTIVE APPS
  25. 25. CQRS EVOLVED
  26. 26. CQRS & THE REACTIVE MANIFESTO Message Based  Core of CQRS Responsive  Commands are ack/nack  Queries are (can be) against an optimized read model Resillient  Not core to CQRS  Implementation Elastic  Not core to CQRS  Implementation
  27. 27. SIDE NOTE: REACTIVE VS. PROACTIVE Isn’t proactive a good thing? A system taking upon itself to check state is proactive  It’s doing too much A reactive system reacts to events that occur  It’s doing just the right amount
  28. 28. SIDE NOTE: REACTIVE VS. PROACTIVE Isn’t proactive a good thing? A system taking upon itself to check state is proactive  It’s doing too much A reactive system reacts to events that occur  It’s doing just the right amount System EvtEvt System EvtEvt Proactive Reactive
  29. 29. CQRS + REACTIVE Resillient and Elastic core to Reactive CQRS’ core is Message-Based and Responsive Combining the two is powerful
  30. 30. ACTOR MODEL CQRS lacks prescriptive guidance Actor Model provides a message-based pattern Provides prescriptive guidance Makes for more generalized implementation Actor Model is driving more reactive-based development
  31. 31. ACTOR MODEL Actor Model is a system made of small units of concurrent computation  Formulated in the early 70’s Each unit is an actor Communicates to each other via messages Actors can have children, which they can supervise
  32. 32. WHAT IS AN ACTOR? Lightweight class that encapsulates state and behavior  State can be persisted (Akka.Persistence)  Behavior can be switched (Become/Unbecome) Has a mailbox to receive messages  Ordered delivery by default  Queue Can create child actors Has a supervisory strategy for child actors  How to handle misbehaving children Are always thread safe Have a lifecycle  Started, stopped, restarted
  33. 33. ACTOR HIERARCHY baseball gameCoo rdinator gameInfo -x gameInfo -y playerSup ervisor batter-abatter-bbatter-c c-01 c-11 c-32
  34. 34. AKKA: RESILIENCY Actors supervise their children When they misbehave, they are notified Can punish (fail) all children, or tell the misbehaving on to try again Protects rest of the system
  35. 35. AKKA.NET Akka.NET is an open source framework Actor Model for .NET Port of Akka (for JVM/Scala) http://getakka.net NuGet – searching for “akka.net”, shows up low in the list
  36. 36. STARTING WITH AKKA.NET Akka.NET is a hierarchical system Root of the system is ActorSystem Expensive to instantiate – create and hold! ActorSystem can then be used to create Actors
  37. 37. CREATING AN ACTOR An Actor has a unique address Can be accessed/communicated to like a URI Call ActorOf off ActorSystem, provide it a name  URI (actor path) is created hierarchically Actor Path = akka://myName/user/announcer
  38. 38. SENDING A MESSAGE Messages are basis of actor communication  Should be IMMUTABLE!!! “Tell” an actor a message Async call  Immutable!!
  39. 39. RECEIVING A MESSAGE Create your Actor Derive from ReceiveActor In constructor, register your message subscriptions Looks similar to a CQRS Handler
  40. 40. RECEIVING A MESSAGE Each actor has a mailbox Messages are received in the actor’s mailbox (queue) Actor is notified that there’s a message  It receives the message  Takes it out of the mailbox  Next one is queued up Ordered delivery by default  Other mailboxes (like Priority) that are out-of-order Actor Mailbox Msg
  41. 41. DEMO: HELLO AKKA.NET
  42. 42. ELEMENTS OF CQRS IN ACTOR MODEL Message-based Potentially async Focused code Actor System is your Command Side
  43. 43. CQRS + ACTORS Combining the principles of CQRS and the patterns of the Actor Model Message-based communication with concurrent processing
  44. 44. CQRS + ACTORS: MESSAGES Messages in an Actor System include both Commands and Events Still encapsulate intent Still should still contain information necessary to process/handle command or event Basically, no big change here
  45. 45. CQRS + ACTORS: MESSAGES
  46. 46. CQRS + ACTORS: HANDLERS Handlers begin to encapsulate your Actor System Generally a single Actor System per handler Have several actors at top level of hierarchy to handle initial messages Domain can be injected into the Actor System via Create() Handler Client Queue Persistence Other Handler Direct call or via queue
  47. 47. CQRS + ACTORS: HANDLERS
  48. 48. CQRS + ACTORS: HANDLERS
  49. 49. CQRS + ACTORS: HANDLERS Handlers act as the entry point  Web API  Service subscribed to queue CQRS  IHandle<T> for those messages “exposed” Actor System is called by the CQRS Handler Actor System has many other “internal” actors
  50. 50. CQRS + ACTORS: SERVICES Word Counter Handler super Count Write Stats A B G H Y Z
  51. 51. DEMO: HELLO CQRS + AKKA.NET
  52. 52. Word Counter Handler CQRS + ACTORS: SERVICES super Count A B Word Writer Handler super Write G H Word Stats Handler super Stats Y Z Message Bus
  53. 53. CQRS + ACTORS + SERVICES Service A Service B Service C Message Bus DW / DL / Read Model Cmds / Events Cmds / Events API Gateway SvcCommands Queries RM RMRM Queries (?)
  54. 54. Service A Service A Service A CQRS + ACTORS + SERVICES + CLUSTER (AKKA.CLUSTER) Service A Service B Service C Message Bus DW / DL / Read Model Cmds / Events Cmds / Events API Gateway SvcCommands Queries RM RMRM Queries (?)
  55. 55. CQRS + ACTORS + SERVICES Actors act as the workers for your handlers Services encapsulate handler into a bounded context CQRS is the messaging and communication pattern within system Using these three helps build killer reactive applications
  56. 56. CONCLUSIONS CQRS is not dead Key principles of messaging, separate paths and responsibility segregation Apply CQRS to Reactive systems (e.g. an Akka.NET system) for best of both Encapsulate handlers into services for scalability and reliability

×