Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event-driven architecture and structure of events

I'm new to EDA and I've read a lot about benefits and would probably be interested to apply it during my next project but still haven't understood something.

When raising an event, which pattern is the most suited:

  1. Name the event "CustomerUpdate" and include all information (updated or not) about the customer
  2. Name the event "CustomerUpdate" and include only information that have really been updated
  3. Name the event "CustomerUpdate" and include minimum information (Identifier) and/or a URI to let the consumer retrieves information about this Customer.

I ask the question because some of our events could be heavy and frequent.

Thx for your answers and time.

like image 977
Cédric L. Charlier Avatar asked Dec 31 '15 10:12

Cédric L. Charlier


People also ask

What is event-driven structure?

In computer programming, event-driven programming is a programming paradigm in which the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or message passing from other programs or threads.

What is event-driven architecture example?

An Event-Driven Architecture for data and applications is a modern design approach centered around data that describes “events” (i.e., something that just happened). Examples of events include the taking of a measurement, the pressing of a button, or the swiping of a credit card.

What is event-driven architecture used for?

As to when you should use an Event-Driven Architecture? EDA is ideal when you want to capture behaviors in the real world as well as the digital, along with pertinent facts that can be used for decision-making. A record of the event is stored for future analysis.

What are the characteristics of event-driven architecture?

An EDA uses asynchronous messaging, typically pub/sub. An EDA is granular at the event level. EDAs have event listeners, event producers, event processors, and event reactors—ideally based on Simple Object Access Protocol (SOAP) Web services and compatible application components.


1 Answers

Name the event "CustomerUpdate"

First let's start with your event name. The purpose of an event is to describe something which has already happenned. This is different from a command, which is to issue an instruction for something yet to happen.

Your event name "CustomerUpdate" sounds ambiguous in this respect, as it could be describing something in the past or something in the future.

CustomerUpdated would be better, but even then, Updated is another ambiguous term, and is nonspecific in a business context. Why was the customer updated in this instance? Was it because they changed their payment details? Moved home? Were they upgraded from silver to gold status? Events can be made as specific as needed.

This may seem at first to be overthinking, but event naming becomes especially relevant as you remove data and context from the event payload, moving more toward skinny events (the "option 3" from your question, which I discuss below).

That is not to suggest that it is always appropriate to define events at this level of granularity, only that it is an avenue which is open to you early on in the project which may pay dividends later on (or may swamp you with thousands of event types).

Going back to your actual question, let's take each of your options in turn:

Name the event "CustomerUpdate" and include all information (updated or not) about the customer

Let's call this "pattern" the Fat message.

Fat messages (also called snapshots) represent the state of the described entity at a given point in time with all the event context present in the payload. They are interesting because the message itself represents the contract between service and consumer. They can be used for communicating changes of state between business domains, where it may be preferred that all event context be present during message processing by the consumer.

Advantages:

  • Self consistent - can be consumed entirely without knowledge of other systems.
  • Simple to consume (upsert).

Disadvantages:

  • Brittle - the contract between service and consumer is coupled to the message itself.
  • Easy to overwrite current data with old data if messages arrive in the wrong order (hint: you can mitigate this by using the event sourcing pattern)
  • Large.

Name the event "CustomerUpdate" and include only information that have really been updated

Let's call this pattern the Delta message.

Deltas are similar to fat messages in many ways, though they are generally more complex to generate and consume. A good example here is the JSONPatch standard.

Because they are only a partial description of the event entity, deltas also come with a built-in assumption that the consumer knows something about the event being described. For this reason they may be less suitable for sending outside a business domain, where the event entity may not be well known.

Deltas really shine when synchronising data between systems sharing the same entity model, ideally persisted in non-relational storage (eg, no-sql). In this instance an entity can be retrieved, the delta applied, and then persisted again with minimal effort.

Advantages:

  • Smaller than Fat messages
  • Excels in use cases involving shared entity models
  • Portable (if based on a standard such as jsonpatch, or to a lesser extent, diffgram)

Disadvantages:

  • Similar to the Fat message, assumes complete knowledge of the data entity.
  • Easy to overwrite current data with old data.
  • Complex to generate and consume (except for specific use cases)

Name the event "CustomerUpdate" and include minimum information (Identifier) and/or a URI to let the consumer retrieves information about this Customer.

Let's call this the Skinny message.

Skinny messages are different from the other message patterns you have defined, in that the service/consumer contract is no longer explicit in the message, but implied in that at some later time the consumer will retrieve the event context. This decouples the contract and the message exchange, which is a good thing.

This may or may not lend itself well to cross-business domain communication of events, depending on how your enterprise is set up. Because the event payload is so small (usually an ID with some headers), there is no context other than the name of the event on which the consumer can base processing decisions; therefore it becomes more important to make sure the event is named appropriately, especially if there are multiple ways a consumer could handle a CustomerUpdated message.

Additionally it may not be good practice to include an actual resource address in the event data - because events are things which have already happened, event messages are generally immutable and therefore any information in the event should be true forever in case the events need to be replayed. In this instance a resource address could easily become obsolete and events would not be re-playable.

Advantages:

  • Decouples service contract from message.
  • Information about the event contained in the event name.
  • Naturally idempotent (with time-stamp).
  • Generally tiny.
  • Simple to generate and consume.

Disadvantages:

  • Consumer must make additional call to retrieve event context - requires explicit knowledge of other systems.
  • Event context may have become obsolete at the point where the consumer retrieves it, making this approach generally unsuitable for some real-time applications.

When raising an event, which pattern is the most suited?

I think the answer to this is: it depends on lots of things, and there is probably no one right answer.

Update from comments: Also worth reading, a very old, classic, blog post on messaging: https://docs.microsoft.com/en-gb/archive/blogs/nickmalik/killing-the-command-message-should-we-use-events-or-documents (also here: http://vanguardea.com/killing-the-command-message-should-we-use-events-or-documents/)

like image 149
tom redfern Avatar answered Oct 20 '22 20:10

tom redfern