57
Change the Rules Rethink you architecture with CQRS

Rethink your architecture with CQRS

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Rethink your architecture with CQRS

Change the Rules

Rethink you architecture with

CQRS

Page 2: Rethink your architecture with CQRS

Change the Rules

INTRODUCTION

Who are those guys?

Page 3: Rethink your architecture with CQRS

Change the Rules

Page 4: Rethink your architecture with CQRS

Change the Rules

Page 5: Rethink your architecture with CQRS

Change the Rules

Agenda

• Introduction

• Current architectures & problems

• CQRS

• Food for thought

Page 6: Rethink your architecture with CQRS

Change the Rules

Page 7: Rethink your architecture with CQRS

Change the Rules

Huidige architecturen

bron: .NET Application Architecture Guide 2.0

Page 8: Rethink your architecture with CQRS

Change the Rules

Huidige architecturen Presentation layer

Application layer

Data layer

Page 9: Rethink your architecture with CQRS

Change the Rules

multi-tier

Presentation layer

Domain layer

Data layer

Page 10: Rethink your architecture with CQRS

Change the Rules

Presentation layer

Data layer

Domain layer

Request DTO

Map DTO

RequestDTO

Map DTO

Query data Query data

Update DTO

Map DTO

Query data Query data

RequestDTO

Page 11: Rethink your architecture with CQRS

Change the Rules

Presentation layer

Data layer

Domain layer Send 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

Update DTO

Map DTO

Query data Query data

RequestDTO

Page 12: Rethink your architecture with CQRS

Change the Rules

“A single model cannot be appropriate for reporting, searching, and transactional behaviors.” – Greg Young

Page 13: Rethink your architecture with CQRS

Change the Rules

“Doordat software processen kan automatiseren stelt het ons in staat deze verder op te schalen dan ooit tevoren.“

Page 14: Rethink your architecture with CQRS

Change the Rules

“Every method should either be a command that performs an action, or a query that returns data to the caller, but not both.” – Bertrand Meyer

Page 15: Rethink your architecture with CQRS

Change the Rules

Presentation layer

Domain layer

Data layer

Page 16: Rethink your architecture with CQRS

Change the Rules

Presentation layer

Domain layer Data layer

Page 17: Rethink your architecture with CQRS

Change the Rules

Presentation layer

Domain layer Data layer

Page 18: Rethink your architecture with CQRS

Change the Rules

Presentation layer

Domain layer Data layer

Read focus State transition focus

Page 19: Rethink your architecture with CQRS

Change the Rules

Presentation layer

Domain layer Data layer

Read focus State transition focus Queries Commands

Page 20: Rethink your architecture with CQRS

Change the Rules

Presentation layer

Domain layer Read layer

Read focus State transities focus Commands Queries

Page 21: Rethink your architecture with CQRS

Change the Rules

FROM CONCEPT TO ARCHITECTUUR

Here we go!

Page 22: Rethink your architecture with CQRS

Change the Rules

User interface

Page 23: Rethink your architecture with CQRS

Change the Rules

Queries

• Requests information

• Is context dependant

• Is executed many times

• Should be simple (select * from x where y)

• Examples:

– Get all products for user X

– Get total price of all orders of month May

Page 24: Rethink your architecture with CQRS

Change the Rules

User interface

Read side

Page 25: Rethink your architecture with CQRS

Change the Rules

Read side

User interface

Read databases

Page 26: Rethink your architecture with CQRS

Change the Rules

Read side

User interface

Read databases

Facade

Page 27: Rethink your architecture with CQRS

Change the Rules

Read side

User interface

Facade

Read databases

DTO’s

Page 28: Rethink your architecture with CQRS

Change the Rules

Read side

• Focus on data needs

• Optimized for reading

• Can contain multiple models if needed

• Can be denormalized

• Facades should be minimized

Page 29: Rethink your architecture with CQRS

Change the Rules

“State transitions are an important part of our problem space and should be modeled within our domain.” – Greg Young

Page 30: Rethink your architecture with CQRS

Change the Rules

Commands

• Causes a state transition

• Contains the intend of the user

• Exists in a bounded context

• Examples:

– AddProductToShoppingCart

– PurchaseOrder

– MoveUserToNewAddress

– CorrectAddressForUser

Page 31: Rethink your architecture with CQRS

Change the Rules

Commands example

Create Cart

Add item Add item Add item Remove

item Purchase

Page 32: Rethink your architecture with CQRS

Change the Rules

User interface

Commandhandlers

Read databases

Read side

commands DTO’s

Write side

Facade

Page 33: Rethink your architecture with CQRS

