CQRS:存储事件并发布它们-如何以安全的方式做到这一点? [英] CQRS: Storing events and publishing them - how do I do this in a safe way?

查看:83
本文介绍了CQRS:存储事件并发布它们-如何以安全的方式做到这一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我在>为什么是CQRS存储库发布事件,而不是事件存储吗?这是CQRS存储库发布事件的任务.到目前为止,一切都很好.

As I've learned in Why is the CQRS repository publishing events, not the event store? it's the CQRS repository's task to publish events. So far, so good.

当然,存储事件和发布事件应该在一个事务中.从技术上讲,这意味着将一个(或多个)记录写入商店,并将一个(或多个)事件发布到消息总线.因此,简单的数据库事务是不够的,它应该是分布式的.

Of course, storing the events and publishing them should be within one single transaction. Technically this means writing one (or more) records to the store, and publishing one (or more) events to a message bus. Hence, a simple database transaction is not enough, it should be a distributed one.

现在,不幸的是,许多NoSQL数据库(例如MongoDB)不支持ACID兼容事务,甚至没有谈论发生在分布式事务中的可能性.而且,还有一些消息队列也不支持分布式事务.

Now, unfortunately, many NoSQL databases (such as MongoDB) do not support ACID-compliant transactions, not even to talk of the possibility of taking place in a distributed transaction. More over, there are message queues out there that do not support distributed transactions as well.

问题是:我该如何处理?

So the question is: How do I deal with this?

是否有推荐使用的模式?

Is there a recommended pattern to use?

推荐答案

您的存储库可以发布事件,而不必这样做.在这种情况下,解决方案是将事件存储用作队列.您将有一个后台进程来监视事件存储中的新事件,将其发布到(例如)总线上,然后将其标记为已调度.

Your repository could publish events, it doesn't have to. The solution in this case is to use the event store as a queue. You would have a background process that monitors the event store for new events, publishes them to (for instance) a bus, and then marks them as dispatched.

一如既往,需要权衡取舍.您可能必须处理至少一次消息传递和幂等处理.比使用简单的分布式事务要复杂得多.

As always, there are trade-offs. You'll likely have to deal with at-least-once messaging and idempotent processing. It's more complex than using a simple distributed transaction.

乔纳森·奥利弗(Jonathan Oliver)撰写了有关该主题的几篇文章,这些文章可能会对您有所帮助: >删除2PC ,如何避免两阶段提交幂等模式

Jonathan Oliver has written several posts about this topic that might help you out: Removing 2PC, How I Avoid Two-Phase Commit, Idempotency Patterns

这篇关于CQRS:存储事件并发布它们-如何以安全的方式做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