7 Eylül 2020 Pazartesi

Command Query Responsibility Segregation - CQRS

CQRS Nedir
Açıklaması şöyle.
Command-Query Responsibility Segregation is a pattern that tells us to separate operations that mutate data from the ones that query it. It is derived from Command Query Separation (CQS) principle devised by Bertrand Mayer – author of Eiffel.
Bertrand Meyer tarafından verilen açıklama şöyle. İlk olarak 1998 yılındaki kitabında bahsetmiş.
Every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, Asking a question should not change the answer.”
CQRS'ten Önce
Açıklaması şöyle. Şu anda Datawarehouse için Google Cloud'da BigQuery, Amazon'da ise Redshift kullanılıyor.
Note that before term CQRS was coined, Reporting Database via Datawarehouse and Analytics products were still being used, which in a way, followed the core theme of CQRS, to decouple reads and queries. 
Popüler Hale Gelmesi
CQRS'in popüler hale gelmesini sağlayan kişi Greg Young isimli kişi.

CQRS ve Domain Driven Design (DDD)
Açıklaması şöyle.
CQRS is more of a stepping stone and you have to put it in its historical context. It was a natural consequence of what was happening with Domain-Driven Design at that time. CQRS is not an end goal, it should be seen as a stepping stone for going to an event sourced system.
Nerede Kullanılmalı
Açıklaması şöyle.
You should be cautious however not to use it everywhere and only in some Bounded Contexts that need it, as everyone agrees that applying the CQRS principle adds complexity.
CQRS Bir Mimari Türü Değil
Açıklaması şöyle.
Some people see CQRS as a full-blown architecture, but it’s not. This is wrong. CQRS and event sourcing is not a top level architecture. You cannot build an Event Sourced system. Instead, you end up into building a monolith which is event sourced internally. Event sourcing is simply not a good solution to every problem. 
Detaylar
En temel seviyede kodun iki kısma bölünmesini söyler.
1. Mutating kod
2. Querying kod

Dolayısıyla kod şu hale geliyor.
1. XCommandService - change(),terminate() metodları burada
2. XQueryService - find(),get(),search() metodları burada

Aslında CQRS 4 olgunluk seviyesine bölünebilir. Bunlar şöyle
CQRS Maturity Level
As I understand CQRS and practice it, I usually distinguish four levels of maturity

1. CQS: Command and Query Segregation
2. Read and Write model
3. Read and Write Datastores
4. Event sourcing
Level 1: CQS: Command and Query Segregation ve Level 2: Read and Write Model

Level 3: Read and Write Datastores
DB Read Replicas CQRS yazısına taşıdım

Level 4: Optional Event-Sourcing - CQRS With Event Sourcing (CQRS-ES)
Event Sourcing yazısına taşıdım

CQRS'in Zor Yönleri
Açıklaması şöyle
The drawbacks of CQRS are:

More code: yes instead of a basic function you have to write a command object, a command handler ( your tests), validation etc. But the code is easy to read and self-explanatory

Asynchronism issues: since your write data storage and read data storage are separated, the data synchronization may be delayed and causing UI issues for your users.

More layers: command, command bus, event, event handlers are new concepts. Building a service or a DDD repository looks easier ( at the beginning)

Technology abstraction: DDD modeling and CQRS are not quite compatible, more thought has to be provided on it and a compromise to be found.



Hiç yorum yok:

Yorum Gönder