Top Banner
1 Understanding and Applying CQRS Vinicius Feitosa Pacheco @ViniciusPach 07.23.18
42

Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

Jun 23, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

1

Understanding andApplying CQRS

Vinicius Feitosa Pacheco

@ViniciusPach

07.23.18

Page 2: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

2

Microservice Patterns and Best PracticesVinicius Feitosa PachecoJanuary 2018

http://bit.ly/microservice-book

@ViniciusPach

Page 3: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

Agenda

3

1. What is CQRS?

2. When can we use CQRS ?

3. Understanding CQRS

4. Applying CQRS

5. Common Mistakes

Page 4: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

4

What is CQRS?1

Page 5: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

5

Command

Query

Responsibility

Segregation

Page 6: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

6

CQRS is simply the creation of two objects where there was previously only one. The separation occurs based upon whether the methods are a command or a query...

Young, Greg (2010)

Page 7: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

7

When can we use CQRS ?2

Page 8: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

8

The “Normal” Architecture

Page 9: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

9

Page 10: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

10

UI / API

Domain

Repository

Model

DB

Read Write

Page 11: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

11

App InstanceApp Instance App Instance App InstanceApp Instance

Page 12: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

12

First possible solution: Master/Slave

Page 13: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

13

UI / API

Domain

Repository

Model

Master SlaveSlave

Read Write

Eventual Consistency

Page 14: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

14

Second possible solution: Cache

Page 15: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

15

UI / API

Domain

Repository

Model

MasterCache

WriteRead

Data Synchronization

Page 16: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

16

When CQRS is util?

● When my storage is a bottleneck.

● When the application has complex queries and these

queries could be optimized.

● When a big number of users are updating a small data

set and the data could be outdated.

Page 17: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

17

Understanding CQRS3

Page 18: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

18

CQRS

● Query Stack – Operations that retrieve information from

the data in the application.

● Command Stack – Operations that modify the State of

the data in the application.

Page 19: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

QueryStack

3.1

Page 20: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

20

QueryStack

● It is simpler than the CommandStack.

● It is a synchronous layer that retrieves data from a

denormalized reading.

● Presumes “flat” queries.

Page 21: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

CommandStack

3.2

Page 22: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

22

CommandStack

● Potentially asynchronous.

● Has behavior-centric approach where all business intention is initially triggered by the

client as a use case.

● The Commands are declared in an imperative fashion and are raised

asynchronously.

● The handlers returns success or failure.

● A command updates the state of an entity and raises an event that will update the

data needed in the database reading.

Page 23: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

Synchronization

3.3

Page 24: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

24

Synchronization: Automatic updating

QueryStack

CommandStack

GET

POST/PUT...

Synchronous(REST/RPC...)

Page 25: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

25

Synchronization: Update possible

QueryStack

CommandStack

GET

POST/PUT...

Asynchronous(Bus/Queues...)Event

Page 26: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

26

Synchronization: Controlled update

QueryStack

CommandStack

GET

POST/PUT...

Asynchronous(Cron Process...)

Page 27: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

27

Synchronization: Update on demand

QueryStack

CommandStack

GET

POST/PUT... A

B

Asynchronous(Check-in Process)<

Page 28: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

Queueing

3.4

Page 29: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

29

Page 30: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

30

Applying CQRS4

Page 31: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

31

https://github.com/viniciusfeitosa/europython2018

Page 32: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

32

Page 33: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

33

Common Mistakes5

Page 34: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

34

Mistake 1:

CQRS and Event Sourcing must be

implemented together

Page 35: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

35

Mistake 2:

CQRS requires eventual consistency

Page 36: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

36

Mistake 3:

CQRS depends on Queues and Message

Brokers

Page 37: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

37

Mistake 4:

CQRS is easy

Page 38: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

38

Mistake 5:

CQRS is architecture

Page 39: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

39

CQRS is the best thing since sliced bread!

Pacheco, Vinicius (Today)

Page 40: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

40

Microservice Patterns and Best PracticesVinicius Feitosa PachecoJanuary 2018

http://bit.ly/microservice-book

@ViniciusPach

Page 41: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

41

May the CQRS be with you

Kenobi, Vinicius (2018)

Page 42: Applying CQRS 07.23.18 @ViniciusPach Understanding and … · 2018-08-05 · When CQRS is util? When my storage is a bottleneck. When the application has complex queries and these

42

Thank you