必须处理事件溯源的最终一致系统中不存在的聚合标识。

huangapple go评论49阅读模式
英文:

Have to handle an aggregate Id not being present in an event sourced eventual consistent system

问题

I am currently struggling in how to ensure that aggregates are eventually consistent in the case that an aggregate does not exist

Lets take I have a order service that deals with creating me an order of products and I have a product service that manages those products

I have two aggregates that are distributed over multiple services and lets say I add product to the order, I would create a command that would create an event to set up the order and one to add the product, what happens tho if the Id provided to the order for the Product does not exist?

// What I have tried

I did think I could send an event to say that the product does not exist after it was added to the order and this in turn would resolve the issue, however, as far as I am aware I would need to create an event based on an aggregate but the Product service would not have an aggregate based on that Id

The other way I thought was to say from order I would tigger and event that say ProductRequestedForOrder, then if the product service has the aggregate it would fire an event to state it exists and then the order service would create another event saying ProductAddedToOrder

I appreciate any help you can provide, thank you

英文:

I am currently struggling in how to ensure that aggregates are eventually consistent in the case that an aggregate does not exist

Lets take I have a order service that deals with creating me an order of products and I have a product service that manages those products

I have two aggregates that are distributed over multiple services and lets say I add product to the order, I would create a command that would create an event to set up the order and one to add the product, what happens tho if the Id provided to the order for the Product does not exist?

// What I have tried

I did think I could send an event to say that the product does not exist after it was added to the order and this in turn would resolve the issue, however, as far as I am aware I would need to create an event based on an aggregate but the Product service would not have an aggregate based on that Id

The other way I thought was to say from order I would tigger and event that say ProductRequestedForOrder, then if the product service has the aggregate it would fire a event to state it exists and then the order service would create another event saying ProductAddedToOrder

I appreciate any help you can provide, thank you

答案1

得分: 1

请注意,代码部分不需要翻译。以下是您要求的内容的翻译:

如果提供给产品订单的 ID 不存在会发生什么?

在业务层面上,应该发生什么?这是您需要首先弄清楚的事情。也许业务想要拒绝订单;或者他们可能想接受订单,即使订单是否能够被满足并不明显。也许订单应该被视为草稿,允许客户进行错误更正。

因为您从订单信息中分发了产品信息(即,在处理订单时无法“锁定”产品),在产品发生更改和该更改对订单处理可见之间总会存在一定的延迟。

这意味着总会存在某种可能性,订单的处理与产品目录进行的某些非计划更改不一致。

如果这是一个严重问题,那么您就无法设计一个以分布方式处理此信息的系统。

如果这不是一个严重问题,那么您实际上只是在尝试设计一个正确平衡延迟协调成本与其他考虑因素的解决方案。

总的来说,您最终将使订单处理具有所需产品信息的本地缓存,并在其自身的业务逻辑中使用该本地副本。然后,您只需要找到正确的缓存策略来使用(我们是否尝试为每个订单刷新缓存?如果产品目录不可用,是否可以重复使用旧数据?如果我们拥有的产品信息太旧,是否需要停止处理订单?等等)。

建议阅读:竞态条件不存在(Udi Dahan,2010)

英文:

> what happens tho if the Id provided to the order for the Product does not exist?

What should happen, at a business level? That's the first thing you need to figure out. Maybe the business wants to reject the order; or maybe they want to accept the order, even though it isn't obvious that it can be fulfilled. Maybe the order should be treated as a draft, permitting error correction by the customer.

Because you have distributed product information from order information (ie, because you can't lock products while working on orders), there's always going to be a bit of latency between a change in Products and that change being visible to Order processing.

And that's always going to mean that there is some possibility that the handling of an order is inconsistent with some unscheduled change made to the product catalog.

If that's a show stopper, then you just can't design a system that treats this information in a distributed fashion.

If it's not a show stopper, then you are really just trying to design a solution that correctly balances the cost of delayed reconciliation against your other considerations.

Broadly, you're going to end up with Ordering having a locally cached copy of the Product information that it needs, and using that local copy in its own business logic. Then you need only discover the right caching policy to use (do we try to refresh the cache for each order? can we re-use our old data if the product catalog isn't available? do we need to stop processing orders if the product information we have is too old? and so on).

Recommended reading: Race Conditions Don't Exist (Udi Dahan, 2010)

huangapple
  • 本文由 发表于 2023年3月15日 18:02:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定