SlideShare a Scribd company logo
1 of 33
CQRS
IN 4 STEPS
WHAT IS THE
COMMAND QUERY RESPONSIBILITY
SEGREGATION
What is not required in CQRS
• More than 1 data source
• Domain events
• Event Sourcing
• Message buses
• And others
0
RECENT STATE
Ecommerce app
ProductsServiceProductsController
OrdersServiceOrdersController
Ecommerce app - NopCommerce
Disadvantages of this approach
• Enormous controllers / services / helpers
• Classes are not following SRP
• Refactoring is really difficult
• Complexity in writing unit tests
1
CQRS IS TWO OBJECTS
WHERE THERE WAS ONCE ONE
Jimmy Bogard - https://lostechies.com/jimmybogard/2012/08/22/busting-some-cqrs-myths/
Martin Fowler - https://martinfowler.com/bliki/CQRS.html
Step 1 – Splitting to objects
GetProducts
QueryHandler
ProductsController
AddProduct
CommandHandler
Mediator
Jimmy Bogard - https://lostechies.com/jimmybogard/2015/05/05/cqrs-with-mediatr-and-automapper/
Profit after this step
• Single responsibility
• Clearly defined target and dependencies
• Split to change and receive data
• Simplicity of writing unit tests
2
BOOSTING
YOUR QUERIES
Many objects
GetProductsQueryHandle
r
ProductsController
AddProductCommandHandler
CategoriesController
GetProductsFieldsQueryHandler
GetCategoriesQueryHandle
r
AddCategoryCommandHandler
GetCategoriesProductsQueryHandle
r
Objects without change
AddProductCommandHandler
GetProductsFieldsQueryHandler
AddCategoryCommandHandler
Step 2 – Request optimization
GetProductsQueryHandler
GetCategoriesQueryHandler
GetCategoriesProductsQueryHandle
r
X
Plenty of options
SQL Views
Materialized
AutoMapper
ProjectTo
Dapper
MicroORM
EF No tracking
OrmLite
MicroORM
SQL Queries
Profit after this step
• Improved speed of receiving data
• No impact on other parts of the system
3
CREATING SYNCHRONOUS
READMODEL
Publishing domain events
ChangeProductField
CommandHandler
Mediator
AddProduct
CommandHandler
Mediator
ProductAdded
ProductFieldChanged
Udi Dahan - http://udidahan.com/2009/06/14/domain-events-salvation/
Step 3 – Synchronous read model
ProductAdded
ProductFieldChanged
Product
View
Handler
ProductView
(table)
Profit after this step
• Iterative gain in read model
• Immediate queries
• No data loss
• Ability to recreate a view
4
CREATING ASYNCHRONOUS
READMODEL
Message bus
ProductAdded
ProductFieldChanged
Async
Job
Step 4 – Asynchronous read model
Product
Search
Handler
Async
Job
Product
Cache
Handler
Notify the user - SignalR
Product
Search
Handler
Async
Job
Products
Controller
SignalR
Correlation Identifier - http://www.enterpriseintegrationpatterns.com/patterns/messaging/CorrelationIdentifier.html
Profit after this step
• Read model per functionality
• Non-blockable commands
• Small, testable components
Summary
• 1 – split to objects: Command & Query
• 2 – query optimization
• 3 – synchronous read model
• 4 – asynchronous read model & notifications
THANK YOU

More Related Content

What's hot

BDD testing with cucumber
BDD testing with cucumberBDD testing with cucumber
BDD testing with cucumberDaniel Kummer
 
Xray for Jira - Overview
Xray for Jira - OverviewXray for Jira - Overview
Xray for Jira - OverviewXpand IT
 
Getting started with karate dsl
Getting started with karate dslGetting started with karate dsl
Getting started with karate dslKnoldus Inc.
 
Agile_Jira_Presentation_1.pptx
Agile_Jira_Presentation_1.pptxAgile_Jira_Presentation_1.pptx
Agile_Jira_Presentation_1.pptxknowworld
 
Jira in action
Jira in actionJira in action
Jira in actionTan Tran
 
Successfully Implementing BDD in an Agile World
Successfully Implementing BDD in an Agile WorldSuccessfully Implementing BDD in an Agile World
Successfully Implementing BDD in an Agile WorldSmartBear
 
