Appearance
07. Event-Driven Architecture: Waking the Agent Without Losing Anything
The problem. A resident agent does not run permanently: it "sleeps" between two sessions. What wakes it at the right moment, and how do we make sure no event is lost between two wake-ups?
Example 6: the restaurant. At 12:05, three customers order at the same time, then the point-of-sale system crashes at 12:07. If the agent were only woken "when someone talks to it", the two orders of 12:07 would be lost. With an event-driven architecture, each order is an event written to a durable inbox (a persistent queue) before any processing: the agent is woken by the arrival of a new event, and pending events survive the agent being shut down.
Example 7: the hospital. A lab result arrives at 3 a.m. for a patient on the ward. The event is dropped into the queue; the on-call agent is woken, sees that the result is abnormal, and triggers the alert procedure toward the on-call nurse, without any human having to watch the inbox all night.
The concept. In an event-driven architecture, the agent does not poll the world in a loop: it reacts to events. The basic mechanisms are simple:
- A webhook is an automatic call: an external system (point of sale, laboratory, messaging) calls one of the agent's addresses as soon as something happens.
- A queue (or durable inbox) is a buffer between event producers and the agent: it decouples their rhythms, guarantees that no event is lost if the agent is off, and allows an event to be replayed after a crash. The producer/consumer pair works like this: external systems drop events into the queue (the point of sale, the laboratory, the messaging app are the producers), and the agent processes them (it is the consumer). This decoupling guarantees that producers can go fast even when the agent is slow or off: events accumulate in the queue and are processed in order.
- The schedule triggers tasks at a fixed cadence (morning report, nightly stock check).
- The tick lets the agent decide for itself what to do at each tick: powerful, but costly, since each tick is a model call.
Two design rules follow naturally. First, event processing must be idempotent: if the same event is replayed after a crash, it must not produce the same effect twice (for example, do not send the same reminder email twice, nor place the same supplier order twice). In practice, this means the agent must be able to recognize an event it has already processed, typically via an event ID, before acting. Second, every processed event leaves a trace (who, what, when, what result): this is observability (layer 7) applied to events, and it is what makes it possible to reconstruct the agent's activity between two sessions, and to replay an incident after the fact.
A note on vocabulary: we often distinguish "at-least-once" delivery (the event may be replayed, hence the idempotency requirement) from "exactly-once" delivery (harder to guarantee). Safe design starts from the assumption of "at-least-once" and makes processing idempotent.