How To Understand Event Sourcing In Microservices Architecture

Problem Statement

Event Sourcing

  • Event Sourcing is usually combined with CQRS.
  • All update operations of an entity are presented as a commands. Usually these commands relate to the domain area and have some meaning to the business.
  • Each command is saved into append-only event log. As a result, commands are immutable and cannot change. This brings audit features out of the box.
  • A service is subscribed to the event log in order to process events and provide some business value.

Event Sourcing Example

  • User initiates Add Rating command on the Movie Actions Service.
  • Movie Actions Service finds specified movie. It can contain a lot of fields. However, we are only interested in fields to calculate a rating: total number of movie votes and a sum of all movie votes.
  • Information about a rating is updated. One is added to the total number of movie votes. New rating from Add Rating command is added to the sum of all movie votes.
  • The full movie snapshot is sent into the Message Broker.
  • All users can see an updated movie rating (the sum of all movie votes is divided by the total number of movie votes) using Movie Summary Service.
  • User initiates Add Rating command on the Movie Actions Service.
  • Event has next information: Add Rating 8 to movie The Lord of The Rings.
  • Movie Actions Service saves this event into the event log (e.g. Kafka).
  • Movie Summary Service has previously used total number of movie votes and fields for each movie in it’s database.
  • Movie Summary Service processes records from event log and updates the rating.
  • All users can see an updated movie rating (the sum of all movie votes is divided by ).

Event Sourcing Advantages

  • Separation of concerns.
  • Scaling is independent for each service.
  • Suitable data storage and its schema can be selected for each service.
  • Simple and efficient queries in all services.
  • New services can be eaasily added to an existing application.

Event Sourcing Disadvantages

  • Complexity.
  • Eventual consistency.
  • New service can take significant amout of time to process existing events.

Summary

--

--

--

IT Consultant with 8+ years of industry experience — https://datamify.com/

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Oleksii Dushenin

Oleksii Dushenin

IT Consultant with 8+ years of industry experience — https://datamify.com/

More from Medium

How to implement security for microservices

SAGA Microservices Database Pattern

Splitting monolithic systems into microservices

Microservice Logging