API Testing with Open Source Code and Cucumber
API Testing with Open Source Code and CucumberAPI Testing with Open Source Code and Cucumber
API Testing with Open Source Code and CucumberSmartBear
 
Introduction To Jira Slide Share
Introduction To Jira Slide ShareIntroduction To Jira Slide Share
Introduction To Jira Slide ShareRenjith V
 
Introduction to JIRA
Introduction to JIRAIntroduction to JIRA
Introduction to JIRARozi khan
 
WebSocket MicroService vs. REST Microservice
WebSocket MicroService vs. REST MicroserviceWebSocket MicroService vs. REST Microservice
WebSocket MicroService vs. REST MicroserviceRick Hightower
 
Prometheus: What is is, what is new, what is coming
Prometheus: What is is, what is new, what is comingPrometheus: What is is, what is new, what is coming
Prometheus: What is is, what is new, what is comingJulien Pivotto
 
Use of Jira Confluence as Project Management Tool
Use of Jira Confluence as Project Management ToolUse of Jira Confluence as Project Management Tool
Use of Jira Confluence as Project Management ToolJipin Nakarmi
 
Entity relationship Diagram for Online buy and Sale Project
Entity relationship Diagram for Online buy and Sale ProjectEntity relationship Diagram for Online buy and Sale Project
Entity relationship Diagram for Online buy and Sale ProjectNaimul Arif
 

What's hot (20)

Code Review
Code ReviewCode Review
Code Review
 
BDD testing with cucumber
BDD testing with cucumberBDD testing with cucumber
BDD testing with cucumber
 
Xray for Jira - Overview
Xray for Jira - OverviewXray for Jira - Overview
Xray for Jira - Overview
 
Jira Training
Jira TrainingJira Training
Jira Training
 
Getting started with karate dsl
Getting started with karate dslGetting started with karate dsl
Getting started with karate dsl
 
Agile_Jira_Presentation_1.pptx
Agile_Jira_Presentation_1.pptxAgile_Jira_Presentation_1.pptx
Agile_Jira_Presentation_1.pptx
 
Jira in action
Jira in actionJira in action
Jira in action
 
Test Automation Framework with BDD and Cucumber
Test Automation Framework with BDD and CucumberTest Automation Framework with BDD and Cucumber
Test Automation Framework with BDD and Cucumber
 
Successfully Implementing BDD in an Agile World
Successfully Implementing BDD in an Agile WorldSuccessfully Implementing BDD in an Agile World
Successfully Implementing BDD in an Agile World
 
API Testing with Open Source Code and Cucumber
API Testing with Open Source Code and CucumberAPI Testing with Open Source Code and Cucumber
API Testing with Open Source Code and Cucumber
 
Introduction To Jira Slide Share
Introduction To Jira Slide ShareIntroduction To Jira Slide Share
Introduction To Jira Slide Share
 
Introduction to JIRA
Introduction to JIRAIntroduction to JIRA
Introduction to JIRA
 
WebSocket MicroService vs. REST Microservice
WebSocket MicroService vs. REST MicroserviceWebSocket MicroService vs. REST Microservice
WebSocket MicroService vs. REST Microservice
 
User stories in agile software development
User stories in agile software developmentUser stories in agile software development
User stories in agile software development
 
Prometheus: What is is, what is new, what is coming
Prometheus: What is is, what is new, what is comingPrometheus: What is is, what is new, what is coming
Prometheus: What is is, what is new, what is coming
 
BDD with Cucumber
BDD with CucumberBDD with Cucumber
BDD with Cucumber
 
Use of Jira Confluence as Project Management Tool
Use of Jira Confluence as Project Management ToolUse of Jira Confluence as Project Management Tool
Use of Jira Confluence as Project Management Tool
 
Graphql
GraphqlGraphql
Graphql
 
Entity relationship Diagram for Online buy and Sale Project
Entity relationship Diagram for Online buy and Sale ProjectEntity relationship Diagram for Online buy and Sale Project
Entity relationship Diagram for Online buy and Sale Project
 
JIRA
JIRAJIRA
JIRA
 

Similar to CQRS in 4 steps - Lightning Talk

Continuous Performance Testing
Continuous Performance TestingContinuous Performance Testing
Continuous Performance TestingGrid Dynamics
 
