Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

architecture - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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). All the events that are stored after a snapshot, are based on the application state from the snapshot.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...