简体   繁体   English

如何在新增的微服务中实现数据一致性?

[英]How to achieve data consistency in a newly added microservice?

For example, we have microservices with event sourcing.例如,我们有带有事件溯源的微服务。 To achieve data consistency we use the following approach:为了实现数据一致性,我们使用以下方法:

  1. A microservice generates an event微服务生成事件
  2. The event is stored in an event store事件存储在事件存储中
  3. The event is published to the subscribed microservices事件发布到订阅的微服务

This approach works fine with microservices that are already in use.这种方法适用于已经在使用的微服务。 But what if I need to deploy another microservice that needs to synchronize data with the event store?但是如果我需要部署另一个需要与事件存储同步数据的微服务呢? Obviously, this new microservice missed all the published events.显然,这个新的微服务错过了所有已发布的事件。

Is this new microservice supposed to pull events from the event store by itself?这个新的微服务是否应该自己从事件存储中提取事件?

Pulling all events from the event store can become costly over time, because the number of events can explode.随着时间的推移,从事件存储中提取所有事件可能会变得昂贵,因为事件的数量可能会激增。

To speed up the initialization of a new microservice, you should create snapshots of your application state, from time to time (and store it in some fast-access memory / cache).为了加快新微服务的初始化,您应该不时创建应用程序 state 的快照(并将其存储在一些快速访问的 memory / 缓存中)。 All the events that are stored after a snapshot, are based on the application state from the snapshot.快照后存储的所有事件均基于快照中的应用程序 state。

In that way, when you deploy a new microservice, it will first load the most recent snapshot, and then apply all the events that were stored after that snapshot, to get up to date with the app state.这样,当您部署新的微服务时,它将首先加载最新的快照,然后应用在该快照之后存储的所有事件,以使用应用程序 state 获取最新信息。

You can adjust the frequency on which the snapshots are being made, so that the number of extra-events that need to be loaded, is relatively small.您可以调整创建快照的频率,以便需要加载的额外事件的数量相对较少。

This is a nice article that you can check out for more details.是一篇不错的文章,您可以查看更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM