Technology

Event sourcing - when it makes sense to use it and what options are available

September 9, 2021

‍Whether in the field of finance, healthcare, retail or logistics: Working with event sourcing is a simple and reliable procedure for companies, because in this way it is easy to record which actions have taken place and for what reason. The pattern automatically lists changes as an event and transfers them to the event log - the history of all triggered calls and APIs can therefore be viewed at any time during event sourcing. Here you can find out what other advantages event sourcing brings and where event sourcing can be used.‍

The benefits of event sourcing

Event sourcing is becoming more and more popular because information can be made quickly accessible by using the pattern and large amounts of data can be processed in a distributed manner with the combination of domain-driven design (DDD) and Command-Query-Responsibility-Segregation (CQRS).

Store information with Event Sourcing

With event sourcing, no information is lost because every request is saved as an event that can be viewed and analyzed afterwards. This can be important for future developments if information is required and retrieved at a later point in time. The overall event sourcing architecture is also very scalable and resilient to system failures, for example when coupled with other systems.‍

Audit implementation: create and understand security audit logs

By storing events, a reliable and event-driven audit log can be created. The data provided makes it possible to take reversible actions and thereby restore information and states. An example: if you manipulate data using CRUD operations, you lose the history when setting an account balance. SET Balance = $150 would overwrite the current balance. If, on the other hand, you save the events individually, you do not lose the history. By means of event sourcing, statuses can be viewed and understood retrospectively.‍

Event-driven architectures and the provision of different data statuses

Events can be made available using event sourcing through a publish-subscribe model. This will alert subscribers when something relevant happens and use that information to keep all systems in sync.

Simplification of structures through Event Sourcing + DDD + CQRS

Event sourcing is often combined with DDD (Domain Driven Design) and CQRS (Command and Query Responsibility Segregation). Thanks to Eventsourcing + DDD, write access to a domain is always fast, since the events only have to be appended. Thanks to Eventsourcing + CQRS, as can be seen in the figure above, so-called read models, also known as projections, are denormalized by processing the events. This allows the read access to be optimized for each projection / read model. The application thus always offers good performance for write and read access. A system based on event sourcing is therefore more complex than traditional 3-tier systems, but offers more scope for optimization because responsibilities can be distributed. Object-relational impedance discrepancies are also avoided by storing the events (instead of storing the domain objects).

Thinking in terms of events

Thinking in terms of events can make sense for certain use cases, especially in more complex business domains. For example, if business processes are closely linked, events instead of CRUD operations can help to map the cognitive overhead of the processes in CRUD operations, since the events follow the natural language of the company. Domain processes are thus easier to understand for new developers if they are well visualized. It is important to consider whether it makes sense to use event sourcing for the application, because e.g. the infrastructure, deployments and other parts can also become more complex.

Planning with events

If a system is set up based on events, terms such as: domain design, stream design (aggregate design), bounded context quickly circulate within the development team.

It is important to plan the events that come into circulation, because they define the business processes. Furthermore, the areas of responsibility must be defined and aggregated. E.g. there are several events that can be bundled together into a so-called aggregate. A well-known technique for this is event storming.

Furthermore, the dependencies between events and projections must be well visualized and planned. A lesser-known but very effective technique for this is event modeling - it can be used to visualize and plan such dependencies.

If the right techniques are applied correctly, event-based systems can be planned and visualized very nicely. Since the events are mostly based on verbs like: “userCreated”, you can also find them in the blueprints of the features. This helps enormously to understand the blueprints very quickly.

Determine the status of the application and rebuild it

By storing events, it is possible to determine the state of an entity at any point in time. For example, it is possible to see the status as of a week before. If the selected events are processed again in an empty application with an empty database, the application status. Be set up for this new service. It is thus possible, for example, to add a service to the architecture listed above and to set up the application state for it without writing a data migration script.

Troubleshooting with Event Sourcing

If an error is noticed in the structure of the stored data, this can be easily corrected thanks to the architecture built using event sourcing. An example would be if you had stored the “account” without its “accountID” in the MobileApp database listed above, but now need it for all accounts. You would only have to correct the consumer of the event and make sure when saving that the “accountId” is also saved. The old events could now be replayed and the new data model built in parallel. Complex data migration scripts are no longer needed.

Event sourcing is also helpful for debugging, since the events are always very descriptive. Using distributed tracing and a good visualization tool like Jaeger, it is also very easy to inspect the entire architecture live. This overview helps with troubleshooting as well as with development.

Creating sustainability through the right support

Switching to the cloud, guaranteeing 24/7 operating times, handling high traffic loads and using collected data sets in big data is part of everyday business for many companies these days. Experts from the digital environment help and then provide reliable support in setting up and maintaining digital strategies: from taking on system maintenance and designing data-intensive applications to exchanging ideas as technical sparring partners. Because in order to digitalize processes completely and successfully, the necessary technical skills, the right business model and a suitable mindset are required.

Conclusion

Event sourcing is based on the result-oriented principle and helps to make later queries about actions and processes transparent and to implement them. The popular storage pattern is a diverse solution in which future challenges are planned and prepared for early and proactively. There are various possibilities to include everyday tasks such as creating audits directly and thus not having to spend any additional resources on them. Event sourcing is a good architecture pattern, especially for the frequently required history. Furthermore, the language remains the same - there is no transpilation from the natural events in a company to CRUD operations - which benefits communication between domain experts and developers. Thanks to events, you can also achieve loose coupling, with which you can adjust the scaling individually for each service, if this becomes necessary over time. We are your partner for demanding digitization topics and provide you with comprehensive advice on the subject of event sourcing. Contact us here and let us advise you!

📝 Like what you read?

Then let’s get in touch! Our engineering team will get back to you as soon as possible.