SlideShare a Scribd company logo
A scalable pattern for building large multi-user system




Brian Ritchie                  Twitter: @brian_ritchie
Chief Architect                Email: brian.ritchie@gmail.com
PaySpan, Inc.                  Blog: http://weblog.asp.net/britchie
                               Web: http://www.dotnetpowered.com
Brian Ritchie
» Chief Architect at PaySpan, Inc.
» Nearly 20 years of development
  experience
» Developing on .NET since 1.0
  Beta 1
» Contributed to Mono and other
  open source projects
Agenda
» What is CQRS anyway?
» Why is it needed?
» How does CQRS work?
» When should I use CQRS?
» Review example implementation
» According to Wikipedia:
  "CQRS is simply the creation of two objects
  where there was previously only one. The
  separation occurs based upon whether the
  methods are a command or a query (the same
  definition that is used by Meyer in Command
  and Query Separation, a command is any
  method that mutates state and a query is any
  method that returns a value)."
CQRS: Command/Query Responsibility Segregation
Put another way…
  Command/Query Responsibility Segregation (CQRS) is the
  idea that you can use a different model to update
  information than the model you use to read information.

In this context,
» Commands = Writes
» Queries = Reads

Pioneered by Greg Young & Udi Dahan
CQRS: Command/Query Responsibility Segregation
Let’s take a step back. Why do we build applications like we do today?

                                           It started with a stack of paper…




                                 …that needed to be keyed
        …and along came              into the machine
         the CRUD app!
Being good developers, we didn’t stop there. We built various models to
      protect us from change at different layers of the application.




                                              Diagram © Martin Fowler
But this wasn’t scalable…so we add more layers.




Not only did we add layers, but also complexity.
» All of this to provide scalability & a consistent view
  of the data.

                 But did we succeed?
Back to our CRUD app…



                                                             ?
                          ?



   ?          ?
                                                             ?
                          ?




Where is the consistency? We have stale data all over the place!
To understand this better, let’s look at a basic multi-user system.




                    Retrieve data
                                            Retrieve data

User is looking at stale
          data                                               Modify data




            Stale data is inherent in a multi-user system.
     The machine is now the source of truth…not a piece of paper.
Which begs the question…

» Is the data the user is looking at right now stale?
» Since stale data always exists,    No, we need a different
  is all of this complexity really   approach.
  needed to scale?
                                     One that…
                                     » Offers extreme scalability
                                     » Inherently handle multiple
                                       users
                                     » Can grow to handle
                                       complex problems without
                                       growing development costs
Which brings us
back to CQRS…



           Command captures the
             intent of the user


                                                                          Scale out
                                                                          as many
                                                                          copies as
                                                                           needed



                                                                            Persistent View Model schema
           After database is                                                   matches UI view model
           updated, publish
         result to view model
                                                         Diagram from Rinat Abdullin
                                                         http://abdullin.com/cqrs
                                    A queue can be
                                  utilized to optimize
                                  write performance
Let’s break it down…
Common components of the CQRS pattern:
» Task-based UI
» Commands
» Domain Objects
» Events
» Persistent View Model


             Note: these are common components…not required components
Task-based UI
Task-based UI
Why rethink the User Interface?
» Grids don’t capture the user’s intent
Task-based UI
Rethinking the User Interface
» Adjust UI design to capture intent
   ˃ what did the user really mean?
   ˃ intent becomes a command

» Why is intent important?
   ˃ Last name changed because of misspelling
   ˃ Last name changed because of marriage
   ˃ Last name changed because of divorce

» User interface can affect your architecture
Task-based UI
» Validation
   ˃ increase likelihood of command succeeding
   ˃ validate client-side
   ˃ optimize validation using persistent view model

» What about user feedback?
   ˃ Polling: wait until read model is updated
   ˃ Use asynchronous messaging such as email
       “Your request is being processed. You will receive an email
       when it is completed”
   ˃ Just fake it!
       Scope the change to the current user. Update a local in-
       memory model
Commands
Commands
» Commands encapsulate the user’s intent but do
  not contain business logic, only enough data for
  the command
» What makes a good command?
   ˃A command is an action – starts with a verb
   ˃The kind you can reply with: “Thank you. Your confirmation
    email will arrive shortly”. Inherently asynchronous.
