Rethink your architecture with CQRS

60
Rethink your architecture with CQRS Pieter Joost van de Sande Passionated software developer, Atos Email: [email protected] @twitter: pjvds

description

Rethink your architecture with CQRS. Pieter Joost van de Sande Passionated software developer , Atos. E mail: [email protected] @ twitter : pjvds. This is meant to be a take away!. Architecture. Architecture. Presentation. Domain. Data. Presentation. Why do we love it?. Domain. - PowerPoint PPT Presentation

Transcript of Rethink your architecture with CQRS

Page 1: Rethink your architecture with  CQRS

Rethink your architecturewith CQRS

Pieter Joost van de SandePassionated software developer, Atos

Email: [email protected]@twitter: pjvds

Page 2: Rethink your architecture with  CQRS

This is meant to be a take away!

Page 3: Rethink your architecture with  CQRS

Architecture

Page 4: Rethink your architecture with  CQRS

Architecture

Presentation

Domain

Data

Page 5: Rethink your architecture with  CQRS

Presentation

Domain

Data

Why do we love it?

Page 6: Rethink your architecture with  CQRS

model

Domain Driven DesignEric Evans - 2003

Page 7: Rethink your architecture with  CQRS

The value of a structured Model

Page 8: Rethink your architecture with  CQRS

Who believes in this?

Page 9: Rethink your architecture with  CQRS

Structured models are always created from a single viewpoint!

Page 10: Rethink your architecture with  CQRS
Page 11: Rethink your architecture with  CQRS
Page 12: Rethink your architecture with  CQRS
Page 13: Rethink your architecture with  CQRS
Page 14: Rethink your architecture with  CQRS
Page 15: Rethink your architecture with  CQRS

A model can only be optimized for one thing… and one thing only!

Page 16: Rethink your architecture with  CQRS

Presentation layer

Data layer

Domain layerSend DTO

Send DTO

Map DTO

Map DTO

Write data Write data

Map DTO

Write data Write data

Send DTO

Request DTO

Map DTO

RequestDTO

Map DTO

Query data Query data

Request data

Map DTO

Query data Query data

RequestDTO

Send updateUpdate screen

Page 17: Rethink your architecture with  CQRS

TWO FUNDAMENTALS

Page 18: Rethink your architecture with  CQRS

Information

Page 19: Rethink your architecture with  CQRS

Transitions

Page 20: Rethink your architecture with  CQRS

WRITING != READING

Page 21: Rethink your architecture with  CQRS

From 3-tier to CQRS

Presentation layer

Domain layer

Data layer

Page 22: Rethink your architecture with  CQRS

Presentation layer

Domain layerData layer

Just re-order the tiers

Page 23: Rethink your architecture with  CQRS

Presentation layer

Domain layerData layer

Switch dependency

Page 24: Rethink your architecture with  CQRS

Presentation layer

Domain layerData layer

CQRS!

Query side Command side

Page 25: Rethink your architecture with  CQRS

FROM CONCEPT TO ARCHITECTURE

HERE WE GO!

Page 26: Rethink your architecture with  CQRS

User interface

Page 27: Rethink your architecture with  CQRS

Demand for information

Page 28: Rethink your architecture with  CQRS

User interface

Read side

Page 29: Rethink your architecture with  CQRS

We read a lot

Page 30: Rethink your architecture with  CQRS

We have multiple screens to fill

Page 31: Rethink your architecture with  CQRS

Data should be close to us

Page 32: Rethink your architecture with  CQRS

Relational databases are sub optimal for view data!

Page 33: Rethink your architecture with  CQRS

SELECT CCUS.CUST_FIRST_NAME , CCUS.CUST_LAST_NAME , CINT.CUST_INTEREST_RANK , CILO.CUST_INTERSTFROM CUST_CUSTOMER AS CCUS , CUST_INTEREST_LOOKUP AS CILO , CUST_INTEREST AS CINTWHERE ( CCUS.CUST_CITY = 'Singapore' AND CCUS.CUST_PROV_STATE = 'Singapore' AND CCUS.CUST_CODE IN ( SELECT COHE.CUST_CODE FROM CUST_ORDER_HEADER AS COHE , CUST_ORDER_STATUS AS COST WHERE ( COHE.CUST_ORDER_DATE >='2009-01-01 00:00:00.001' AND COST.CUST_ORDER_STATUS IN( 'Shipped', 'Back-ordered', 'In-process' ) AND COHE.CUST_ORDER_STATUS_CODE = COST.CUST_ORDER_STATUS_CODE ) ) AND CCUS.CUST_CODE = CINT.CUST_CODE AND CINT.CUST_INTEREST_CODE = CILO.CUST_INTEREST_CODE )ORDER BY CCUS.CUST_LAST_NAME ASC , CCUS.CUST_FIRST_NAME ASC , CINT.CUST_INTEREST_RANK ASC

