Event, producer, and consumer
AWS defines event-driven architecture as an architecture that uses events to communicate between independent services and trigger them. An event represents a change in state or an update. Adding a product to a shopping cart is one example.
The structure described by AWS has three basic components: event producer, event router, and event consumer. A producer publishes an event. The router filters the event and forwards it to consumers. Producer and consumer services can be scaled, updated, and deployed independently of one another.
Microsoft Azure Architecture Center also describes a structure in which producers generate events, consumers listen for them, and event channels carry events between them. A producer does not know which consumers are listening. Consumers are also independent of one another.
The CloudEvents specification defines an event as a data record that expresses an occurrence and its context. A single occurrence can result in more than one event. Information in the event can be used for routing, but the event does not identify a specific routing destination.
According to CloudEvents, a producer is a process, device, or specific instance that creates the CloudEvent data structure. A consumer receives the event and performs an operation using the context and data it contains. This operation may lead to the creation of other events.
Information carried by an event
According to AWS, an event can carry state information. For example, the purchased product, its price, and the delivery address can be included in the event. An event can also carry only an identifier indicating that the order has been shipped.
Martin Fowler explains that in the event notification model, a system notifies other systems of a change in its own domain. The system sending the notification often does not expect a direct response. The flow that sends the notification is separate from the flow that reacts to it.
In Fowler’s explanation, a notification can contain a small amount of data: for example, an identifier and a link for returning to the sending system to request more information. The recipient learns that something has changed; it can then make a request to the sender to determine its next operation.
In the event-carried state transfer model, the event can carry the details of the changed data. In Fowler’s customer management example, when a customer’s address changes, the recipient applies the change to its own copy of the customer data. The recipient does not have to consult the main customer system for its subsequent work.
This model carries more data and copies of data. The recipient becomes more complex because it is responsible for maintaining its own state. Fowler also lists the recipient’s ability to work when the customer system is unavailable and the absence of a need for remote calls for customer information among the consequences of the model.
Azure Architecture Center describes two approaches to payload. Putting all necessary fields in the payload allows the consumer to process it without requesting information from elsewhere. However, large payloads can increase transport costs and bandwidth usage. Records held in multiple systems can lead to consistency problems, particularly after updates.
In the approach where only keys are carried, the consumer retrieves the remaining fields from the relevant data system. According to Azure, having a single system of record helps with data consistency. On the other hand, frequent queries can reduce performance compared with the approach in which all fields are carried in the payload. Smaller events and simpler contracts reduce the complexity associated with bandwidth and contract management.
Context and data in CloudEvents
In the CloudEvents specification, an event carries data about the occurrence and context metadata describing it. Context attributes are designed so that they can be serialized independently of the event data. This allows these fields to be examined without decoding the event data.
Every CloudEvent contains the id, source, specversion, and type fields. id identifies the event; source specifies the context in which the event occurred. The producer ensures that the combination of source and id is unique for different events. A consumer may consider events with the same values for these two fields to be duplicates.
type describes the type of event associated with the occurrence and can be used for operations such as routing. The optional subject specifies the subject of the event in the producer’s context. In the specification’s storage example, source identifies the container, while subject identifies the name of the object created.
The optional datacontenttype specifies the content type of the data field. The optional dataschema identifies the schema that data adheres to. According to CloudEvents, the data field, which contains domain-specific information, is also optional.
Publish-subscribe and event streaming
Azure Architecture Center explains the publish-subscribe and event streaming models in event-driven architecture. Publish-subscribe infrastructure tracks subscriptions and sends the published event to each subscriber. Once received, the event is not retained in a persistent log; subscribers who join later do not see past events.
In the event streaming model, events are written to a log. Events within a partition are kept in a strict order and are persistent. Instead of subscribing to the stream, a client can read from any part of it. The client is responsible for advancing its position in the stream.