quality center.ppt
quality center.pptquality center.ppt
quality center.pptAnilKumarARS
 
From Zero to Performance Hero in Minutes - Agile Testing Days 2014 Potsdam
From Zero to Performance Hero in Minutes - Agile Testing Days 2014 PotsdamFrom Zero to Performance Hero in Minutes - Agile Testing Days 2014 Potsdam
From Zero to Performance Hero in Minutes - Agile Testing Days 2014 PotsdamAndreas Grabner
 
Collaboration for Executives
Collaboration for ExecutivesCollaboration for Executives
Collaboration for Executivescolleenfry
 
Overcoming Barriers to the Cloud
Overcoming Barriers to the Cloud Overcoming Barriers to the Cloud
Overcoming Barriers to the Cloud Andy Milsark
 
[TDC 2018 - Trilha XP] Feature flags e releases controlados
[TDC 2018 - Trilha XP] Feature flags e releases controlados[TDC 2018 - Trilha XP] Feature flags e releases controlados
[TDC 2018 - Trilha XP] Feature flags e releases controladosEduardo Colabardini
 
TDC2018SP | Trilha UX - Feature flags e Releases controlados
TDC2018SP | Trilha UX - Feature flags e Releases controladosTDC2018SP | Trilha UX - Feature flags e Releases controlados
TDC2018SP | Trilha UX - Feature flags e Releases controladostdc-globalcode
 
Building real-time data analytics on Google Cloud
Building real-time data analytics on Google CloudBuilding real-time data analytics on Google Cloud
Building real-time data analytics on Google CloudJonny Daenen
 
Jagger: Сервер непрерывного тестирования производительности
Jagger: Сервер непрерывного тестирования производительностиJagger: Сервер непрерывного тестирования производительности
Jagger: Сервер непрерывного тестирования производительностиSQALab
 
Using Q-Pulse to Manage Maintenance & Errors
Using Q-Pulse to Manage Maintenance & ErrorsUsing Q-Pulse to Manage Maintenance & Errors
Using Q-Pulse to Manage Maintenance & ErrorsFionaMacGael
 
Mobile apps that win
Mobile apps that winMobile apps that win
Mobile apps that winDellFoglight
 
Observability with Spring-based distributed systems
Observability with Spring-based distributed systemsObservability with Spring-based distributed systems
Observability with Spring-based distributed systemsRakuten Group, Inc.
 
Design Reviews for Operations - Velocity Europe 2014
Design Reviews for Operations - Velocity Europe 2014Design Reviews for Operations - Velocity Europe 2014
Design Reviews for Operations - Velocity Europe 2014Mandi Walls
 
Yasgui: not just another sparql gui
Yasgui: not just another sparql guiYasgui: not just another sparql gui
Yasgui: not just another sparql guilaurensrietveld
 
The Top 5 Practices of a Highly Successful ChangeMan ZMF Administrator
The Top 5 Practices of a Highly Successful ChangeMan ZMF AdministratorThe Top 5 Practices of a Highly Successful ChangeMan ZMF Administrator
The Top 5 Practices of a Highly Successful ChangeMan ZMF AdministratorSerena Software
 
Tools and techniques for APIs
Tools and techniques for APIsTools and techniques for APIs
Tools and techniques for APIsJason Harmon
 
New Tech for Project Managers
New Tech for Project ManagersNew Tech for Project Managers
New Tech for Project ManagersPratip Mallik
 

Similar to CQRS in 4 steps - Lightning Talk (20)

Continuous Performance Testing
Continuous Performance TestingContinuous Performance Testing
Continuous Performance Testing
 
quality center.ppt
quality center.pptquality center.ppt
quality center.ppt
 
From Zero to Performance Hero in Minutes - Agile Testing Days 2014 Potsdam
From Zero to Performance Hero in Minutes - Agile Testing Days 2014 PotsdamFrom Zero to Performance Hero in Minutes - Agile Testing Days 2014 Potsdam
From Zero to Performance Hero in Minutes - Agile Testing Days 2014 Potsdam
 
Collaboration for Executives
Collaboration for ExecutivesCollaboration for Executives
Collaboration for Executives
 
Agile & Wakanda
Agile & WakandaAgile & Wakanda
Agile & Wakanda
 
Overcoming Barriers to the Cloud
Overcoming Barriers to the Cloud Overcoming Barriers to the Cloud
Overcoming Barriers to the Cloud
 
