architecturemicroservicesevent-sourcingdata-consistency

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?


Solution

  • 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.