Why service collaboration needs choreography AND orchestration

时间:2022-08-25 10:27:17

转自:https://blog.bernd-ruecker.com/why-service-collaboration-needs-choreography-and-orchestration-239c4f9700fa

Let’s assume you want to build a simple order system covering the whole order fulfillment process with the following three microservices involved (you might name it services, components or aggregates if you prefer):

  • Payment
  • Inventory
  • Shipping

The overall order fulfillment definitely requires these three services to collaborate. Currently there is a big fight going on as to which collaboration style to use — orchestration or choreography (for an introduction on the basic styles see Orchestration or Choreography). But is this an either or decision?

I strongly believe that in most real-life scenarios you need a clever mix of both collaboration styles so let’s go over that.

Choreography

Assume that we want to use event-based choreography as much as possible. This is in sync with the typical recommendation among microservice publications, e.g. “Building Microservices” from Sam Newman:

In general, I have found that systems that tend more toward the choreographed approach are more loosely coupled, and are more flexible and amenable to change […].

[…], asynchronous event collaboration helps us adopt a choreographed approach which can yield significantly more decoupled services — something we want to strive for to ensure our services are independently releasable.

And I agree that this make sense on a large scale. Doing so results in the following event flow for our order application:

Why service collaboration needs choreography AND orchestration

So far so good, no component is talking directly to each other and only events are used to communicate. Very de-coupled — isn’t it?

No.

Event command transformation

Why service collaboration needs choreography AND orchestration

The problem is that the payment service has to react to the order placed event. It has to know its consumer! As soon as other services need payment, it has to be adjusted. This is exactly what we don’t want, at least in the case of the payment.

Why service collaboration needs choreography AND orchestration

Another example: You want to implement the new business requirement that VIP customers can pay later by invoice. Now you have to change multiple components: The paymentneeds to know only to execute payments for non VIP customers. The inventory has to know that it reacts also on order placed but only for VIP customers. So you already had to tell two services about your VIP customer even though they should never know about it.

That’s why an additional component that decides that you have to do payment whenever an order was created improves de-coupling of the payment service from its concrete consumer. My friend Martin Schimak an I call that event command transformation:

Why service collaboration needs choreography AND orchestration

Event command transformation pattern

In our example this might be done by an order service.

Orchestration

As soon as you issue commands, you’ve taken the first step towards orchestration as the transformer automatically is some kind of conductor commanding another component. However, the command is still a message and can be sent over the normal bus. So I see good reasons to still summarize that as choreography without remorse. As wording is very important in the current discussions I think this helps to remove misconceptions that orchestration has to be avoided at any price; as you can see that the event command transformation is actually essential to build a nicely de-coupled event-based system!

State Handling

We have a second challenge. The payment might take quite long to complete. Assume we want a call center to call the customer to clear problems during payment. That might take days to weeks. So now you get a long running flowin your order service and you have to track the state. And you might face timing requirements, for example the pricing in the order might only be fixed for 7 days so you want to take action if the order is delayed too long.

I have described basic implementation approaches in How to implement long running flows, sagas, business processes or similar. A typical approach is to use a state machine to do this. This finally means you are using an orchestrated style of collaboration within your order service. It is important to notice the small word within here.

“Overall choregraphy and local orchestration”

This yields the following approach:

  • Orchestration takes place locally in the order service. It is an implementation decision of that service to use a (local) brain to coordinate the flow it has to implement. There is no central conductor steering different services, no central spider in a giant spiderweb. That’s the typical misconception with orchestration engines. But there is a proper event command transformation so that the order service can command the payment service in order to improve de-coupling.
  • The end-to-end business process of ordering goods can be implemented as overall choreography. Only messages (events and commands) are sent over the wire, there is no central conductor controlling everything.
Why service collaboration needs choreography AND orchestration

This brings the best of both worlds: Handling state and mastering control over the flow happens in a context you can easily control — one service. Choreography is used on the larger scale of collaboration of various services to improve flexibility.

Conclusion

For real-life use cases services need to collaborate. You need both: A choreographed event-based style as well as an orchestrated command-based style depending on the situation at hand. With a clever mix you reach the level of de-coupling you aim for especially when using microservices.

If you are interested in concrete code examples for the concepts discussed, there is the flowing retail sample application showing all these aspects in action.

 
 
 
 