» Commands can be considered messages
   ˃Messaging provides an asynchronous delivery mechanism
    for the commands. As a message, it can be
    routed, queued, and transformed all independent of the
    sender & receiver
Commands & Domain Objects
» The domain model is utilized for processing
  commands; it is unnecessary for queries.
» Unlike entity objects you may be used
  to, aggregate roots in CQRS only have methods (no
  getters/setters)
  Aggregate Roots
  Some things belong together, like Apple Pie and Ice Cream, or Sonny and
  Cher. And so it is with Entities and Value Objects (VOs) – some of them
  belong together. Aggregates are groups of things that belong together. An
  Aggregate Root is the thing that holds them all together.
  Example: OrderLines have no reason to exist without their parent Order, nor can they belong to
  any other Order. In this case, Order and OrderLines would be an Aggregate, and the Order
  would be the Aggregate Root
Commands & Domain Objects
» Setters are an anti pattern in your domain. DDD is not about
  modeling data, or nouns. It is about modeling behaviors that
  are solving the domain problem, or verbs.
» The public interface of your domain should solely consist in
  public methods on your aggregate roots. The idea is that each
  method represents a use case.
» From a design perspective, it is also the only way to ensure
  your objects invariants. That way, your aggregates are always
  fully consistent – they valid state at all times.
» If DDD is about behavior, then getters also should be an anti
  pattern. And they are.

                         Julienn Letrouit http://julienletrouit.com/?p=22
Events
Events
» Events describe changes in the system state
» An Event Bus can be utilized to dispatch events to
  subscribers
» Events primary purpose update the read model
» Events can also provider integration with external systems
» CQRS can also be used in conjunction with Event Sourcing.
 Event Sourcing
 Captures all changes to an application state as a sequence of events. The
 current state is constructed by applying the events in the order they were
 recorded. Not only does it give us the current state, but we can also use the
 event log to reconstruct past states, and as a foundation to automatically adjust
 the state to cope with retroactive changes.
 Summarized from Martin Fowler – http://martinfowler.com/eaaDev/EventSourcing.html
Persistent View Model
Persistent View Model

» Reads are usually the most common activity –
  many times 80-90%. Why not optimize them?
» Read model is based on how the user wants to see
  the data.
» Read model can be denormalized
  RDBMS, document store, etc.
» Reads from the view model don’t need to be
  loaded into the domain model, they can be bond
  directly to the UI.
Persistent View Model


                      UI
                                 Query only…keep it simple



            Persistent View Model


           For each view in the UI,
       have a view/table in the database

SELECT * FROM ViewModelTable (WHERE ID = @ID)
Persistent View Model
              Data Duplicated, No Relationships, Data Pre-Calculated
Customer Service Rep view                              Supervisor view
          List of customers                            List of customers
ID        Name    Phone                 ID      Name     Phone Lifetime value




          Rep_Customers_Table                     Supervisor_Customers_Table
     ID          Name           Phone     ID      Name        Phone        Lifetime Value
First off, when should I avoid it?
» CQRS can be overkill for simple applications.
» Don’t use it in a non-collaborative domain or
  where you can horizontally add more database
  servers to support more users/requests/data at
  the same time you’re adding web servers – there
  is no real scalability problem – Udi Dahan
CQRS is a pattern that is usually leveraged for a portion of a
system.
» This builds on a concept from Domain Driven Design (DDD)
   known as a Bounded Context.
  Bounded Context
  A Bounded Context is an area of your application which has explicitly defined borders, has it’s own
  model, has it’s own Model, and maintains it’s own code. - Jak Charlton

  A Bounded Context can be considered as a miniature application, containing it’s own
  domain, code and persistence mechanisms. Within a Bounded Context, there should be logical
  consistency, each Bounded Context should be independent of any other Bounded Context. -
  ThinkDDD.org

» A typical application there are multiple bounded
  contexts, any of which can be implemented the way it
  makes sense.
Guidelines for using CQRS:
» Large, multi-user systems CQRS is designed to address
  concurrency issues.
» Scalability matters With CQRS you can achieve great read
  and write performance. The system intrinsically supports
  scaling out. By separating read & write operations, each
  can be optimized.
» Difficult business logic CQRS forces you to not mix domain
  logic and infrastructural operations.
» Large or Distributed teams you can split development tasks
  between different teams with defined interfaces.
CQRS: Command/Query Responsibility Segregation
Commands are simple object that contain all the data to perform the underlying action. They
                          also express intent by there name.
