Using IDs for causation and correlation

tl;dr: IDs are identifiers of single commands and events. Causation IDs are pointers to the individual predecessor of a command or event. Correlation IDs are pointers to the very first command or event in a chain.

However, actually there are three IDs. First, there is the ID of a command or an event itself. Then, there is a causation ID. Finally, there is a correlation ID. What are they good for?

IDs identify commands and events

As already said, the command or event ID is just an identifier for a single command or event. When you are in a distributed system, you typically use a UUID for this. This way, not only the server, but also the client is able to generate them, without the need for central ID management, such as a database. It's just an ID.

Causation IDs create links

Now, commands typically lead to events. In other words, events are caused by commands. This is what causation IDs are for. They create a link between a command and an event, where one led to the other. While an event gets its own ID, its causation ID is set to the ID of the command that the event responds to.

If commands are being created as a result of an event, they can have a causation ID as well. In these terms, causation IDs define 1:1 relations between commands and events.

Correlation IDs build chains

Correlation IDs, at last, are for implementing chains. A command that causes an event, that causes a command, that causes an event, and so on is called a chain. Each of the commands and events has their own unique and independent ID, and each two of them are connected by a causation ID, as described previously.

This means that you could use the causation ID of the last received element to step backwards within the chain and find the predecessor of the element. Anyway, it is difficult to get all the commands and events at once that belong to a single chain.

This is what the correlation ID is for. It is set to the ID of the very first command, and then copied over when the command causes an event, or an event causes a command.

Summary

So, to cut a long story short: IDs are identifiers of single commands and events. Causation IDs are pointers to the individual predecessor of a command or event. Correlation IDs are pointers to the very first command or event in a chain, which might be used to identify all elements of a long-running transaction.

Twitter Facebook LinkedIn

Golo Roden

Founder, CTO, and managing partner

Since we want to deliver elegant yet simple solutions of high quality for complex problems, we care about details with love: things are done when they are done, and we give them the time they need to mature.