Event Sourcing and
CQRS
26 October, 2019
.NET Conf 2019 Dhaka
Rezwan Rafiq
Sr. Software Engineer,
SELISE
rir@selise.ch
What is Event Sourcing?
Let us think of a typical ecommerce software
Example use cases
Add items to
cart
Delete items Checkout Make payment
Event Sourcing
● Logging?
● Are they for auditing purpose?
● New business requirement to run a campaign?
CQRS
What is CQRS?
CQRS
C Command
Q Query
R Responsibility
S Segregation
It states that every method should
either be a command that performs an
action, or a query that returns data to
the caller, but not both. In other
words, asking a question should not
change the answer. More formally,
methods should return a value only if
they are referentially transparent and
hence possess no side effects
CQRS
Commands
Take the write
responsibilities
Queries
Take the read
responsibilities
Commands
POST, PUT,
PATCH,
DELETE
Queries GET
○ Use separate model to update the
state
○ Use different model to read the
information
Why do we need CQRS?
Overview of a software system
More users are coming in...
● Now what?
○ What is we need more memory?
○ More power?
○ More TPS?
● So, let’s be a good developer and add more complexity and layers!
Achieve scalability using load balancer
What have we achieved?
or
Scalable
system
Consistent
view
Now the application looks like...
Where is the consistency?
Stale data everywhere
What is stale data?
● Stale data is an artifact of caching, in which an object in the cache is not the most recent
version committed to the data source
What have we achieved so far?
● Since stale data exists, why did we take the trouble to scale by adding more complexity?
How CQRS works...
How CQRS works...
● Suggestive component of CQRS pattern
○ Should use task based UI
○ Should use domain object
○ Should use persistent view model
○ Should use command
○ Should use event
Task based UI
● Intent
● Intent becomes Command
● Example intent
○ Supplier transaction entry – Reason new bill
○ Supplier transaction entry – Reason error adjustment
● Validation
○ Increase the chance for a command to be successful
○ Validate client side
● Messaging
○ Asynchronous messaging such as “Your request will be processed soon”
● Temporarily show the assume changed in the current user state.
Command
● An action starts with the verb
● We can provide the ack/nak in return
● Command can be considered as message. It can be routed, queued to all independent of
the sender & receiver as a message
● It encapsulates the intent but not any business logic
Command & Domain Object
● Domain model is utilized for processing which is not necessary for queries
● Aggregate root
● Unlike entity it only have methods no {get; set;}
● This is the only way to ensure object invariants and the aggregates are fully consistent
Events
● Events describe changes in the system state.
● An event bus can be used. RabbitMQ for example.
● The primary purpose is to update the read model secondarily to integrate to external
system.
View model
● The ratio of read vs write for an average software is: 90% - 10%
● So, lets optimize that
● It is like how the user want to see or how the UI is designed to view
● This does not to be loaded from the domain model. They can hold just enough to supply to
UI
● No math; just pre calculated value
So, should we use CQRS?
Yes, but....
It can add significant over jargon for a simple application
No point of using it for a non collaborative domain or more database
can be added horizontally
Summary
Example Code
https://github.com/iamrezwan/Cqrs
Thank you

Event sourcing and CQRS

  • 1.
    Event Sourcing and CQRS 26October, 2019 .NET Conf 2019 Dhaka
  • 2.
    Rezwan Rafiq Sr. SoftwareEngineer, SELISE rir@selise.ch
  • 3.
    What is EventSourcing? Let us think of a typical ecommerce software
  • 4.
    Example use cases Additems to cart Delete items Checkout Make payment
  • 5.
    Event Sourcing ● Logging? ●Are they for auditing purpose? ● New business requirement to run a campaign?
  • 6.
  • 7.
    CQRS C Command Q Query RResponsibility S Segregation It states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, asking a question should not change the answer. More formally, methods should return a value only if they are referentially transparent and hence possess no side effects
  • 8.
    CQRS Commands Take the write responsibilities Queries Takethe read responsibilities Commands POST, PUT, PATCH, DELETE Queries GET ○ Use separate model to update the state ○ Use different model to read the information
  • 9.
    Why do weneed CQRS?
  • 10.
    Overview of asoftware system
  • 11.
    More users arecoming in... ● Now what? ○ What is we need more memory? ○ More power? ○ More TPS? ● So, let’s be a good developer and add more complexity and layers!
  • 12.
  • 13.
    What have weachieved? or Scalable system Consistent view
  • 14.
    Now the applicationlooks like...
  • 15.
    Where is theconsistency? Stale data everywhere
  • 16.
    What is staledata? ● Stale data is an artifact of caching, in which an object in the cache is not the most recent version committed to the data source
  • 17.
    What have weachieved so far? ● Since stale data exists, why did we take the trouble to scale by adding more complexity?
  • 18.
  • 19.
    How CQRS works... ●Suggestive component of CQRS pattern ○ Should use task based UI ○ Should use domain object ○ Should use persistent view model ○ Should use command ○ Should use event
  • 20.
    Task based UI ●Intent ● Intent becomes Command ● Example intent ○ Supplier transaction entry – Reason new bill ○ Supplier transaction entry – Reason error adjustment ● Validation ○ Increase the chance for a command to be successful ○ Validate client side ● Messaging ○ Asynchronous messaging such as “Your request will be processed soon” ● Temporarily show the assume changed in the current user state.
  • 21.
    Command ● An actionstarts with the verb ● We can provide the ack/nak in return ● Command can be considered as message. It can be routed, queued to all independent of the sender & receiver as a message ● It encapsulates the intent but not any business logic
  • 22.
    Command & DomainObject ● Domain model is utilized for processing which is not necessary for queries ● Aggregate root ● Unlike entity it only have methods no {get; set;} ● This is the only way to ensure object invariants and the aggregates are fully consistent
  • 23.
    Events ● Events describechanges in the system state. ● An event bus can be used. RabbitMQ for example. ● The primary purpose is to update the read model secondarily to integrate to external system.
  • 24.
    View model ● Theratio of read vs write for an average software is: 90% - 10% ● So, lets optimize that ● It is like how the user want to see or how the UI is designed to view ● This does not to be loaded from the domain model. They can hold just enough to supply to UI ● No math; just pre calculated value
  • 25.
    So, should weuse CQRS? Yes, but.... It can add significant over jargon for a simple application No point of using it for a non collaborative domain or more database can be added horizontally
  • 26.
  • 27.