Event sourcing: event’s data (payload) vs metadata

domain-driven-designevent-sourcing

There is often introduced a concept of using metadata in events.

I just would like to understand the difference between data (which is referred to as payload) and metadata. I've read what I could find on the web on it, still would like to hear some opinions.

Let's take some general case. Say there are some items (Item is an aggregate in the system). And users (User is another aggregate too) can create and update those items.

So the question is is information about the user that created/updated the item should go to Event's data or metadata? What should be taken into consideration when deciding on it?

On one hand, user initiated the action is a candidate for metadata, but on the other hand in some view, this user may be considered a kind of important part of item's state (when it is the case), and could be treated as a part of the payload.

So just interested in a perspective on the problem in general and on the particular case.

Best Answer

The simplest way to think about metadata is that it is data that describes other data. For example, the size of the payload (in bytes) is not the content of the payload but rather a data element that is about the payload. If the payload were formatted slightly differently (e.g. extra meaningless white-space), the size might change but the content/meaning of the payload would not. The time a photograph was created and who created it is not the picture, therefore it's metadata.

The information about the user that created the data should most likely go in the metadata since it is describing the content and not actually the content that is being created.

Keep in mind that whether something is metadata or not depends on the context. Metadata is also data in itself and you can have metadata for metadata (and so on and so forth.) For example, if you were building an audit capability and you wanted to see all the users that edited the record, the list of users would be the payload in that context, not the metadata.