了解 Lagoms 持久读取端

understanding Lagoms persistent read side

我通读了 Lagom 文档,并且已经编写了一些相互交互的小服务。但是因为这是我第一次涉足 CQRS,所以我仍然有一些关于持久读取端的概念问题,我并不真正理解。

例如,我有一个用户服务,它保留用户列表(作为聚合)及其个人资料数据,如电子邮件地址、姓名、地址等。

我现在的问题是

如您所见,整个概念还没有真正 'clicked',非常感谢 and/or 一些指点的回答。

if i want to retrieve the users profile given a certain email-address, should i query the read side for the users id, and then query the event-store using this id for the profile data? or should the read side already keep all profile information?

您应该使用专门设计的 ReadModel 来使用电子邮件地址搜索个人资料。您应该仅查询事件存储以重新组合聚合,并且仅重新组合聚合以向它们发送命令,而不是查询。在 CQRS 中,可能无法查询聚合。

If the read side has all information, what is the reason for the event-store? If its truly write-only, it's not really useful is it?

事件存储是写入端(聚合)的真实来源。它用于在处理命令之前重新水化聚合(它们根据先前发出的事件重建其内部和私有状态)并持久化新事件。因此,事件存储是仅附加的,但也用于读取事件流(聚合实例发出的事件)。事件存储确保聚合实例(即由类型和 ID 标识)一次只处理一个命令。

if the user-model changes (for instance, the profile now includes a description of the profile) and i use a read-side that contains all profile data, how do i update this read side in lagom to now also contain this description?

除了我自己的框架,我没有使用任何其他框架,但我猜你重写了(以在事件上使用新添加的字段)并重建 ReadModel。

Following that question, should i keep different read-side tables for different fields of the profile instead of one table containing the whole profile

对于每个用例,您应该有一个单独的 ReadModel(具有自己的 table(s))。 ReadModel 应该非常快,这意味着它应该尽可能小,只包含特定用例所需的字段。这一点非常重要,它是使用 CQRS 的主要好处之一。

if a different service needs access to the data, should it always ask the user-service, or should it keep its own read side as needed? In case of the latter, doesn't that violate the CQRS principle that the service that owns the data should be the only one reading and writing that data?

架构师,就看你了。最好每个 ReadModel 都拥有自己的数据,也就是说,它应该订阅正确的事件,它不应该依赖于其他 ReadModel。但这会导致大量的代码重复。根据我的经验,我希望拥有一些拥有一些数据但也可以按需共享的规范 ReadModel。为此,在 CQRS 中,还有术语 query。就像命令和事件一样,查询可以在您的系统中传播,但只能从 ReadModel 到 ReadModel。

不应在客户请求期间发送查询。它们应该只在后台发送,作为一种异步同步机制。这是影响系统弹性和响应能力的一个重要方面。

我也使用实时查询,当答案改变时,实时查询从权威的 ReadModels 推送到订阅的 ReadModels。

In case of the latter, doesn't that violate the CQRS principle that the service that owns the data should be the only one reading and writing that data?

不,不是。 CQRS 没有指定 如何 R(读取端)更新,只是 R 不应该处理命令并且 C 不应该被查询.