Cloud Design Pattern - Event Sourcing Pattern(事件溯源模式)

来源:互联网 发布:win10有网络不能上网 编辑:程序博客网 时间:2024/03/29 17:35

1.前言

上一篇我们讨论了云计算设计模式之读写隔离模式,讨论了在读写隔离在云计算架构的设计,这一篇要讨论的设计模式和上一篇有一点关联,即Event Sourcing(事件溯源)模式.我看到网上有作者也在总结归纳这两个模式,并且讨论了二者之间的关联,大多数博客里面都将这二者一起来讨论,个人认为确实能起到承上启下,融会贯通的效果.

2.概念

传统的应用中,数据库里存的是Domain Model的实例的当前状态.比如某个储户银行账户的存款数,通常是一个数字.如果考虑到如下的两个情形,我们可能付出的代价比较大.

1) 问题跟踪

如果某个储户的账户出现问题,那么我们只有从浩如烟海的日志中去分析用户的账户数据是如何出错的.但如果一旦日志不够详细,找出问题根源基本只能靠人品了.如果考虑并发的情形,那就基本不可能还原当时的情形,因为基本无法模拟方当时并发的情形.

2) 趋势分析

历史数据的作用在于分析未来的趋势,如果仅仅从浩如烟海的日志中寻找规律,或者按照模型去做数据清洗,数据样例分析,是非常耗时的,基本难以做到快速有效.

3) 事务回滚

在介绍事务修正模式中,我们讲到某个步骤发生错误,之前的各个节点可以自己独立地完成回滚,回滚的依据就是记录的操作步骤及相关参数,根据这些有用信息就可以每个节点自行回滚到原始状态,并且在失败的时候可以retry

可见存储对于Domain Model 的各个事件还是非常有用的,尤其是对于复杂的系统,这也就是我们今天要讨论的事件溯源模式.

3.解决方案

事件溯源模式就是要在动作执行的时候,不仅记录模型当前的状态,还要记录下对模型所做的操作。

4.示例

一个会议室预定程序,在多人都登录预定会议室的时候,系统不仅记录了当前被预定的会议室的数目,同时还记录了会议室被预定(或者取消)的经过.

5.相关阅读

The following patterns and guidance may also be relevant when implementing this pattern:

  • Command and Query Responsibility Segregation (CQRS) Pattern. The write store that provides the immutable source of information for a CQRS implementation is often based on an implementation of the Event Sourcing pattern. The Command and Query Responsibility Segregation pattern describes how to segregate the operations that read data in an application from the operations that update data by using separate interfaces.
  • Materialized View Pattern. The data store used in a system based on event sourcing is typically not well suited to efficient querying. Instead, a common approach is to generate pre-populated views of the data at regular intervals, or when the data changes. The Materialized View pattern shows how this can be achieved.
  • Compensating Transaction Pattern. The existing data in an event sourcing store is not updated; instead new entries are added that transition the state of entities to the new values. To reverse a change, compensating entries are used because it is not possible to simply reverse the previous change. The Compensating Transaction pattern describes how to undo the work that was performed by a previous operation.
  • Data Consistency Primer. When using event sourcing with a separate read store or materialized views, the read data will not be immediately consistent; instead it will be only eventually consistent. The Data Consistency Primer summarizes the issues surrounding maintaining consistency over distributed data.
  • Data Partitioning Guidance. Data is often partitioned when using event sourcing in order to improve scalability, reduce contention, and optimize performance. The Data Partitioning Guidance describes how to divide data into discrete partitions, and the issues that can arise.

More Information

  • The article see “CRUD, Only When You Can Afford It” on MSDN.
  • Introducing Event Sourcing and A CQRS and ES Deep Dive in the patterns & practices guide CQRS Journey on MSDN.
  • The post Event Sourcing on Martin Fowler's blog.
  • Greg Young’s post Why use Event Sourcing? On the Code Better website.

0 0