Change the Rules

Command handler sample

Page 34: Rethink your architecture with CQRS

Change the Rules

The domain

• Validates and handles commands

• Encapsulates behavior

• Build with rich object oriented techniques

Page 35: Rethink your architecture with CQRS

Change the Rules

User interface

Commandhandlers

Read databases

Read side

commands DTO’s

Do

mei

n

Write side

Repository

Facade

Page 36: Rethink your architecture with CQRS

Change the Rules

User interface

Commandhandlers

Read databases

Read side

commands DTO’s

Do

mei

n

Write side

Repository

?

Facade

Page 37: Rethink your architecture with CQRS

Change the Rules

Domain events

• Result of a command

• Represents a event

• Examples

– ProductAddedToShoppingCart

– OrderPurchased

– UserMovedToNewAddress

– AddressCorrectedForUser

Page 38: Rethink your architecture with CQRS

Change the Rules

Transitional flow

Page 39: Rethink your architecture with CQRS

Change the Rules

User interface

Commandhandlers

Read databases

Read side

commands DTO’s

Do

mei

n

Write side

Repository

?

Facade

Page 40: Rethink your architecture with CQRS

Change the Rules

User interface

Commandhandlers

Repository

Event b

us

Den

orm

aliz

ers

Read databases

Write side Read side

events

events events

commands DTO’s

Do

mei

n

Facade

Page 41: Rethink your architecture with CQRS

Change the Rules

Denormalizer sample

Page 42: Rethink your architecture with CQRS

Change the Rules

User interface

Commandhandlers

Repository

Event b

us

Den

orm

aliz

ers

Read databases

Write side Read side

events

events events

commands DTO’s

Do

mei

n

Event store

events

Facade

Page 43: Rethink your architecture with CQRS

Change the Rules

Event store

• Contains all domain events

• Does not need to be relational

• Contains the audit log

• Read models can (re)build themself of it

• The domain uses it to get the current state

Page 44: Rethink your architecture with CQRS

Change the Rules

queue

User interface

Commandhandlers

Repository

Event b

us

Den

orm

aliz

ers

Event store Read databases

Write side Read side

events

events

events events

commands DTO’s

Do

mei

n

Facade

Page 45: Rethink your architecture with CQRS

Change the Rules

Application flow

Page 46: Rethink your architecture with CQRS

Change the Rules

CQRS Wrapup

• The domain receives commands and

publishes domain events.

• All state changes are represented by

domain Events

• The read models are updated as a result of

the published Events

• All Queries go directly to the read models,

the domain model is not involved

Page 47: Rethink your architecture with CQRS

Change the Rules

Benefits

• Fully encapsulated domain that only

exposes behavior

Page 48: Rethink your architecture with CQRS

Change the Rules

Benefits

• Fully encapsulated domain that only

exposes behavior

• Keep users intent

Page 49: Rethink your architecture with CQRS

Change the Rules

Benefits

• Fully encapsulated domain that only

exposes behavior

• Keep users intent

• Bullet-proof auditing and historical tracing

Page 50: Rethink your architecture with CQRS

Change the Rules

Benefits

• Fully encapsulated domain that only

exposes behavior

• Keep users intent

• Bullet-proof auditing and historical tracing

• No object-relational impedance mismatch

Page 51: Rethink your architecture with CQRS

Change the Rules

Benefits

• Fully encapsulated domain that only

exposes behavior

• Keep users intent

• Bullet-proof auditing and historical tracing

• No object-relational impedance mismatch

• Optimized and multiple read models

Page 52: Rethink your architecture with CQRS

Change the Rules

Benefits

• Fully encapsulated domain that only

exposes behavior

• Keep users intent

• Bullet-proof auditing and historical tracing

• No object-relational impedance mismatch

• Optimized and multiple read models

• Testability

Page 53: Rethink your architecture with CQRS

Change the Rules

Benefits

• Fully encapsulated domain that only exposes behavior

• Keep users intent

• Bullet-proof auditing and historical tracing

• No object-relational impedance mismatch

• Optimized and multiple read models

• Testability

• Easy integration with external systems

Page 54: Rethink your architecture with CQRS

Change the Rules

Consequences

• Learning curve

• Overhead for simple CRUD

• Data duplication

• Introducing an new architecture has a big

impact

Page 55: Rethink your architecture with CQRS

Change the Rules

Food for thought

• Should your read model even contains a

relational database?

• What does this mean for requirement

engineering?

• What does this mean for outsourcing?

• What does this mean for DBA?

Page 56: Rethink your architecture with CQRS

Change the Rules

You’ve been a great audience!