[TDC 2018 - Trilha XP] Feature flags e releases controlados
[TDC 2018 - Trilha XP] Feature flags e releases controlados[TDC 2018 - Trilha XP] Feature flags e releases controlados
[TDC 2018 - Trilha XP] Feature flags e releases controlados
 
TDC2018SP | Trilha UX - Feature flags e Releases controlados
TDC2018SP | Trilha UX - Feature flags e Releases controladosTDC2018SP | Trilha UX - Feature flags e Releases controlados
TDC2018SP | Trilha UX - Feature flags e Releases controlados
 
Building real-time data analytics on Google Cloud
Building real-time data analytics on Google CloudBuilding real-time data analytics on Google Cloud
Building real-time data analytics on Google Cloud
 
Jagger: Сервер непрерывного тестирования производительности
Jagger: Сервер непрерывного тестирования производительностиJagger: Сервер непрерывного тестирования производительности
Jagger: Сервер непрерывного тестирования производительности
 
The Continuous PHP Pipeline
The Continuous PHP PipelineThe Continuous PHP Pipeline
The Continuous PHP Pipeline
 
Using Q-Pulse to Manage Maintenance & Errors
Using Q-Pulse to Manage Maintenance & ErrorsUsing Q-Pulse to Manage Maintenance & Errors
Using Q-Pulse to Manage Maintenance & Errors
 
Mobile apps that win
Mobile apps that winMobile apps that win
Mobile apps that win
 
ADF Performance Monitor
ADF Performance MonitorADF Performance Monitor
ADF Performance Monitor
 
Observability with Spring-based distributed systems
Observability with Spring-based distributed systemsObservability with Spring-based distributed systems
Observability with Spring-based distributed systems
 
Design Reviews for Operations - Velocity Europe 2014
Design Reviews for Operations - Velocity Europe 2014Design Reviews for Operations - Velocity Europe 2014
Design Reviews for Operations - Velocity Europe 2014
 
Yasgui: not just another sparql gui
Yasgui: not just another sparql guiYasgui: not just another sparql gui
Yasgui: not just another sparql gui
 
The Top 5 Practices of a Highly Successful ChangeMan ZMF Administrator
The Top 5 Practices of a Highly Successful ChangeMan ZMF AdministratorThe Top 5 Practices of a Highly Successful ChangeMan ZMF Administrator
The Top 5 Practices of a Highly Successful ChangeMan ZMF Administrator
 
Tools and techniques for APIs
Tools and techniques for APIsTools and techniques for APIs
Tools and techniques for APIs
 
New Tech for Project Managers
New Tech for Project ManagersNew Tech for Project Managers
New Tech for Project Managers
 

Recently uploaded

Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Challengers I Told Ya ShirtChallengers I Told Ya Shirt
Challengers I Told Ya ShirtChallengers I Told Ya ShirtChallengers I Told Ya ShirtChallengers I Told Ya Shirt
Challengers I Told Ya ShirtChallengers I Told Ya Shirtrahman018755
 
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls KolkataLow Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxAWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxellan12
 
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607dollysharma2066
 
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)Dana Luther
 
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...Sheetaleventcompany
 
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...Diya Sharma
 
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024APNIC
 
How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)Damian Radcliffe
 
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With RoomVIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Roomgirls4nights
 
VIP Kolkata Call Girl Kestopur 👉 8250192130 Available With Room
VIP Kolkata Call Girl Kestopur 👉 8250192130  Available With RoomVIP Kolkata Call Girl Kestopur 👉 8250192130  Available With Room
VIP Kolkata Call Girl Kestopur 👉 8250192130 Available With Roomdivyansh0kumar0
 
VIP Call Girls Pune Madhuri 8617697112 Independent Escort Service Pune
VIP Call Girls Pune Madhuri 8617697112 Independent Escort Service PuneVIP Call Girls Pune Madhuri 8617697112 Independent Escort Service Pune
VIP Call Girls Pune Madhuri 8617697112 Independent Escort Service PuneCall girls in Ahmedabad High profile
 
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts servicevipmodelshub1
 

