0% found this document useful (0 votes)
11 views23 pages

Introduction Slides

Uploaded by

lounes.lou20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views23 pages

Introduction Slides

Uploaded by

lounes.lou20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CQRS in Practice

INTRODUCTION

Vladimir Khorikov

@vkhorikov www.enterprisecraftsmanship.com
CQRS

Sync
strategies?

CQRS

Separate
DDD
databases?

Event
sourcing?
CQRS
Introduction
Introducing a sample project
Segregating Commands and Queries

Overview Refactoring Towards Task-based


Interface
Simplifying the Read Model
Using MediatR to Implement CQRS
Introducing a Separate Database for
Queries
Synchronizing the Commands and
Queries Databases
CQRS Best Practices and Misconceptions
Prerequisites

“Domain-Driven Design in Practice” by Vladimir Khorikov


CQRS and Its Origins
Command and Query Responsibility Segregation

Read model Write model

Reads

Writes
CQRS and Its Origins

Command and Query Responsibility Segregation


Greg Young, 2010

Command Query Separation


Bertrand Meyer

https://cqrs.files.wordpress.com/2010/11/cqrs_documents.p
df
CQS

Command–query Separation Principle

Command Query

Produces side-
Side-effect free
effects

Returns void Returns non-void


CQS Limitations

var stack = new Stack<string>();


stack.Push("value"); // Command
string value = stack.Pop(); // Both query and command

When result of a query can


become stale quickly
CQS Limitations

public bool FileExists(string path) public Result WriteToFile(


{ string path, string content)
return File.Exists(path); {
} try
{
public void WriteToFile( File.WriteAllText(path, content);
string path, string content) return Result.Ok();
{ }
if (!FileExists(path)) catch (FileNotFoundException)
throw new ArgumentException(); {
return Result.Fail("File not found");
File.WriteAllText(path, content); }
} }

Follows CQS Doesn’t follow CQS


CQRS and CQS

CQRS

Models,
classes

Methods CQS
CQRS and CQS

CQS CQRS

Method-command Command model

Method-query Query model


Why CQRS?

Scalability

Create Read Update Delete


Why CQRS?

Create

Update Read

Delete

Command side Query side

1 server 10 servers
Why CQRS?

Scalability

Performance
Why CQRS?

Write
API model
Client Create
Read
API
model

Database
Server
Cache Highly optimized
SQL queries
Why CQRS?

Scalability

Performance

Simplicity
Why CQRS?

Commands Queries

Changing data Reading data

Offloading complexity

SRP applied at the architectural level


CQRS is about optimizing
decisions for different
situations.
CQRS in the Real World

ORM for
writing

ADO.NET
for reading

CQRS
CQRS in the Real World

CQRS
Command Query Responsibility Segregation
(CQRS) originates from the Command Query
Separation Principle (CQS)
- CQRS extends CQS to the architectural
level
Summary - Split a unified domain model into two: for
commands and for queries

CQRS allows us to make different decisions


for reads and writes
- Better scalability
- Better performance
- Simpler code

CQRS is SRP applied at the architectural level


Examples of CQRS in the real world
In the Next Module

Introducing a Sample Project

You might also like