Saga的实现模式——进化(Saga implementation patterns – variations)
在之前的几个博客中,我主要讲了两个saga的实现模式:
- 基于command的控制者模式
- 基于事件的观察者模式
当然,这些都不是实现saga的唯一方式。我们甚至可以将这些结合起来。
发布者——收集者
回顾我们的麦当劳的例子,我们可以稍稍改进一下我们的方案。我们可以通过一个command去启动一个saga,然后这个saga就会发布消息。然后saga就等待回复事件(忽略顺序):
这里有一个优点,那就是我们的saga只有一个入口。因此我们不必担心我们的saga被其他消息触发。
当你在麦当劳下一个订单的时候,总是有一个服务员把托盘放到柜台上。当工作台完成了薯条,三明治等等食物的制作,他们从来不到柜台看看:“我是否还需要一个新的托盘?”因为托盘总是已经被准备好在那里了,就像我们的saga在业务开始之前总是已经开始了的。
在这里没有任何阻止产生额外saga的方法——但是其实是有的。
报告者
saga可以扮演另一个不做任何决定而仅仅报告状态的角色:
This is a situation where a saga might never actually complete, and goes on forever. Its role is to communicate status of a longer-running process in the back-end, not for coordination purposes, but for reporting purposes.
The reason we might want a saga for this case is we still don’t know what order we’ve received messages from downstream systems we don’t own. As we learn about downstream events, we can evaluate them based on our knowledge of the overall business process. An order can’t go backwards from “shipped” to “verified”, so receiving these out of order doesn’t change the fact that the order shipped! Note: this doesn’t necessarily imply the status is in the saga entity itself. It still could be separate.
Keeping it as a saga lets us handle messages in any order and keep some centralized logic around interpreting these messages.
Sagas/process managers are pretty flexible in how we compose the pieces together. I often get questions around “why can’t I design a saga like a workflow?” And the answer is that sagas are meant to handle cases where I don’t have a directed workflow, where I live in a world where messages can arrive out of order.
It’s a much easier world to scale – but we need to accept the complexity it brings on the other end.
There is another clear downside here – we have shared state amongst multiple messages and handlers, which can potentially lead to scaling problems. Next time, we’ll look at scaling sagas.
有一种saga可能永远无法完成并且会永远运行下去。它的职责是和后台运行的长时间运行的事务保持通讯,主要为了报告状态而不是为了协调业务。
我们可能需要这种saga的原因是我们仍然不知道从不属于我们的上游系统中得到消息的顺序。当我们处理上游的事件的时候,我们可以通过我们对整个业务过程的理解来判断。就好比一个订单不可能从“已发货”变成“已审核”,所以尽管收到的事件的顺序不一样也不可能改变货物已经发货的事实。注意:这并不意味着状态必须被包含在saga中,它也是可以被分出去的。
saga让我们能够忽略事件到来的顺序,并且能够通过一定的逻辑解读这些消息。
sagas/process manager能够让我们更灵活地把这些消息组织起来。我经常被人问道:“为什么我们不能把saga设计成一个工作流呢?”答案就是saga是用来处理一些不能控制的工作流情况的,因为在现实世界里消息的到来可能会不按照顺序。
Saga的实现模式——进化(Saga implementation patterns – variations)的更多相关文章
- Saga的实现模式——观察者(Saga implementation patterns – Observer)
https://lostechies.com/jimmybogard/2013/03/11/saga-implementation-patterns-observer/ 侵删. NServiceBus ...
- Saga的实现模式——控制者(Saga implementation patterns – Controller)
https://lostechies.com/jimmybogard/2013/03/14/saga-implementation-patterns-controller/ 之前的文章中我们介绍了观察 ...
- 《Language Implementation Patterns》之 增强解析模式
上一章节讲述了基本的语言解析模式,LL(k)足以应付大多数的任务,但是对一些复杂的语言仍然显得不足,已付出更多的复杂度.和运行时效率为代价,我们可以得到能力更强的Parser. Pattern 5 : ...
- 局部二值模式(Local Binary Patterns)纹理灰度与旋转不变性
Multiresolution Gray Scale and Rotation Invariant Texture Classification with Local Binary Patterns, ...
- .Net Core自实现CLR异步编程模式(Asynchronous programming patterns)
最近在看一个线程框架,对.Net的异步编程模型很感兴趣,所以在这里实现CLR定义的异步编程模型,在CLR里有三种异步模式如下,如果不了解的可以详细看MSDN 文档Asynchronous progra ...
- Javascript编程模式(JavaScript Programming Patterns)Part 2.(高级篇)
模块编程模式的启示(Revealing Module Pattern) 客户端对象(Custom Objects) 懒函数定义(Lazy Function Definition) Christian ...
- Javascript编程模式(JavaScript Programming Patterns)Part 1.(初级篇)
JavaScript 为网站添加状态,这些状态可能是校验或者更复杂的行为像拖拽终止功能或者是异步的请求webserver (aka Ajax). 在过去的那些年里, JavaScript librar ...
- 《Language Implementation Patterns》之 解释器
前面讲述了如何验证语句,这章讲述如何构建一个解释器来执行语句,解释器有两种,高级解释器直接执行语句源码或AST这样的中间结构,低级解释器执行执行字节码(更接近机器指令的形式). 高级解释器比较适合DS ...
- 《Language Implementation Patterns》之 强类型规则
语句的语义取决于其语法结构和相关符号:前者说明了了要"做什么",后者说明了操作"什么对象".所以即使语法结构正确的,如果被操作的对象不合法,语句也是不合法的.语 ...
随机推荐
- 3中转换JSON数据的方式
一:前言 来公司一个星期,把最近做的东西梳理下,并把觉得有必要的知识点记载下,现在传数据很多都是用JSON来传数据,所以我就找了集中传json的方式,其实是有五种的,但是有一个我没有用过,太陌生了,上 ...
- [转]使用 LDAP OU 限制访问
使用 LDAP OU 限制访问 http://www-01.ibm.com/support/knowledgecenter/api/content/SSEP7J_10.2.2/com.ibm.swg. ...
- BZOJ 2095: [Poi2010]Bridges
2095: [Poi2010]Bridges Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 869 Solved: 299[Submit][Stat ...
- 【BZOJ4080】【WF2014】Sensor Network [随机化]
Sensor Network Time Limit: 2 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 魔法炮来到了帝都 ...
- bzoj1015: [JSOI2008]星球大战starwar 并查集+离线处理
题目传送门 这道题可以改为离线处理 倒着找答案 这样删点就变成加点了 有了这个思想题目就很好写了哇 23333 #include<cstdio> #include<cstring&g ...
- Oracle rman 各种恢复
--恢复整个数据库run {shutdown immediate;startup mount;restore database;recover database;alter database open ...
- Chrome扩展及应用开发
Chrome扩展及应用开发(电子书) http://www.ituring.com.cn/minibook/950 文档 官方 https://developer.chrome.com/extensi ...
- 3.orm之peewee
peewee是一款orm框架,为什么选择peewee,是因为它比较简单和Django比较类似,而且还有一个async-peewee,可以进行异步化. 如何定义model和生成表 ''' 我们要定义两张 ...
- 6.memcached缓存系统
1.memcached的安装和参数 memcached缓存系统一般还是部署在linux服务器上,所以这里只介绍linux上memcache的安装 首先切换到root用户,然后apt-get insta ...
- 【 Python 】函数的参数
一.默认参数: 默认参数可以简化函数的调用,设置默认参数时,有几点要注意: 1,必选参数在前,默认参数在后,否则python的解释器会报错. 2,如何设置默认参数. 当函数有多个参数时,把变化大的参数 ...