Why service collaboration needs choreography AND orchestration的更多相关文章

  1. RocketMQ 4.3正式发布,支持分布式事务

      冯嘉 作者 | 冯嘉   近日,Apache RocketMQ 4.3 版本宣布发布,此次发布不仅包括提升性能,减少内存使用等原有特性增强,还修复了部分社区提出的若干问题,更重要的是该版本开源了社 ...

  2. Apache RocketMQ 正式开源分布式事务消息

    近日,Apache RocketMQ 社区正式发布4.3版本.此次发布不仅包括提升性能,减少内存使用等原有特性增强,还修复了部分社区提出的若干问题,更重要的是该版本开源了社区最为关心的分布式事务消息, ...

  3. BizTalk开发系列(三十)单向端口实现请求-响应

    BizTalk本质上是异步的消息处理引擎.BizTalk的请求与响应模式是基于异步之上的同步消息交换.消息引擎通过消息的扩展架构链接许 多异步消息,消息的相关集关联请求与响应消息.例如,客户端发送一个 ...

  4. Docker Resources

    Menu Main Resources Books Websites Documents Archives Community Blogs Personal Blogs Videos Related ...

  5. OpenStack及其构成简介

    新的一年新的开始,突然想学习下Openstack,之前了解过很多,但是想系统的学习一下,第一次写博客,只想把学到的东西记录下来加深印象,如有写的不好的地方请多多见谅.下面开门见山. 1.What is ...

  6. centos7部署openstack-ocata

    1.前言 本文旨在记录本人的一个实验过程,因为其中有一些坑,方便以后回顾查询. 其中限于篇幅(大部分是配置部分)有些内容省略掉了,官网都有,各位如果是安装部署的话可以参考官网,不建议使用本文. 以下是 ...

  7. The Microservices Workflow Automation Cheat Sheet

    Written by Bernd Rücker on Dec 12 2018 in the Best Practices category. Editor’s Note: This post orig ...

  8. CentOS7.4安装部署openstack [Liberty版] (二)

    继上一篇博客CentOS7.4安装部署openstack [Liberty版] (一),本篇继续讲述后续部分的内容 一.添加块设备存储服务 1.服务简述: OpenStack块存储服务为实例提供块存储 ...

  9. OpenStack的基本概念与架构图

    https://blog.csdn.net/zjluobing/article/details/51489325 OpenStack项目是一个开源的云计算平台,旨在实现很简单,大规模可伸缩,功能丰富. ...

随机推荐

  1. MySql操作时间

    今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 7天 DAY) <= date(时间字段名) 近30天 DAY) &l ...

  2. Orchard源码分析(2):Orchard&period;Web&period;MvcApplication类&lpar;Global&rpar;

    概述 分析一个的ASP.NET项目源码,首先可以浏览其项目结构,大致一窥项目其全貌,了解项目之间的依赖关系.其次可以浏览Web.config和Global.asax文件,找到应用程序的入口点. 本 文 ...

  3. Linux 内核版本规律

    版本组成:主版本号.次版本号.修正版本号 主版本号和次版本号一起标志着重要的功能变更,修正版本号表示较小的功能变更.次版本号表示该版本是否为稳定版本,偶数则为稳定版本,奇数则可能存在一些BUG.

  4. LDA-math-神奇的Gamma函数

    http://cos.name/2013/01/lda-math-gamma-function/ 1. 神奇的Gamma函数1.1 Gamma 函数诞生记学高等数学的时候,我们都学习过如下一个长相有点 ...

  5. JAVA GUI设计中遇到的一个小问题

    最近新学,大牛勿喷.. 写下笔记主要是记录自己常犯的错误,也方便新人解决问题学习参考. 问题:win7下设计GUI,文本框和密码框显示不出来. 我的解决方案: 1. JTextField text = ...

  6. AT&amp&semi;T汇编

    AT&T汇编和8086汇编语言虽然两者很相似,但是还是不能根据8086的语法规则来读AT&T汇编的吧,所以还是要看看AT&T汇编的语法规则,因为在读内核代码时,跟硬件打交道的部 ...

  7. FS SIP呼叫的消息线程和状态机线程

    THREAD 当收到一次呼叫的时候,FS会在TU层创建两个线程,一个线程为状态机线程,另外一个为消息线程.状态机线程通过switch_core_session_thread_launch创建,顾名思义 ...

  8. Redis应用----消息传递

    1.摘要 消息传递这一应用广泛存在于各个网站中,这个功能也是一个网站必不可少的.常见的消息传递应用有,新浪微博中的@我呀.给你评论然后的提示呀.赞赞赞提示.私信呀.甚至是发微博分享的新鲜事:知乎中的私 ...

  9. JAVA优化建议

    前言 代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用, ...

  10. SQL Server锁类型

    SQL Server锁类型(SQL)收藏 1. HOLDLOCK: 在该表上保持共享锁,直到整个事务结束,而不是在语句执行完立即释放所添加的锁. 2. NOLOCK:不添加共享锁和排它锁,当这个选项生 ...