Sub topic 1
Why route events: producers, routers, and consumers
Prerequisites
This chapter assumes you have the following:
- An AWS account and a configured AWS CLI.
- Working knowledge of JSON.
- Basic understanding of IAM roles and policies.
- Familiarity with at least one service such as Lambda, SQS, or SNS.
You won't run any commands here. The chapter builds the mental model that the rest of the tutorial relies on.
The problem: direct calls couple services
Suppose an order service must tell three other systems when an order is placed. These are billing, inventory, and a notification service. The simplest design has the order service call each one directly.
This design has three costs:
- The order service must know every consumer and its address.
- Adding a fourth consumer means changing and redeploying the order service.
- If one consumer is slow or down, the order service has to handle that failure itself.
This is tight coupling. The producer carries knowledge that belongs to its consumers.
The event-driven model
An event is a record that something happened, such as "order 1001 was placed". It describes a fact in the past. It does not ask anyone to do anything.
Event-driven architecture splits the work among three roles:
- A producer emits events. It does not know who will receive them.
- A router receives events and decides where each one goes.
- A consumer reacts to events it cares about.
Amazon EventBridge is the router. Producers send events to an event bus, which is the entry point that receives events. Rules attached to the bus inspect each event. When an event matches a rule, EventBridge delivers it to that rule's targets, which are the consumers, such as a Lambda function or an SQS queue.
What the router changes
With a router in the middle, the order service sends one event to one place. Each consumer is added through a rule, so the producer is never touched.
| Concern | Direct calls | Routed through EventBridge |
|---|---|---|
| Producer knows consumers | Yes | No |
| Add a new consumer | Change the producer | Add a rule and target |
| One consumer fails | Producer must handle it | Router retries delivery |
| Filtering | Each consumer or producer codes it | Rules filter by event content |
The producer and consumers now depend only on the shape of the event. That shape is a contract, and the next chapter covers it.
The flow in detail
Following one event through the system shows where each later chapter fits:
- The producer publishes an event to a bus. This uses the PutEvents API.
- The bus compares the event against every rule on that bus.
- Each rule uses an event pattern, a JSON description of the events it wants.
- For every matching rule, EventBridge sends the event to each of its targets. It can first reshape the event with an input transformer.
- If delivery fails, EventBridge retries according to a retry policy. It can then park the event in a dead-letter queue.
A single event can match several rules. Each match delivers independently, so one event can trigger many consumers.
When routing is not the right tool
EventBridge routes events by content. It is a good fit when many consumers want different slices of a stream. It is a poor fit when you need strict ordering, or when one consumer needs to buffer a large backlog and process it at its own pace. In those cases you often combine EventBridge with a queue, as in a rule that targets an SQS queue.
Exercises
Exercise
An order service currently calls billing and inventory directly. You must add a notification service. Describe what changes in the producer if you move to EventBridge, and what you add for the new consumer.
Show answer
Once the producer publishes to an event bus, it needs no change to support the notification service. You add a rule on the bus with an event pattern matching the order events, and attach the notification service as a target.
Exercise
In the three roles of producer, router, and consumer, which role decides which consumers receive an event, and which EventBridge components implement that decision?
Show answer
The router decides. In EventBridge, rules with their event patterns do the matching, and the targets attached to each matching rule receive the event.