英文:
How to handle data sync in data replication approach on microservices archetecture?
问题
在这篇微软的文章中,他们说一个微服务不应该调用另一个微服务来获取数据连接。这是可以理解的,因为它增加了自治性。相反,它应该使用发布/订阅消息代理(例如Kafka)传播原始数据,这样数据将被部分地提前保留在两个微服务中。
假设我们正在讨论的是订单微服务,它依赖于用户微服务提供的数据,限定在这个问题的范围内。
这种架构方法导致了一些我不知道如何解决的重要问题:
-
如何同步部分复制到订单微服务的数据库中的用户实体的更改?甚至更复杂的是,当不是所有的修改都相关时,而其他修改是相关的:如果用户更改了他的名字,这个更新对订单服务来说是不相关的,因为它不使用用户的名字。然而,如果用户升级了他的权限,这对订单服务来说是相关的,以执行其逻辑。
-
假设我们的产品希望在创建订单时确保给定用户具有一些特定的特权。这些特权对象的数组并没有从用户集合中的第一个复制到订单服务中,因此我们需要在数据中执行大量的集合扫描以填充这部分特定的数据。在庞大的数据集中,这可能是不可能的。
-
一个新的微服务已经启动。它要如何复制所有用户到自己的数据库?(与问题2相关)
英文:
In this Microsoft's article, they say that one microservice should never call another microservice for joining data. Its understandable - it adds autonomousment.
Instead, it should propagate the origin data using pub/sub message broker - Kafka for example, so the data will be kept, partially, ahead of time, in both microservices.
Lets say that we are talking about Orders microservice which is reliable on data comes from Users microservice in the scope of this question.
This architectural approach leads to big issues which I don't understand how to solve:
-
How to sync changes in user entity which is partially replicated to the Orders microservice's DB?
And even more complicated - when not all of the modifications are relevant, but others are: if user changes his first name, this update doesn't relevant for the Orders service, since it doesn't make a use with the name of the user. However, if user upgraded his permissions, it is relevant for the Orders service to perform its logic. -
Say our product wants a new feature that when creating order we will ensure that a given user has some specific privileges. These privileges array of objects were not replicated from first of all to the users collection resides in the Order service - we should do a huge collscan in order to populate this specific part of data. In huge data set that can be impossible.
-
A new microservice has launched. How does it gonna to copy all of the users to its own DB? (related to question 2.)
答案1
得分: 1
根据我的理解(这可能有点主观),事件驱动架构/事件溯源可能是解决您的问题的一种方法。
-
如何同步部分复制到订单微服务的数据库中的用户实体的更改?甚至更复杂的是,当并非所有修改都相关,而其他一些修改是相关的:如果用户更改了名字,这个更新对订单服务没有关联,因为它不使用用户的名字。但是,如果用户升级了权限,这对订单服务来说是相关的,因为它需要执行其逻辑。
您可以让用户服务为用户属性的每次更改触发更新事件(以这种方式,您可以将有意义的属性组合成一个事件)。对于对这些事件感兴趣的任何其他服务,都可以注册自己作为监听者并根据事件做出响应。我假设您的订单服务具有一组(有限的)用户属性,以便完成其任务。
-
假设我们的产品想要一个新功能,即在创建订单时确保给定的用户具有特定的权限。这些特权对象的数组未从一开始就复制到订单服务的用户集合中,因此我们需要在数据的特定部分执行大规模扫描。在大规模数据集中,这可能是不可能的。
在这种情况下的关键是,通过重新播放从应用程序启动以来的所有用户事件集合,或者从某个快照开始(以节省一些重建时间,具体取决于要处理的事件数量),让您的订单服务重建其自己的数据存储。请记住,需要仔细考虑这将花费的时间/资源数量,以及是否可能重建订单服务的数据存储。如果订单服务的数据存储与其他服务共享(这是在设计应用程序/服务之前需要慎重考虑的事情),那么这将不可行。
-
新的微服务已经启动。它如何将所有用户复制到自己的数据库中?(与问题2有关。)
如前一个回答中所提到的,通过回放所有用户事件,假设您已经建立了事件驱动架构。
英文:
To my understanding (which makes the answer perhaps a bit opinionated) event driven architecture(s)/event sourcing could be a solution for your questions.
> 1. How to sync changes in user entity which is partially replicated to the Orders microservice's DB? And even more complicated - when not all of the modifications are relevant, but others are: if user changes his first name, this update doesn't relevant for the Orders service, since it doesn't make a use with the name of the user. However, if user upgraded his permissions, it is relevant for the Orders service to perform its logic.
What you could do is let your user service fire update events for every change to the user's attributes (in such way you group attributes that make sense as a whole into one event). Every other services interested in such events, registers itself as a listener and responds to the event accordingly. I assume Your order µ-service has a (limited) set of user with properties it need to fulfil its tasks.
> 2. Say our product wants a new feature that when creating order we will ensure that a given user has some specific privileges. These privileges array of objects were not replicated from first of all to the users collection resides in the Order service - we should do a huge collscan in order to populate this specific part of data. In huge data set that can be impossible.
The key in this situation is to let your order service rebuild its own datastore with user details that are relevant in the new use case by replaying the entire collection of user events since the beginning of time (of your application) or since a certain snapshot (to save some rebuilding time depending on the amount of events to process). Keep in mind, one needs to carefully think about the amount of time/resources this will take and if it is possible to rebuild the order service's datastore. This will not be feasible is the same datastore of the order service is shared with other services (which is something one should carefully consider before designing an application/service that way).
> 3. A new microservice has launched. How does it gonna to copy all of the
> users to its own DB? (related to question 2.)
As mentioned in the previous answer, by replaying all user events assuming you have an event driven architecture in place.
If you are looking for some additional background information, check this article or this article.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论