CQRS innovations (English version)

Post on 14-May-2015

1,058 views 1 download

Tags:

transcript

April 12, 2023 www.ExigenServices.com

CQRS - Innovative solution for problems of modern enterprise architectures.

Andrey Lomakin (lomakin.andrey@gmail.com) Artem Orobets (enisher@gmail.com)

2 www.ExigenServices.com

Table of content

1. Why CQRS ?

a) Milestones of enterprise architecture history

b) Pitfalls of CRUD

c) CQRS as solution

2. CQRS internals

3. Axon framework as CQRS implementation

3 www.ExigenServices.com

Why CQRS ?

I. Why CQRS ?

4 www.ExigenServices.com

Lets talk about evolution of enterprise architecture

?

Back to the beginning

5 www.ExigenServices.com

Evolution of presentation of documents

Epoch of paper documents

6 www.ExigenServices.com

Evolution of presentation of documents

Epoch of electronic storages

7 www.ExigenServices.com

Epoch of electronic storages

Search in electronic storage

8 www.ExigenServices.com

Epoch of electronic storages

Epoch of CRUD architecture has came

9 www.ExigenServices.com

Evolution of data processing

Epoch of business processes

10 www.ExigenServices.com

CRUD approach is oriented on data manipulation, but not on business

logic modeling .

However…

11 www.ExigenServices.com

Pitfalls of CRUD

II. Pitfalls of CRUD

12 www.ExigenServices.com

Problem #1. Usage of JavaBeans ….

JavaBean – “Reusable software components that can be manipulated visually in a builder tool”.

13 www.ExigenServices.com

1. Broken encapsulation of business objects.

2. Worse code readability.3. Harder maintainability.4. All business logic

is placed in service methods.

… results in …

14 www.ExigenServices.com

Problem #2. Performance optimization and consequences.

Usage of ORM tools with denormalization dilutes

15 www.ExigenServices.com

Problem #3. Scalability

During business data processing we always chose C in CAP theorem

16 www.ExigenServices.com

Problem #4:In real life concurrent data modification conflicts do not exist.

Real life business processes do not have concurrent data modification conflicts.

Detection of such conflict is sign of flaw in implementation of business logic.

CRUD does not take this into account.

17 www.ExigenServices.com

CQRS as solution

III. CQRS as solution

18 www.ExigenServices.com

CQRS

CQRS - Command Query Responsibility Segregation

19 www.ExigenServices.com

Problem # 1: Usage of JavaBeans

JavaBeans are still used on query side onlybut JavaBean != Domain Entity.

20 www.ExigenServices.com

Problem #2. Performance optimization and consequences.

Data denormailzation is used on query side only

21 www.ExigenServices.com

Data on query side

Each table – reflection of presentation of data on user screen.

Database are not restricted to be SQL only.

Possible alternatives - Apache Cassandra, HBase, OrientDB ….

22 www.ExigenServices.com

Problem #3. Scalability

Data consistency is needed on business logic side only.

On query side eventual consistency is totally acceptable.

23 www.ExigenServices.com

Problem #4:In real life concurrent data modification conflicts do not exist.

1. In the form of values of variables inside object

Two approaches to present object state

24 www.ExigenServices.com

Two approaches to present object state

+11.2 M $ + 5.3 M $ - 8.2 M $

Sum: 8.3 M $

2. In form of event queue

25 www.ExigenServices.com

Two approaches to present object state

Each aggregate (DDD) – event container. There is no need to use relational

databases. It is preferable to have DB with ACID

properties.

26 www.ExigenServices.com

Two approaches to present object state

Advantages: System monitoring support. Ability rollback system state till any moment

in the past. Framework for data replication and conflict

resolution.

27 www.ExigenServices.com

Conflict resolution

Correct customer address

Customer reallocated

Conflict resolver

Customer reallocated

28 www.ExigenServices.com

Architecture of CQRS applications

IV. Architecture of CQRS applications

29 www.ExigenServices.com

CQRS – is approach only

CQRS is approach only, how it will be implemented, depends

on you.

31 www.ExigenServices.com

Queries

Multilayer architecture

DB query

CQRS

Conversion to domain model

Conversion to DTO

Transmutation of datato client

DB query

Conversion to domain model

Conversion to DTO

Transmutation of datato client

33 www.ExigenServices.com

Commands

Command – presentation of business action, action which is meaningful for end user.

Advantages of usage of commands:1. Orientation on business problems.2. Convenient facilities for monitoring and scalability.

36 www.ExigenServices.com

….advantages…..

1. Good preconditions for DDD usage.2. Usage of complex event processing

possibilities.3. Simplicity of distribution of responsibilities

between highly specialized commands.

37 www.ExigenServices.com

CQRS frameworks

V. CQRS frameworks

38 www.ExigenServices.com

Axon framework

Axon framework - most popular and feature rich.

39 www.ExigenServices.com

Example of CQRS application

Address Book – address list management

41 www.ExigenServices.com

Command creation and submission

43 www.ExigenServices.com

Command processing

44 www.ExigenServices.com

Business action

45 www.ExigenServices.com

Event processing on query side

47 www.ExigenServices.com

Query side

48 www.ExigenServices.com

Simplicity of configuration

49 www.ExigenServices.com

Axon – repository и event store.

Repository

Event Store

JPA

File System JPA Mongo DB

50 www.ExigenServices.com

Event Stores – pros and cons

There is no ACID support better performance

JPA Event Store

Transactional but slow

Mongo DB, File system

51 www.ExigenServices.com

Questions

52 www.ExigenServices.com

Authors

Artem Orobets. twitter: @Dr_EniSh , enisher@gmail.com, skype: dr_enish

Andrey Lomakin. twitter: @Andrey_Lomakin , lomakin.andrey@gmail.com , skype: lomakin_andrey

53 www.ExigenServices.com

Sources

1. First CQRS introduction http://www.infoq.com/presentations/greg-young-unshackle-qcon08

2. CQRS architecture overview - http://elegantcode.com/2009/11/11/cqrs-la-greg-young/

3. Greg Young blog - http://codebetter.com/gregyoung/

4. Race conditions does not exist http://www.udidahan.com/2010/08/31/race-conditions-dont-exist/

5. Domain Driven Design Aggregator - http://domaindrivendesign.org/

6. Axon framework home page - http://code.google.com/p/axonframework/

7. Mark Nijhof blog http://cre8ivethought.com/blog