Event sourcing is just a tool; it’s not a top level architecture style and should not be used everywhere, Dennis Doomen pointed out in his presentation on the Event Sourcing day at the recent DDD Europe 2020 conference in Amsterdam, where he shared some of the practices he has found useful when applying event sourcing on a problem.
Doomen, continuous improver at Aviva Solutions, notes that one consequence of using event sourcing is that you must understand the domain you are working in. This is for him, a good thing. In his experience, too many systems are built without a proper understanding of the business perspective. To capture how the business really thinks about a domain, he recommends using event storming, a technique that aligns well with event sourcing.
As an example of how to reason about where event sourcing can be used, Doomen uses an application handling legal documents with three subdomains or boundaries: a drafting domain, a policy execution domain and a search domain. In the drafting domain policy, documents are designed and information is collected, but no legal data is handled. Here a traditional CRUD-based architecture is probably good enough, maybe using a document store. In the policy execution domain, policies are executed and things like signatures and other legally important information must be tracked including when it happened. Lots of functional conflicts and things working together that need to be tracked are reasons that indicate that the use of event sourcing can be beneficial within this boundary. For the search domain, something that is optimized for search should be used, like Elasticsearch or something else built on Apache Lucene.
Doomen emphasizes that events from inside a domain should not be published outside of the boundary or bounded context. He has tried this, and it has always caused problems. For the outside world, separate high-level events should be used, and he refers to a paper Pat Helland wrote in 2005: Data on the Outside versus Data on the Inside. He also recommends using only primitive types in events. This will minimize the risk of not being able to read old events because the rules for a more complex type has changed over the years.
For versioning of events, Doomen often uses a converter that can rename, split or merge events before forwarding them to the aggregate that is being read. Such a converter can be run during a migration and sometimes also on the fly. For more versioning strategies, he refers to two papers by Marten Spoor and others about managing data conversions and data schema changes in an event sourced system.
Projections is something Doomen still sees developers do wrong. Two crucial attributes for projections are that they are autonomous and independent. Each projector must have its own subscription and independently track the events needed for the projection. This also means that for every projection, the most optimized type of storage technique can be used. Projections should not be reused for multiple purposes; changes occurring over time with fields added can in the end lead to a large unmaintainable projection. Being able to reason about and optimize each projection individually is very important and a practice he tries to follow all the time.
When updating projections, raw SQL is often used instead of an object-relation mapping (ORM) tool, with the argument that it’s faster and that there is no object-relational mismatch in projections. For Doomen, an ORM can still have an advantage because they commonly use the unit of work pattern and can thereby optimize the writing to the database. He notes that by making projections autonomous, we have the freedom to select the tool that works best for each projection.
In his presentation, Doomen also discusses lookups to enhance a projection, how to archive projections when a system grows with a huge amount of events, tombstoning where a snapshot is taken and old events are deleted, and different types of upgrades.
The slides from Doomen’s presentation are available for download. Most presentations at the conference were recorded and will be published during the coming months.