PATCH requests in event sourcing

Hello everyone is there any best practices in connection with patch requests ?

In event-sourcing every action will produce event.
There could be 3 cases:
1.If a field is not passed, we pass a null value, but we don’t want to assign null value to an entity.
2. We pass null value and want to assign null to a projection
3. Normal way when we assign value to an entity

We tried to use Optional wrap in every field and define its value, Optional.empty, or null or normal way field set. It works, but not convenient. Plus writing up casters is cumbersome.

Any thoughts would be appreciated !

A command can cause multiple events. So you could have an event for every attribute that’s part of the patch, not sure that’s what you want. Maybe something like an EntityUpdatedEvent, which has a map of attributes that are updated?

In my opinion, it all depends on why your action is a patch.

Patch events are difficult in Event-Sourcing, because of their nature. Often, a PATCH-event represents multiple possible use cases depending on the fields that are updated, for example:

  • Updating field D and field F together means that the customer has moved to a new address.
  • Updating fields A and F together means that one of their children moved out.

They are different actions represented by the same request. In this case, I would recommend splitting them out into multiple events by checking the field combo that was updated. In this case, for example, I would make it a CustomerMovedEvent and a ChildMovedAwayFromCustomerEvent.

However, if the PATCH truly is one action by a user or system, for example submitting an administrative update on the customers’ dossier as a whole, use an appropriate CustomerDossierUpdateBySystemXEvent with the Optional fields. This will make the intent very clear. Splitting this up (speaking from experience) is the source of a lot of trouble since it’s an artificial split that’s not based on a business case.

I hope this helps, good luck!