Skip to content
Back to log

Fundamentals of event-driven architecture

Chainabit's Chao · · 9 min read

In short

An introduction explaining the flow between event producers, routers, and consumers, with concrete examples of payload, event streaming, and topology options.

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.

Turn this insight into action.

Use the mobile app to continue with guided workflows.

According to Azure, the client’s ability to reread events supports consumers that join later, reprocessing after an error is corrected, and recovery scenarios. In the publish-subscribe model, subscribers who join later do not see past events.

AWS also describes using fanout to forward an event to multiple systems. The router sends the event to the relevant systems; each system can process the event for a different purpose and in parallel with the others. This structure does not require custom code to send the event to each consumer individually.

How does a consumer process an event?

In Azure Architecture Center’s description of simple event processing, a single event immediately triggers an action within the consumer. In the basic event correlation approach, the consumer correlates several separate business events through an identifier. It stores information from previous events and uses it when processing subsequent events.

Complex event processing examines a series of events to identify patterns in the data. In Azure’s example, measurements from a device are collected over a period of time. A notification can be generated if the moving average exceeds a certain threshold.

In the event stream processing approach, a streaming platform receives events and passes them to stream processors. The processors process or transform the stream. There can be multiple stream processors for different subsystems of the application.

Azure states that multiple instances of the same consumer type can be run. This may also be necessary to handle the volume and frequency of events. However, multiple instances can create challenges when events need to be processed in order or only once.

Organizing the event flow

Azure Architecture Center defines two topologies. In a broker topology, components publish events to the system. Other components react to the event or ignore it. There is no central coordination or orchestration; Azure describes this structure as useful when the event processing flow is relatively simple.

In a broker topology, no component holds or knows the complete state of a multistep business transaction. Actions are performed asynchronously. Error handling and data consistency can become more difficult because there is no built-in mechanism for restarting or replaying distributed transactions.

In a mediator topology, an event mediator manages and controls the event flow. It maintains state and manages error handling and restart functions. Unlike the general broadcast in a broker topology, it sends commands to specific channels. Consumers are expected to process these commands.

According to Azure, a mediator provides greater control and enables distributed error handling. In return, dependencies between components increase. The mediator can also become a bottleneck or a reliability issue.

Event sourcing and rebuilding state

Martin Fowler explains that in the event sourcing approach, every change in system state is recorded as an event. The system state can be rebuilt by reprocessing the events. The event store is the primary source of truth; the system’s current state is derived from these records.

According to Fowler, event sourcing does not require event processing to be asynchronous. Nor does every part of the application need to read the event log directly to access useful data. Operations can also be performed using a working copy derived from the event log.

A snapshot of a working copy can be used to avoid reprocessing all events from the beginning each time. Replaying the event log up to a specific point can recreate a past state. However, replay becomes problematic if the results depend on interactions with external systems. Changes to the event schema over time are also an issue that must be addressed.

Limits and processing challenges

According to Azure Architecture Center, event-driven architecture may not be suitable for simple request-response workflows if synchronous calls meet latency and throughput requirements. In such interactions, operating an event broker, asynchronous error handling, and eventual consistency add overhead.

The data held by producers and consumers separated by asynchronous channels does not become consistent immediately when an event is published. Consumers process events at their own pace. During this interval, different parts of the system may have different information about the current state. This can create problems for business transactions that require strong consistency across services.

Debugging can also become more difficult in distributed flows. Azure states that a single business transaction can pass through multiple producers, channels, and consumers that operate independently and asynchronously. Because there is no shared call context, it becomes harder to determine which component caused the problem and why.

The event notification model also has a visibility limit. According to Fowler, if a logical flow passes through multiple notifications, that flow may not be clearly visible in the program text. Understanding the flow may require monitoring the live system; debugging and making changes can become more difficult.

Azure also states that an event can be lost if a component crashes before successfully processing the event and passing it to the next component. Using a separate error-handler processor when a consumer encounters an error is one of the options described. If this processor can resolve the problem, it resends the event to the first channel; if it cannot, it can transfer the event to a dead-letter queue for examination. Resent events are processed out of order.

Ready to start?

Download the app and continue from your phone.

End of Entry