并发对 sagas 的价值是什么?

What's the value of concurrency for sagas?

我不明白 saga 的并发消息的用途。我希望它表现得更像一个演员。所以所有具有相同 CorrelationId 的消息都被顺序处理。 saga 的全部目的是编排 long 运行 进程,那么为什么并行消息处理很重要?

你能给出一个合法的例子,说明与顺序模式相比,同时处理 saga 实例的消息是有益的吗?

还是我理解错了,并发就是几个不同的saga实例运行并行?

问的原因是来自 NServiceBus 的片段 docs:

The main reason for avoiding accessing data from external resources is possible contention and inconsistency of saga state. Depending on persister, the saga state is retrieved and persisted with either pessimistic or optimistic locking. If the transaction takes too long, it's possible another message will come in that correlates to the same saga instance. It might be processed on a different (concurrent) thread (or perhaps a scaled out endpoint) and it will either fail immediately (pessimistic locking) or while trying to persist the state (optimistic locking). In both cases the message will be retried.

有none,单个saga实例的消息需要顺序处理。 MassTransit 中的 saga 配置没有什么特别的,你真的想为它使用一个单独的端点并将并发限制设置为一个。

但这会降低处理不同 saga 实例的消息的性能。要解决此问题,请将并发限制保持在高于 1 的水平,并使用相关 ID 的分区过滤器。不幸的是,分区过滤器需要按消息配置,因此您需要为 saga 使用的所有消息配置分区。

但这完全取决于用例。当使用基于持久性的乐观并发时,所有并发问题都通过重试来解决,每个 saga 持久性提供者都有记录。当然,它通过重试数据库操作会产生一些噪音,但是如果重试次数在控制范围内,那么您可以保持原样。

如果由于大量并发更新而导致重试次数过多,您可以恢复为对 saga 进行分区。