An Command Executors accepts commands of a certain type and performs a corresponding
 action. The action should not contain business logic and should directly use the domain.
All events that have occurred end up in the event store. It contains all the event that
represents the state changes in the system. These can be used to build up the current state by
  replaying them all. This store can also be used to fill up new or repair existing read model.




                      For the event store, NCQRS supports
                  MSSQL, MongoDB, RavenDB, SqlLite, and more…
NCQRS provides a base class for denormalizers that allows them
             to be subscribed to the event bus.
Command / Query Responsibility Segregation
                  A scalable pattern for building large multi-user system




Brian Ritchie                          Twitter : @brian_ritchie
Chief Architect                        Email: brian.ritchie@gmail.com
PaySpan, Inc.                          Blog: http://weblog.asp.net/britchie
                                       Web: http://www.dotnetpowered.com
Command / Query Responsibility Segregation
                  A scalable pattern for building large multi-user system




Brian Ritchie                          Twitter : @brian_ritchie
Chief Architect                        Email: brian.ritchie@gmail.com
PaySpan, Inc.                          Blog: http://weblog.asp.net/britchie
                                       Web: http://www.dotnetpowered.com
Ad

More Related Content

What's hot (20)

CQRS and Event Sourcing, An Alternative Architecture for DDD
CQRS and Event Sourcing, An Alternative Architecture for DDDCQRS and Event Sourcing, An Alternative Architecture for DDD
CQRS and Event Sourcing, An Alternative Architecture for DDD
Dennis Doomen
 
Building Event Driven (Micro)services with Apache Kafka
Building Event Driven (Micro)services with Apache KafkaBuilding Event Driven (Micro)services with Apache Kafka
Building Event Driven (Micro)services with Apache Kafka
Guido Schmutz
 
CQRS
CQRSCQRS
CQRS
Piotr Pelczar
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
Araf Karsh Hamid
 
CQRS and Event Sourcing in Action
CQRS and Event  Sourcing in ActionCQRS and Event  Sourcing in Action
CQRS and Event Sourcing in Action
Knoldus Inc.
 
Introduction to CQRS (Command Query Responsibility Segregation)
Introduction to CQRS (Command Query Responsibility Segregation)Introduction to CQRS (Command Query Responsibility Segregation)
Introduction to CQRS (Command Query Responsibility Segregation)
GlobalLogic Ukraine
 
Design patterns for microservice architecture
Design patterns for microservice architectureDesign patterns for microservice architecture
Design patterns for microservice architecture
The Software House
 
Microservices for Application Modernisation
Microservices for Application ModernisationMicroservices for Application Modernisation
Microservices for Application Modernisation
Ajay Kumar Uppal
 
Domain Driven Design
Domain Driven Design Domain Driven Design
Domain Driven Design
Araf Karsh Hamid
 
The Art of Discovering Bounded Contexts
The Art of Discovering Bounded ContextsThe Art of Discovering Bounded Contexts
The Art of Discovering Bounded Contexts
Nick Tune
 
Microservice architecture design principles
Microservice architecture design principlesMicroservice architecture design principles
Microservice architecture design principles
Sanjoy Kumar Roy
 
CQRS and Event Sourcing
CQRS and Event Sourcing CQRS and Event Sourcing
CQRS and Event Sourcing
Inho Kang
 
Docker swarm
Docker swarmDocker swarm
Docker swarm
Alberto Guimarães Viana
 
Microservice Architecture with CQRS and Event Sourcing
Microservice Architecture with CQRS and Event SourcingMicroservice Architecture with CQRS and Event Sourcing
Microservice Architecture with CQRS and Event Sourcing
Ben Wilcock
 
WAS vs JBoss, WebLogic, Tomcat (year 2015)
WAS vs JBoss, WebLogic, Tomcat (year 2015)WAS vs JBoss, WebLogic, Tomcat (year 2015)
WAS vs JBoss, WebLogic, Tomcat (year 2015)
Roman Kharkovski
 
Microservices Architecture Part 2 Event Sourcing and Saga
Microservices Architecture Part 2 Event Sourcing and SagaMicroservices Architecture Part 2 Event Sourcing and Saga
Microservices Architecture Part 2 Event Sourcing and Saga
Araf Karsh Hamid
 
