Sub topic 2
Event Notification and Event-Carried State Transfer
Assume you know basic programming, HTTP request/response APIs, database transactions, and distributed-system terms. This chapter focuses on choosing event payloads and handling delivery safely.
Notification versus State Transfer
An event notification says that something happened. Its payload is usually small:
{"type": "order.placed", "orderId": "o-1"}The consumer fetches the current order from the producer's API. This keeps events small, but the consumer depends on the API and may see data that changed after the event.
Event-carried state transfer includes the information the consumer needs:
{
"type": "order.placed",
"orderId": "o-1",
"totalCents": 4599,
"currency": "USD"
}The consumer can work independently and retain its own copy. Producers must then maintain compatible schemas and accept that copies can become stale.
flowchart LR
P[Order service] -->|notification| B[Broker]
B --> C1[Consumer]
C1 -->|fetch current data| P
P -->|state transfer event| B
B --> C2[Independent consumer]Delivery Guarantees
| Guarantee | Meaning | Main consequence |
|---|---|---|
| At-most-once | A message is delivered zero or one time | Loss is possible |
| At-least-once | A message is delivered one or more times | Duplicates are possible |
| Effectively-once | The result appears once | Requires deduplication or transactional design |
At-least-once delivery is common because retrying is safer than silently losing a business event.
Idempotent Consumers
An idempotent operation produces the same result when repeated. Store each event's unique ID with the state change, ideally in one database transaction.
balances = {"acct-1": 100}
processed_ids = set()
def handle_deposit(event):
event_id = event["event_id"]
if event_id in processed_ids:
return
balances[event["account"]] += event["amount"]
processed_ids.add(event_id)
event = {"event_id": "e-42", "account": "acct-1", "amount": 25}
handle_deposit(event)
handle_deposit(event)
print(balances["acct-1"]) # 125Ordering, Keys, and Partitions
Brokers often divide a topic into partitions, which are ordered logs. A key chooses a partition. Use an entity key such as account_id when events for one account must remain ordered. Different keys can be processed in parallel, so global ordering is usually unavailable.
Event Schemas and Versioning
An event schema defines its fields and types. Prefer past-tense names such as order.placed. Add optional fields rather than renaming or removing required fields. Consumers should ignore fields they do not understand.
Include an event ID, type, version, timestamp, and data. Publish a new version when compatibility cannot be preserved, and migrate consumers before retiring the old version.
Exercise
Which design is safer for a consumer that must calculate an invoice without calling the order service?
Show answer
Event-carried state transfer, because the event includes the required order data. The consumer must still handle schema versions and stale data.