Evil execution plan!

Page 34: Rethink your architecture with  CQRS

Bottom line

• Getting data should be simple – select * from x where y

• Examples:– Get all products of user X– Get total price of all orders of month May

Page 35: Rethink your architecture with  CQRS

User interface

Read side

Page 36: Rethink your architecture with  CQRS

Read side

User interface

Read databases

Page 37: Rethink your architecture with  CQRS

Read side

User interface

Read databases

Facade

Page 38: Rethink your architecture with  CQRS

Read side

User interface

Facade

Read databases

DTO’s

Page 39: Rethink your architecture with  CQRS

Demand for change

Page 40: Rethink your architecture with  CQRS

I want to tell the system something

Page 41: Rethink your architecture with  CQRS

Read side

User interface

Facade

Read databases

DTO’s

Write side

Page 42: Rethink your architecture with  CQRS

User interface

Commandhandlers

Read databases

Read side

commands DTO’s

Write side

Facade

Page 43: Rethink your architecture with  CQRS

Commands• Simple message contains all info needed• Lower coupling (law of demeter)• Causes a state transition• Contains the intend of the user• Exists in a bounded context

• Examples:– AddProductToShoppingCart– PurchaseOrder– MoveUserToNewAddress– CorrectAddressForUser– FinalizeOrder

Page 44: Rethink your architecture with  CQRS

Commands example

Create Cart

Add item

Add item

Add item

Remove item

Purchase

Page 45: Rethink your architecture with  CQRS

Users don’t make changes for nothing, intent is important!

Page 46: Rethink your architecture with  CQRS

Change has intent

• Users don’t make changes for nothing• This intent has value!

Correct the address for

user X

Customer X moved to

new addressVS.

Page 47: Rethink your architecture with  CQRS

User interface

Commandhandlers

Read databases

Read side

commands DTO’s

Write side

Facade

Page 48: Rethink your architecture with  CQRS

User interface

Commandhandlers

Read databases

Read side

commands DTO’s

Dom

ein

Write side

Repository

Facade

Page 49: Rethink your architecture with  CQRS

Event Sourcing

Page 50: Rethink your architecture with  CQRS

Events

• Simple message that describes an event• Contains intent• Contains all the data related to the event• Past tense

CustomerMoved{

CustomerId,NewAddress

}

Page 51: Rethink your architecture with  CQRS

Transitional flow

Page 52: Rethink your architecture with  CQRS

User interface

Commandhandlers

Read databases

Read side

commands DTO’s

Dom

ein

Write side

Repository

Facade

Page 53: Rethink your architecture with  CQRS

User interface

Commandhandlers

Read databases

Read side

commands DTO’s

Dom

ein

Write side

Repository

Facade

Page 54: Rethink your architecture with  CQRS

Event store

• Contains all domain events• Does not need to be relational• Write are *awesomely* fast!• Rock solid audit log• Read models can (re)build themself of it• The domain uses it to get the current state

• Can be used to build any structural model you want!

Page 55: Rethink your architecture with  CQRS

User interface

Commandhandlers

Read databases

Read side

commands DTO’s

Dom

ein

Write side

Repository

?

Facade

Page 56: Rethink your architecture with  CQRS

User interface

Commandhandlers

Repository

Event bus

Read databases

Write side Read side

eventscommands DTO’s

Dom

ein

Facade

Page 57: Rethink your architecture with  CQRS

User interface

Commandhandlers

Repository

Event bus

Deno

rmal

izers

Read databases

Write side Read side

events

events events

commands DTO’s

Dom

ein

Facade

Page 58: Rethink your architecture with  CQRS

Application flow

Page 59: Rethink your architecture with  CQRS

Wrap up

• Optimize for reading and writing• Makes changes part of the problem domain• Free rock solid audit log• Capture the intent of changes• Allow multiple read models

Page 60: Rethink your architecture with  CQRS

Thank you!You’ve been a great audience!