Recently uploaded (20)

Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Challengers I Told Ya ShirtChallengers I Told Ya Shirt
Challengers I Told Ya ShirtChallengers I Told Ya ShirtChallengers I Told Ya ShirtChallengers I Told Ya Shirt
Challengers I Told Ya ShirtChallengers I Told Ya Shirt
 
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls KolkataLow Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxAWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
 
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
 
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝
 
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
 
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
 
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
 
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
 
How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)
 
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With RoomVIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
 
VIP Kolkata Call Girl Kestopur 👉 8250192130 Available With Room
VIP Kolkata Call Girl Kestopur 👉 8250192130  Available With RoomVIP Kolkata Call Girl Kestopur 👉 8250192130  Available With Room
VIP Kolkata Call Girl Kestopur 👉 8250192130 Available With Room
 
Model Call Girl in Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in  Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in  Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
 
VIP Call Girls Pune Madhuri 8617697112 Independent Escort Service Pune
VIP Call Girls Pune Madhuri 8617697112 Independent Escort Service PuneVIP Call Girls Pune Madhuri 8617697112 Independent Escort Service Pune
VIP Call Girls Pune Madhuri 8617697112 Independent Escort Service Pune
 
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
 
Call Girls In South Ex 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In South Ex 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICECall Girls In South Ex 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In South Ex 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
 
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No AdvanceRohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
 

CQRS in 4 steps - Lightning Talk

