Sub topic 1
Why route events: producers, routers, and consumers
Prerequisites
This chapter assumes you have the following:
- An AWS account and the AWS CLI configured with a default region.
- Working knowledge of JSON.
- Basic understanding of IAM roles and policies.
- Familiarity with at least one AWS compute or messaging service, such as Lambda, SQS, or SNS.
You do not need to run anything to follow this chapter. Later chapters build the pieces in the CLI.
The problem: services that call each other directly
Suppose an order service finishes a purchase. Several other things need to happen:
- Billing creates an invoice.
- Inventory reserves stock.
- A notification service emails the customer.
- An analytics job records the sale.
The simplest design is for the order service to call each of these services directly, one after another. This works at first. It breaks down as the system grows.
- Tight coupling. The order service must know every downstream service, its endpoint, and its payload format. Adding a fifth consumer means changing and redeploying the order service.
- Failure propagation. If the notification service is down, the order service must decide whether to fail, retry, or skip. Each caller reimplements that decision.
- Latency. Synchronous calls add up. The customer waits for all of them.
- Ownership. Teams that own billing or analytics cannot add themselves as consumers without asking the order team.
Event-driven design removes these problems by changing what the order service does. It no longer tells other services what to do. It announces that something happened, and others react.
Key terms
An event is a record that something happened, such as "order 1001 was placed." It describes a fact in the past. It is not a command.
A producer is any component that emits events. A producer does not know who, if anyone, will react.
A router is the component in the middle. It receives events from producers, decides which consumers should get each one, and delivers them. In this tutorial the router is Amazon EventBridge.
A consumer (EventBridge calls it a target) is a component that reacts to events, such as a Lambda function, an SQS queue, or a Step Functions state machine.
The difference between an event and a command matters for design. A command ("reserve stock for order 1001") names a recipient and expects an action. An event ("OrderPlaced") names no recipient. This is what lets you add or remove consumers without touching the producer.
How EventBridge fits in
Amazon EventBridge is a serverless event router. At a high level, an event travels through four stages:
- A producer sends an event to an event bus.
- The bus evaluates the event against every rule attached to it.
- Each rule has an event pattern. If the event matches, the rule fires.
- The rule delivers a copy of the event to each of its targets.
Three properties of this flow shape most of the design decisions in the rest of the tutorial.
Matching happens on the router. The producer sends the event once. The rules decide who receives it. A consumer subscribes by creating a rule, not by changing the producer.
One event can reach many targets. Every rule that matches receives its own copy. A single rule can also have several targets. This is fan-out, and you get it without writing fan-out code.
An event that matches no rule is dropped. The bus accepts it, finds no match, and discards it. Nothing is stored unless you configure an archive (covered in a later chapter). This surprises many learners, so keep it in mind when you debug "my event vanished" problems.
Sketching the order example
Before using any AWS commands, it helps to describe a system in terms of producers, events, and consumers. For the order example, the design looks like this.
| Role | Component | What it does |
|---|---|---|
| Producer | Order service | Emits `OrderPlaced` when a purchase completes |
| Router | EventBridge bus `orders-bus` | Receives events and applies rules |
| Consumer | Billing Lambda function | Creates an invoice for every order |
| Consumer | Fraud-review SQS queue | Receives only orders of 100 or more |
| Consumer | Analytics pipeline | Receives every order for reporting |
Notice that the order service has one responsibility: publish OrderPlaced. The decision that "only orders of 100 or more go to the fraud-review queue" lives in a rule, not in the order service. If the threshold changes, you edit a rule. You do not redeploy the producer.
You will build exactly this system over the next chapters. A later chapter shows the event pattern that selects high-value orders, which looks like this:
{
"source": ["com.example.orders"],
"detail-type": ["OrderPlaced"],
"detail": {
"amount": [{ "numeric": [">=", 100] }],
"status": [{ "anything-but": ["test"] }]
}
}You do not need to understand every line yet. The shape is what matters: the pattern describes the events a consumer cares about. It does not describe the consumer itself.
What you gain and what you give up
Routing events through a bus is not free. Compare it with direct calls on the dimensions that matter.
| Dimension | Direct calls | Event routing with EventBridge |
|---|---|---|
| Coupling | Producer knows every consumer | Producer knows only the bus |
| Adding a consumer | Change and redeploy the producer | Add a rule and a target |
| Failure handling | Each caller implements its own | Retries and dead-letter queues are configured per target |
| Response to the caller | Immediate, can return a result | None. The producer does not learn the outcome |
| Debugging | One call stack | Must trace across the bus, rules, and targets |
| Ordering | Controlled by the caller | Not guaranteed |
The right-hand column has costs you should plan for.
- Asynchronous by nature. The producer gets confirmation that the bus accepted the event. It does not get confirmation that any consumer handled it. If the producer needs an answer, such as "was the payment authorized?", use a direct call or a workflow service instead.
- At-least-once delivery. EventBridge can deliver an event to a target more than once. Consumers should be idempotent, meaning that processing the same event twice has the same effect as processing it once. A common approach is to record the event
idand skip IDs you have already seen. - No ordering guarantee. Events may arrive out of order. If a consumer must process updates for one order in sequence, EventBridge alone is not the right tool.
- Indirection. When something does not happen, the cause could be the producer, the pattern, the permissions, or the target. Later chapters cover how to test patterns and monitor rules so this stays manageable.
Choosing the right level of event
A common design mistake is publishing events that are too fine-grained or too coarse. A few guidelines help.
- Name events as past-tense facts.
OrderPlacedandPaymentFaileddescribe what happened.ProcessOrderis a command and invites coupling. - Publish business-level events. Emit
OrderPlaced, notDatabaseRowInserted. Consumers should not depend on your storage design. - Include enough data to act on. If every consumer must call back to the producer for details, you have recreated the coupling. Include the fields most consumers need, such as order ID and amount.
- Avoid sensitive data in events. Events can be copied to many targets, archives, and logs. Include identifiers and let consumers fetch sensitive details through a controlled path.
Where the pieces appear in later chapters
This chapter gave you the vocabulary. The next chapters follow the path of one event.
| Stage | Concept | Chapter topic |
|---|---|---|
| Event | Envelope fields and `detail` | Anatomy of an EventBridge event |
| Bus | Default, custom, and partner buses | Event buses |
| Publish | `PutEvents` | Publishing custom events |
| Match | Rules and patterns | Rules and event patterns |
| Deliver | Targets and input transformers | Targets and reshaping |
| Control | IAM, retries, dead-letter queues | Permissions and reliability |
Later chapters then cover testing and monitoring, cross-account and cross-region routing, archive and replay, the schema registry, and scheduled invocations.
Check your understanding
Exercise
A checkout service currently calls an email service, a billing service, and a loyalty-points service over HTTP after each purchase. The loyalty team wants to stop receiving orders under 20 dollars, and a new fraud team wants to start receiving every order. Describe how moving to EventBridge changes who has to make changes for each request. Also name one thing the checkout service can no longer rely on.
Show answer
The checkout service publishes one OrderPlaced event to an event bus and nothing else. The loyalty team changes the event pattern of its own rule to add a numeric condition on the amount, and the fraud team creates a new rule with a target for its consumer. Neither request requires a change or redeploy of the checkout service. The checkout service can no longer rely on an immediate result from the consumers. It learns only that the bus accepted the event, not that any consumer handled it. It also cannot assume ordering or exactly-once delivery, so consumers must tolerate duplicates.
Exercise
An event is published to a bus that has two rules. Neither rule's pattern matches the event, and no archive is configured. What happens to the event, and how many targets receive it?
Show answer
The bus accepts the event, finds no matching rule, and discards it. No target receives it, and the event is not stored anywhere, so it cannot be replayed later.