Simplifying Distributed Transactions with Sagas in Kafka (Stephen Zoio, Simpl...
Simplifying Distributed Transactions with Sagas in Kafka (Stephen Zoio, Simpl...Simplifying Distributed Transactions with Sagas in Kafka (Stephen Zoio, Simpl...
Simplifying Distributed Transactions with Sagas in Kafka (Stephen Zoio, Simpl...
confluent
 
knolx of KubeCost & Infracost
knolx of KubeCost & Infracostknolx of KubeCost & Infracost
knolx of KubeCost & Infracost
Knoldus Inc.
 
CQRS and what it means for your architecture
CQRS and what it means for your architectureCQRS and what it means for your architecture
CQRS and what it means for your architecture
Richard Banks
 
Getting Started With Cypress
Getting Started With CypressGetting Started With Cypress
Getting Started With Cypress
Knoldus Inc.
 
CQRS and Event Sourcing, An Alternative Architecture for DDD
CQRS and Event Sourcing, An Alternative Architecture for DDDCQRS and Event Sourcing, An Alternative Architecture for DDD
CQRS and Event Sourcing, An Alternative Architecture for DDD
Dennis Doomen
 
Building Event Driven (Micro)services with Apache Kafka
Building Event Driven (Micro)services with Apache KafkaBuilding Event Driven (Micro)services with Apache Kafka
Building Event Driven (Micro)services with Apache Kafka
Guido Schmutz
 
CQRS and Event Sourcing in Action
CQRS and Event  Sourcing in ActionCQRS and Event  Sourcing in Action
CQRS and Event Sourcing in Action
Knoldus Inc.
 
Introduction to CQRS (Command Query Responsibility Segregation)
Introduction to CQRS (Command Query Responsibility Segregation)Introduction to CQRS (Command Query Responsibility Segregation)
Introduction to CQRS (Command Query Responsibility Segregation)
GlobalLogic Ukraine
 
Design patterns for microservice architecture
Design patterns for microservice architectureDesign patterns for microservice architecture
Design patterns for microservice architecture
The Software House
 
Microservices for Application Modernisation
Microservices for Application ModernisationMicroservices for Application Modernisation
Microservices for Application Modernisation
Ajay Kumar Uppal
 
The Art of Discovering Bounded Contexts
The Art of Discovering Bounded ContextsThe Art of Discovering Bounded Contexts
The Art of Discovering Bounded Contexts
Nick Tune
 
Microservice architecture design principles
Microservice architecture design principlesMicroservice architecture design principles
Microservice architecture design principles
Sanjoy Kumar Roy
 
CQRS and Event Sourcing
CQRS and Event Sourcing CQRS and Event Sourcing
CQRS and Event Sourcing
Inho Kang
 
Microservice Architecture with CQRS and Event Sourcing
Microservice Architecture with CQRS and Event SourcingMicroservice Architecture with CQRS and Event Sourcing
Microservice Architecture with CQRS and Event Sourcing
Ben Wilcock
 
WAS vs JBoss, WebLogic, Tomcat (year 2015)
WAS vs JBoss, WebLogic, Tomcat (year 2015)WAS vs JBoss, WebLogic, Tomcat (year 2015)
WAS vs JBoss, WebLogic, Tomcat (year 2015)
Roman Kharkovski
 
Microservices Architecture Part 2 Event Sourcing and Saga
Microservices Architecture Part 2 Event Sourcing and SagaMicroservices Architecture Part 2 Event Sourcing and Saga
Microservices Architecture Part 2 Event Sourcing and Saga
Araf Karsh Hamid
 
Simplifying Distributed Transactions with Sagas in Kafka (Stephen Zoio, Simpl...
Simplifying Distributed Transactions with Sagas in Kafka (Stephen Zoio, Simpl...Simplifying Distributed Transactions with Sagas in Kafka (Stephen Zoio, Simpl...
Simplifying Distributed Transactions with Sagas in Kafka (Stephen Zoio, Simpl...
confluent
 
knolx of KubeCost & Infracost
knolx of KubeCost & Infracostknolx of KubeCost & Infracost
knolx of KubeCost & Infracost
Knoldus Inc.
 
CQRS and what it means for your architecture
CQRS and what it means for your architectureCQRS and what it means for your architecture
CQRS and what it means for your architecture
Richard Banks
 
Getting Started With Cypress
Getting Started With CypressGetting Started With Cypress
Getting Started With Cypress
Knoldus Inc.
 

Similar to CQRS: Command/Query Responsibility Segregation (20)

Workshop - cqrs brief introduction
Workshop - cqrs brief introductionWorkshop - cqrs brief introduction
Workshop - cqrs brief introduction
Francesco Garavaglia
 
Software architecture patterns
Software architecture patternsSoftware architecture patterns
Software architecture patterns
Md. Sadhan Sarker
 
Applicare patterns di sviluppo con Azure
Applicare patterns di sviluppo con AzureApplicare patterns di sviluppo con Azure
Applicare patterns di sviluppo con Azure
Marco Parenzan
 
MVVM for Modern Applications
MVVM for Modern ApplicationsMVVM for Modern Applications
MVVM for Modern Applications
Jeremy Likness
 
TU-Charts Project - First Spring
TU-Charts Project - First SpringTU-Charts Project - First Spring
TU-Charts Project - First Spring
Didac Montero
 
Application architecture for cloud
Application architecture for cloudApplication architecture for cloud
Application architecture for cloud
Marco Parenzan
 
Deconstructing Monoliths with Domain Driven Design
Deconstructing Monoliths with Domain Driven DesignDeconstructing Monoliths with Domain Driven Design
Deconstructing Monoliths with Domain Driven Design
VMware Tanzu
 
Intro to Databases
Intro to DatabasesIntro to Databases
Intro to Databases
Sargun Dhillon
 
JavaScript for Enterprise Applications
JavaScript for Enterprise ApplicationsJavaScript for Enterprise Applications
JavaScript for Enterprise Applications
Piyush Katariya
 
cloud-computing presentation using various methods
cloud-computing presentation using various methodscloud-computing presentation using various methods
cloud-computing presentation using various methods
annupriya1295
 
First spring
First springFirst spring
First spring
Didac Montero
 
Cloud Design Patterns
Cloud Design PatternsCloud Design Patterns
Cloud Design Patterns
Carlos Mendible
 
Introduction to CQRS - command and query responsibility segregation
Introduction to CQRS - command and query responsibility segregationIntroduction to CQRS - command and query responsibility segregation
Introduction to CQRS - command and query responsibility segregation
Andrew Siemer
 
Azure and cloud design patterns
Azure and cloud design patternsAzure and cloud design patterns
Azure and cloud design patterns
Venkatesh Narayanan
 
Enterprise Software Development Patterns
Enterprise Software Development PatternsEnterprise Software Development Patterns
Enterprise Software Development Patterns
Josh Lane
 
Cloud computing virtualization
Cloud computing virtualizationCloud computing virtualization
Cloud computing virtualization
Vaibhav Khanna
 
Cqrs and Event Sourcing Intro For Developers
Cqrs and Event Sourcing Intro For DevelopersCqrs and Event Sourcing Intro For Developers
Cqrs and Event Sourcing Intro For Developers
wojtek_s
 
Iot cloud service v2.0
Iot cloud service v2.0Iot cloud service v2.0
Iot cloud service v2.0
Vinod Wilson
 
Integration Patterns and Anti-Patterns for Microservices Architectures
Integration Patterns and Anti-Patterns for Microservices ArchitecturesIntegration Patterns and Anti-Patterns for Microservices Architectures
Integration Patterns and Anti-Patterns for Microservices Architectures
Apcera
 
MVC
MVCMVC
MVC
Devan Muthunayakan Sreemandiram
 
Workshop - cqrs brief introduction
Workshop - cqrs brief introductionWorkshop - cqrs brief introduction
Workshop - cqrs brief introduction
Francesco Garavaglia
 
Software architecture patterns
Software architecture patternsSoftware architecture patterns
Software architecture patterns
Md. Sadhan Sarker
 
Applicare patterns di sviluppo con Azure
Applicare patterns di sviluppo con AzureApplicare patterns di sviluppo con Azure
Applicare patterns di sviluppo con Azure
Marco Parenzan
 
MVVM for Modern Applications
MVVM for Modern ApplicationsMVVM for Modern Applications
MVVM for Modern Applications
Jeremy Likness
 
TU-Charts Project - First Spring
TU-Charts Project - First SpringTU-Charts Project - First Spring
TU-Charts Project - First Spring
Didac Montero
 
Application architecture for cloud
Application architecture for cloudApplication architecture for cloud
Application architecture for cloud
Marco Parenzan
 
Deconstructing Monoliths with Domain Driven Design
Deconstructing Monoliths with Domain Driven DesignDeconstructing Monoliths with Domain Driven Design
Deconstructing Monoliths with Domain Driven Design
VMware Tanzu
 
JavaScript for Enterprise Applications
JavaScript for Enterprise ApplicationsJavaScript for Enterprise Applications
JavaScript for Enterprise Applications
Piyush Katariya
 
cloud-computing presentation using various methods
cloud-computing presentation using various methodscloud-computing presentation using various methods
cloud-computing presentation using various methods
annupriya1295
 
Introduction to CQRS - command and query responsibility segregation
Introduction to CQRS - command and query responsibility segregationIntroduction to CQRS - command and query responsibility segregation
Introduction to CQRS - command and query responsibility segregation
Andrew Siemer
 
Enterprise Software Development Patterns
Enterprise Software Development PatternsEnterprise Software Development Patterns
Enterprise Software Development Patterns
Josh Lane
 
Cloud computing virtualization
Cloud computing virtualizationCloud computing virtualization
Cloud computing virtualization
Vaibhav Khanna
 
Cqrs and Event Sourcing Intro For Developers
Cqrs and Event Sourcing Intro For DevelopersCqrs and Event Sourcing Intro For Developers
Cqrs and Event Sourcing Intro For Developers
wojtek_s
 
Iot cloud service v2.0
Iot cloud service v2.0Iot cloud service v2.0
Iot cloud service v2.0
Vinod Wilson
 
Integration Patterns and Anti-Patterns for Microservices Architectures
Integration Patterns and Anti-Patterns for Microservices ArchitecturesIntegration Patterns and Anti-Patterns for Microservices Architectures
Integration Patterns and Anti-Patterns for Microservices Architectures
Apcera
 
Ad

More from Brian Ritchie (7)

Transforming your application with Elasticsearch
Transforming your application with ElasticsearchTransforming your application with Elasticsearch
Transforming your application with Elasticsearch
Brian Ritchie
 
Building Event-Driven Systems with Apache Kafka
Building Event-Driven Systems with Apache KafkaBuilding Event-Driven Systems with Apache Kafka
Building Event-Driven Systems with Apache Kafka
Brian Ritchie
 
From Dev to Ops:Delivering an API to Production with Splunk
From Dev to Ops:Delivering an API to Production with SplunkFrom Dev to Ops:Delivering an API to Production with Splunk
From Dev to Ops:Delivering an API to Production with Splunk
Brian Ritchie
 
Extending the Enterprise with MEF
Extending the Enterprise with MEFExtending the Enterprise with MEF
Extending the Enterprise with MEF
Brian Ritchie
 
IIS Always-On Services
IIS Always-On ServicesIIS Always-On Services
IIS Always-On Services
Brian Ritchie
 
Scaling Out .NET
Scaling Out .NETScaling Out .NET
Scaling Out .NET
Brian Ritchie
 
Document Databases & RavenDB
Document Databases & RavenDBDocument Databases & RavenDB
Document Databases & RavenDB
Brian Ritchie
 
Transforming your application with Elasticsearch
Transforming your application with ElasticsearchTransforming your application with Elasticsearch
Transforming your application with Elasticsearch
Brian Ritchie
 
Building Event-Driven Systems with Apache Kafka
Building Event-Driven Systems with Apache KafkaBuilding Event-Driven Systems with Apache Kafka
Building Event-Driven Systems with Apache Kafka
Brian Ritchie
 
From Dev to Ops:Delivering an API to Production with Splunk
From Dev to Ops:Delivering an API to Production with SplunkFrom Dev to Ops:Delivering an API to Production with Splunk
From Dev to Ops:Delivering an API to Production with Splunk
Brian Ritchie
 
Extending the Enterprise with MEF
Extending the Enterprise with MEFExtending the Enterprise with MEF
Extending the Enterprise with MEF
Brian Ritchie
 
IIS Always-On Services
IIS Always-On ServicesIIS Always-On Services
IIS Always-On Services
Brian Ritchie
 
Document Databases & RavenDB
Document Databases & RavenDBDocument Databases & RavenDB
Document Databases & RavenDB
Brian Ritchie
 
Ad

Recently uploaded (20)

SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 

CQRS: Command/Query Responsibility Segregation

  • 1. A scalable pattern for building large multi-user system Brian Ritchie Twitter: @brian_ritchie Chief Architect Email: [email protected] PaySpan, Inc. Blog: http://weblog.asp.net/britchie Web: http://www.dotnetpowered.com
  • 2. Brian Ritchie » Chief Architect at PaySpan, Inc. » Nearly 20 years of development experience » Developing on .NET since 1.0 Beta 1 » Contributed to Mono and other open source projects
  • 3. Agenda » What is CQRS anyway? » Why is it needed? » How does CQRS work? » When should I use CQRS? » Review example implementation
  • 4. » According to Wikipedia: "CQRS is simply the creation of two objects where there was previously only one. The separation occurs based upon whether the methods are a command or a query (the same definition that is used by Meyer in Command and Query Separation, a command is any method that mutates state and a query is any method that returns a value)."
  • 6. Put another way… Command/Query Responsibility Segregation (CQRS) is the idea that you can use a different model to update information than the model you use to read information. In this context, » Commands = Writes » Queries = Reads Pioneered by Greg Young & Udi Dahan
  • 8. Let’s take a step back. Why do we build applications like we do today? It started with a stack of paper… …that needed to be keyed …and along came into the machine the CRUD app!
  • 9. Being good developers, we didn’t stop there. We built various models to protect us from change at different layers of the application. Diagram © Martin Fowler
  • 10. But this wasn’t scalable…so we add more layers. Not only did we add layers, but also complexity.
  • 11. » All of this to provide scalability & a consistent view of the data. But did we succeed?
  • 12. Back to our CRUD app… ? ? ? ? ? ? Where is the consistency? We have stale data all over the place!
  • 13. To understand this better, let’s look at a basic multi-user system. Retrieve data Retrieve data User is looking at stale data Modify data Stale data is inherent in a multi-user system. The machine is now the source of truth…not a piece of paper.
  • 14. Which begs the question… » Is the data the user is looking at right now stale?
  • 15. » Since stale data always exists, No, we need a different is all of this complexity really approach. needed to scale? One that… » Offers extreme scalability » Inherently handle multiple users » Can grow to handle complex problems without growing development costs
  • 16. Which brings us back to CQRS… Command captures the intent of the user Scale out as many copies as needed Persistent View Model schema After database is matches UI view model updated, publish result to view model Diagram from Rinat Abdullin http://abdullin.com/cqrs A queue can be utilized to optimize write performance
  • 17. Let’s break it down… Common components of the CQRS pattern: » Task-based UI » Commands » Domain Objects » Events » Persistent View Model Note: these are common components…not required components
  • 19. Task-based UI Why rethink the User Interface? » Grids don’t capture the user’s intent
  • 20. Task-based UI Rethinking the User Interface » Adjust UI design to capture intent ˃ what did the user really mean? ˃ intent becomes a command » Why is intent important? ˃ Last name changed because of misspelling ˃ Last name changed because of marriage ˃ Last name changed because of divorce » User interface can affect your architecture
  • 21. Task-based UI » Validation ˃ increase likelihood of command succeeding ˃ validate client-side ˃ optimize validation using persistent view model » What about user feedback? ˃ Polling: wait until read model is updated ˃ Use asynchronous messaging such as email “Your request is being processed. You will receive an email when it is completed” ˃ Just fake it! Scope the change to the current user. Update a local in- memory model
  • 23. Commands » Commands encapsulate the user’s intent but do not contain business logic, only enough data for the command » What makes a good command? ˃A command is an action – starts with a verb ˃The kind you can reply with: “Thank you. Your confirmation email will arrive shortly”. Inherently asynchronous. » Commands can be considered messages ˃Messaging provides an asynchronous delivery mechanism for the commands. As a message, it can be routed, queued, and transformed all independent of the sender & receiver
  • 24. Commands & Domain Objects » The domain model is utilized for processing commands; it is unnecessary for queries. » Unlike entity objects you may be used to, aggregate roots in CQRS only have methods (no getters/setters) Aggregate Roots Some things belong together, like Apple Pie and Ice Cream, or Sonny and Cher. And so it is with Entities and Value Objects (VOs) – some of them belong together. Aggregates are groups of things that belong together. An Aggregate Root is the thing that holds them all together. Example: OrderLines have no reason to exist without their parent Order, nor can they belong to any other Order. In this case, Order and OrderLines would be an Aggregate, and the Order would be the Aggregate Root
  • 25. Commands & Domain Objects » Setters are an anti pattern in your domain. DDD is not about modeling data, or nouns. It is about modeling behaviors that are solving the domain problem, or verbs. » The public interface of your domain should solely consist in public methods on your aggregate roots. The idea is that each method represents a use case. » From a design perspective, it is also the only way to ensure your objects invariants. That way, your aggregates are always fully consistent – they valid state at all times. » If DDD is about behavior, then getters also should be an anti pattern. And they are. Julienn Letrouit http://julienletrouit.com/?p=22
  • 27. Events » Events describe changes in the system state » An Event Bus can be utilized to dispatch events to subscribers » Events primary purpose update the read model » Events can also provider integration with external systems » CQRS can also be used in conjunction with Event Sourcing. Event Sourcing Captures all changes to an application state as a sequence of events. The current state is constructed by applying the events in the order they were recorded. Not only does it give us the current state, but we can also use the event log to reconstruct past states, and as a foundation to automatically adjust the state to cope with retroactive changes. Summarized from Martin Fowler – http://martinfowler.com/eaaDev/EventSourcing.html
  • 29. Persistent View Model » Reads are usually the most common activity – many times 80-90%. Why not optimize them? » Read model is based on how the user wants to see the data. » Read model can be denormalized RDBMS, document store, etc. » Reads from the view model don’t need to be loaded into the domain model, they can be bond directly to the UI.
  • 30. Persistent View Model UI Query only…keep it simple Persistent View Model For each view in the UI, have a view/table in the database SELECT * FROM ViewModelTable (WHERE ID = @ID)
  • 31. Persistent View Model Data Duplicated, No Relationships, Data Pre-Calculated Customer Service Rep view Supervisor view List of customers List of customers ID Name Phone ID Name Phone Lifetime value Rep_Customers_Table Supervisor_Customers_Table ID Name Phone ID Name Phone Lifetime Value
  • 32. First off, when should I avoid it? » CQRS can be overkill for simple applications. » Don’t use it in a non-collaborative domain or where you can horizontally add more database servers to support more users/requests/data at the same time you’re adding web servers – there is no real scalability problem – Udi Dahan
  • 33. CQRS is a pattern that is usually leveraged for a portion of a system. » This builds on a concept from Domain Driven Design (DDD) known as a Bounded Context. Bounded Context A Bounded Context is an area of your application which has explicitly defined borders, has it’s own model, has it’s own Model, and maintains it’s own code. - Jak Charlton A Bounded Context can be considered as a miniature application, containing it’s own domain, code and persistence mechanisms. Within a Bounded Context, there should be logical consistency, each Bounded Context should be independent of any other Bounded Context. - ThinkDDD.org » A typical application there are multiple bounded contexts, any of which can be implemented the way it makes sense.
  • 34. Guidelines for using CQRS: » Large, multi-user systems CQRS is designed to address concurrency issues. » Scalability matters With CQRS you can achieve great read and write performance. The system intrinsically supports scaling out. By separating read & write operations, each can be optimized. » Difficult business logic CQRS forces you to not mix domain logic and infrastructural operations. » Large or Distributed teams you can split development tasks between different teams with defined interfaces.
  • 36. Commands are simple object that contain all the data to perform the underlying action. They also express intent by there name.
  • 37. An Command Executors accepts commands of a certain type and performs a corresponding action. The action should not contain business logic and should directly use the domain.
  • 38. All events that have occurred end up in the event store. It contains all the event that represents the state changes in the system. These can be used to build up the current state by replaying them all. This store can also be used to fill up new or repair existing read model. For the event store, NCQRS supports MSSQL, MongoDB, RavenDB, SqlLite, and more…
  • 39. NCQRS provides a base class for denormalizers that allows them to be subscribed to the event bus.
  • 40. Command / Query Responsibility Segregation A scalable pattern for building large multi-user system Brian Ritchie Twitter : @brian_ritchie Chief Architect Email: [email protected] PaySpan, Inc. Blog: http://weblog.asp.net/britchie Web: http://www.dotnetpowered.com
  • 41. Command / Query Responsibility Segregation A scalable pattern for building large multi-user system Brian Ritchie Twitter : @brian_ritchie Chief Architect Email: [email protected] PaySpan, Inc. Blog: http://weblog.asp.net/britchie Web: http://www.dotnetpowered.com