Editor's Notes

  1. There is a lot of noise about CQRS. When you search through the Internet to look for information and implementation of this pattern you will find enormous number of arcticles. And it's really hard to start with CQRS when you read that without X Y Z you shouldn't start with CQRS. Or without A B C it's not CQRS but only some silly tries. And I deeply refuse these ideas. It's really great to talk about these additions but at the beginning of the project it's really hard to plan that we will need these technologies. Due to this problem a lot of people refuses to interest about CQRS, so finally we are following the same mistakes over and over. For me CQRS is “Segregate operations that read data from operations that update data by using separate interfaces. This can maximize performance, scalability, and security.” – from Microsoft tutorial page.
  2. To begin with CQRS you don't need any of these stuff: - you can start with single data source and add  - your code can be a simple procedure without domain side-effects - event sourcing is a great pattern but not needed in CQRS - async message buses are not needed as well
  3. But to show you the power of CQRS lets start with an example of application that everybody would know.
  4. Let's assume that you create an ecommerce app with your loved ORM – e.g. Entity Framework. You add layer of services which handle requests from controllers. In there you put validations, requests to database, handling business logic etc. Week after week, month after month, your application starts growing. And it starts looking as:
  5. Maybe you are familiar with the best open-source .NET ecommerce solutution. It's really great peace of product, that you can use to build your own store, with plenty of functionalities. But unfortunately it is built in a typical layered way, that is really hard to mantain in such long-term and complicated projects.
  6. Here are the examples:  OrderProcessingService – 187 times changed, 41 injected dependencies Product Service – 173 times changed,, 27 injected dependencies Everything that is somehow connected with orders or products is moved to these services. With such architecture you finish with god objects responsible for handling almost each request from particular domain.
  7. So this approach is not fitting great in more complex applications: - god objects with dependencies difficult to understand  - classes are responsible for multiple action - it's really hard to split such objects or change their dependencies - with need to mock multiple object and handle their behaviour writing unit tests is almost impossible or such unit tests do not add value to your application So what we can do now?
  8. This quote is from Bogard's post, by which was a big inspiration for me. So you don't really needs to do something difficult to implement CQRS in your projects – only thing is a split from object which changes and receives data to two different one's.
  9. Moreover, Martin Fowler was writing about CQRS showing that to introduce this pattern to your app you need to split for model to command and queries:
  10. So step by step, method by method, you divide your ProductService, which handles requests from ProductController, to two or more objects which handle particular actions. Action which changes data is called command and action which receives data is called query. Controller is using some implementation of dispatcher pattern (check Mediator library created by Jimmy Bogard) to publish these commands and queries. They are handled in appriopriate connected handlers. And this is it, you have made a first step and implemented elements of CQRS in your projects. This is it – you can even stop at this point and call this CQRS – your actions are clearly defined and by definition you would know which object is changing data and which is just receiving it.
  11. It may seem as simple and straigthforward step, but it delivers great value to your application: - your object are now separated and you know what is their responsibility - you know why this object was created and what are its connections to other part of your application - you can easily find elements which changes data and focus on them when something is modifying your database - writing unit tests, with clearly defined output and narrowed number of dependecies, is much easier and gives more value To sum it up, it is much easier to develop and maintain your application if it is divided into small, separate part.
  12. Of course this step is not enhancing performance of your application. So in some point you get to the point when your data querying is too time consuming and is not accepted by the end user. And of course, your best ORM is responsible for it. So what you can do in that point?
  13. You focus on improving performance of your queries, with advantages taken from first step to CQRS – splitting to commands and queries.
  14. So in that point you got some controllers handling requests by divided handlers. And you see that some of your handlers are struggling from using Entity Framework. 
  15. But not all handlers needs our attention. A lot of handlers work really well with our ORM – data queried there is relatively small and query is generated in a moment. In that case you do not need to change anything.
  16. But for some handlers, with difficult queries are too slow for Entity Framework and you have to do something with it. And because you have strictly separated queries and command it's really easy to focus only on these requiring some attention. So you change a way as application connects to database and queries data to better fitted one – with better performance, quicker SQL query generation, less memory needed.
  17. There are plenty of options to achieve such improvement: – you can use No tracking option in EF not to track data changes - SQL Queries and SQL Views are more detailed option to query data, fastest but hardest to maintain as well - you can use one of many micro orms that are not so heavyweight as used one - in .NET there is an option to use Automapper library and extension called ProjectTo to query data on basis of created maps. It's really fast and easy to introduct to your project
  18. By that point you gained big performance improvement but without impact on other parts of the system.
  19. Your system grows and you reach to the point where even how hard you try to improve performance of querying your data, you cannot achieve it. In some situations data is structured in a way that unables you to query it effectively – you need to make multiple unions, joins that makes querying slow on big amount of data. So what you can do in this situation?
  20. In that case you can create additional model which store your data in a way that will be able to be queried fast and without overheads.
  21. First you need to publish domain events – you can use previously created / used dispatcher. At the end of your handler you add a logic that raises event about action done.
  22. Your events are synchronously handled by event handler which knows how to flatten your data to be quickly queried by your application. And puts this data to your database, the same as you currently using. But what is really important is that command handler and event handler is being runned in one transaction, so if adding data to ProductView fails application will also rollback previous operation. You are still using your current database, but part of your data is duplicated to be able to handle different query scenario.
  23. This step solves few problems: - data, which is hard to query, then command by command, is being transfered to simple read model - your model is optimized to be queried really fast - due to synchronous hadling and database transaction you don't lose your data - at any level of your application you can recreate your view and add handle new commands
  24. Unfortunately synchronous model of handling events has one major disadvantage – is synchronous. With many time-consuming event handlers your application freezes until all actions are done. 
  25. So you need to introduce asynchronous read model.
  26. At the beginning you need to change way of handling your events – instead of handling events immidietaly you put your events to message bus. Then some async job, which is connected to the bus, will be notified about new event to be handled.
  27. This async job runs event handlers which moves data to recent or new database. In your store you push product information to Elastic Search to search it efficiently and to Redis to cache data and use it in your queries.
  28. In many cases your end-user will require to get the info when the handler pushing data to async data store finished its work. In that case you can use broadly described pattern called Correlation identifier and great library called SignalR. Every request from end user contains unique CorrelationId. It is being transported through every layer as value in Command and Events to have an ability to find which particular user caused our change. In that case you will notify this user by push notification, so he can query the data from our new model, just after pushing data into it.
  29. Advantages of this step are pretty clear: - you can create any read model, that you will require in current situation - because events are handled asynchronously your command handling is very fast - you end with many different but tiny object which are easy to test and maintain Of course such step does not come without disadvantages, the bigest one are: - you implemented eventual consistency pattern with all its consequences - you need to handle all scenarios where command is done but event fails  - monitoring and debugging of your application is now much more complex than before
  30. Ok, so to sum it all:
  31. Path to CQRS, in my case, contains 4 steps: - splitting objects to command and queries - optimization places which requires such change - creating synchronous read model - creating asynchronous read model What is really important, in different bounded context you can implement these steps in whole or only a part of them, depending of the needs and the use cases. Even more, nothing prevents you to have two different models in theoretically similar queries or commands – it all depends on how complex your application is and what are the needs of your end-user.