CQRS架构介绍

CQRS on itself is a very simple pattern. It only describes that the component of an application that processes commands should be separated from the component that processes queries. Although this separation is very simple on itself, it provides a number of very powerful features when combined with other patterns.

The diagrams below show an example of an extended layout of a CQRS-based event driven architecture. The UI component, displayed on the left, interacts with the rest of the application in two ways: it sends commands to the application (shown in the top section), and it queries the application for information (shown in the bottom section).

 

Command handling

The user interface sends commands to make changes to the system. Commands are simple object that contain all the data needed to perform the underlying action. An example of the command could be MoveUserToNewAddress. The command should hold the new address for the user and the user id that indicates which user has moved.

Commands also tent to express intent by there name. For example, although the command MoveUserToNewAddress andCorrectAddressForUser both contain the same data – the address and the user id – the intent is definitely different.

All commands are send to a Command Service. This service receives the commands and routes them to the corresponding command handlers. Command handlers should not contain any business logic. The only thing they do is making changes to aggregate roots from the domain and makes changes to them.

The domain

The command handler retrieves domain objects (Aggregates) from a repository and executes methods on them to change their state. All business logic is captured within these objects and is not used for querying. This allows us to optimize this model for behavior.

Aggregate roots contain the actual business logic and are  responsible for guarding their own invariants. State changes on aggregate roots cause domain events to occur. This sequence of domain events represents all the changes that has been made. This pattern is called event sourcing. Both the Domain Events and the Aggregates form the domain model.

 

The domain events

All state changes in the domain are represented by domain events. They are simple object that contain all data related to the change. We gave two examples of command names. The events that are related to the state change of these commands will be UserMovedToNewAddress and AddressCorrectedForUser. Notice that the names are in the past tense.

The Repository

The repository act as a in-memory aggregate collection. The repository allow us to get a single aggregate by the aggregate id or save a single aggregate root into it. Getting an aggregate root is done by getting all its related events and replaying them to build up the aggregate root into the latest state. Saving an aggregate root will result in persisting all its uncommitted events that occurred while making change to it. The events are stored in the event store and when an aggregate root is saved, all the events will also be published to the event listeners.

The event store

All events that have occurred end up in the event store. It contains all the event that represents the state changes in the system. These can be used to build up the current state by replaying them all. This store can also be used to fill up new or repair existing read model.

The event bus

When an aggregate root is saved via the repository all the uncommitted events that represent the state changes that has been made are persisted into the event store. Beside that, the repository also publish these events via the event bus. This bus publishes it to event listener that has registered itself as one being interested in the events. An important choice to make is whether the bus publishes the events to the subscribers in a synchronous or asynchronous way.

Synchronous means that the event handling that is done by the subscribers blocks the current execution and that this waits for all subscribers to complete before returning to the user. This model is simple and is a sensible default.

Asynchronous would not wait for all subscribers to complete before returning to the user. It will drop the event on the bus and return to the user. This will keep the execution of command fast, but introduces eventual consistency. The read models will be consistent at some point of time.

The event handlers

There are different event handlers subscribed to the events bus. The most common one are denormalizers. These event handlers take events and makes changes to the read model based on them. For example, a denormalizer could update the users address in the user table based on the data in the UserMovedToNewAddress event. But it could also update the total number of users in city X based on that the same event.

Event handlers are not only interesting to keep the read model up to date. But they could also be written to make changes to an external system or send warning email to the business when certain event occur. It could also be that an event handler issues a new command . Event handlers are great components to extent the system with new functionality without making changes in it.

Read models

An important part of every application is data. Most of the screen in an user interface request it. But every screen just tents to have a different view on the data. For example, one wants all the products with there name, price and category, while another wants the products with there name and top 3 latest product review score and name of the reviewer.

Read models are models that can be optimized for data querying. And what is a optimal query? That is a query that just queries the data from one source. In other words: select * from x where y. No joining, just give me the data. We can accomplish this by creating one table per view. So that every view can just request data by a simple query.

Your read model doesn’t even have to be a relational database, it could be a document based database that contains a collection for every view.

时间: 2024-08-26 15:09:36

CQRS架构介绍的相关文章

ENode 2.0 - 整体架构介绍

前言 今天是个开心的日子,又是周末,可以轻轻松松的写写文章了.去年,我写了ENode 1.0版本,那时我也写了一个分析系列.经过了大半年的时间,我对第一个版本做了很多架构上的改进,最重要的就是让ENode实现了分布式,通过新增一个分布式消息队列EQueue来实现.之所以要设计一个分布式的消息队列是因为在enode 1.0版本中,某个特定的消息队列只能被某个特定的消费者消费.这样就会导致一个问题,就是如果这个消费者挂了,那这个消费者对应的消息队列就不能自动被其他消费者消费了.这个问题会直接导致系统

ENode 1.0 - 整体架构介绍

前言 今天是个开心的日子,又是周末,可以安心轻松的写写文章了.经过了大概3年的DDD理论积累,以及去年年初的第一个版本的event sourcing框架的开发以及项目实践经验,再通过今年上半年利用业余时间的设计与开发,我的enode框架终于可以和大家见面了. 自从Eric Evan提出DDD领域驱动设计以来已经过了很多年了,现在已经有很多人在学习或实践DDD.但是我发现目前能够支持DDD开发的框架还不多,至少在国内还不多.据我所知道的java和.net平台,国外比较有名的有:基于java平台的是

DDD CQRS架构和传统架构的优缺点比较

明天就是大年三十了,今天在家有空,想集中整理一下CQRS架构的特点以及相比传统架构的优缺点分析.先提前祝大家猴年新春快乐.万事如意.身体健康! 最近几年,在DDD的领域,我们经常会看到CQRS架构的概念.我个人也写了一个ENode框架,专门用来实现这个架构.CQRS架构本身的思想其实非常简单,就是读写分离.是一个很好理解的思想.就像我们用MySQL数据库的主备,数据写到主,然后查询从备来查,主备数据的同步由MySQL数据库自己负责,这是一种数据库层面的读写分离.关于CQRS架构的介绍其实已经非常

谈一下关于CQRS架构如何实现高性能

CQRS架构简介 前不久,看到博客园一位园友写了一篇文章,其中的观点是,要想高性能,需要尽量:避开网络开销(IO),避开海量数据,避开资源争夺.对于这3点,我觉得很有道理.所以也想谈一下,CQRS架构下是如何实现高性能的. 关于CQRS(Command Query Responsibility Segration)架构,大家应该不会陌生了.简单的说,就是一个系统,从架构上把它拆分为两部分:命令处理(写请求)+查询处理(读请求).然后读写两边可以用不同的架构实现,以实现CQ两端(即Command

Kafka设计解析(一)- Kafka背景及架构介绍

本文转自Jason's Blog, 原文链接 http://www.jasongj.com/2015/03/10/KafkaColumn1 摘要 Kafka是由LinkedIn开发并开源的分布式消息系统,因其分布式及高吞吐率而被广泛使用,现已与Cloudera Hadoop,Apache Storm,Apache Spark集成.本文介绍了Kafka的创建背景,设计目标,使用消息系统的优势以及目前流行的消息系统对比.并介绍了Kafka的架构,Producer消息路由,Consumer Group

从零开始编写自己的C#框架(5)——三层架构介绍

原文:从零开始编写自己的C#框架(5)--三层架构介绍 三层架构对于开发人员来说,已经是司空见惯了,除了大型与超小型项目外,大多都是这种架构来进行开发.   在这里为初学者们简单介绍一下三层架构: (下面内容摘自<趣味理解:三层架构与养猪-<.NET深入体验与实战精要>>,这是以前看到的关于三层架构介绍,觉得挺经典的,大家有时间的话认真看看) 对比以上两图,我们可以看出: 1)数据库好比猪圈 ,所有的猪有序地按区域或编号,存放在不同的猪栏里. 2)DAL 好比是屠宰场 ,把猪从猪圈

Apache Shiro 使用手册(一) Shiro架构介绍_Linux

一.什么是ShiroApache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能:认证 - 用户身份识别,常被称为用户"登录":授权 - 访问控制:密码加密 - 保护或隐藏数据防止被偷窥:会话管理 - 每用户相关的时间敏感的状态.对于任何一个应用程序,Shiro都可以提供全面的安全管理服务.并且相对于其他安全框架,Shiro要简单的多. 二.Shiro的架构介绍首先,来了解一下Shiro的三个核心组件:Subject, SecurityManager

互联网金融网贷平台技术架构介绍

本文讲的是互联网金融网贷平台技术架构介绍,目前互联网金融正飞速发展,快速改变着我国的金融格局.短时间内,各类融资理财平台(如陆金所.人人贷.旺财谷等)为有理财需求的理财者们,提供各种投资标的和投资渠道;也为大量有融资需求的企业开辟了新的融资渠道.这些平台主动拥抱互联网金融的浪潮,迎来了行业发展的机遇. 伴随着整个行业的成长,各个理财平台技术团队和技术架构也在发展.现在这些平台也不可避免的也发生了分化,有的快速成长,有的面临困境. 比如:有的平台采用外包或购买技术平台,没有能力去进行系统的研发和升

CQRS架构PPT分享

好久没有写文章了,最近工作比较忙.下周要到公司另一个部门做CQRS的分享,所以用一周时间整理了一个PPT.为了方便大家查看,我想直接贴到博客里最简单直接. CQRS是一个不错的架构,但是要真正实践,还是很难的.我虽然学习了很多的理论,框架也实践了不少.但要真正应用到实际项目中,还是不那么容易的.到目前为止我个人也只在一个项目中实践过,但当初实践的时候也没有采用本PPT所提到的最终一致性的技术.不过我想,有兴趣就要坚持,坚持就是胜利.最近我在做另一个CQRS的案例,就是微软的那个CQRS Conf