Video in progress
How much data should you put in an event: just an ID (notification event) or the full state (event-carried state transfer)?
If you send only the ID, the event is small, but every consumer goes back to the producer to get the data — the dependency and the load come back. If you send the full state, consumers are independent, but the event is big and the schema becomes a contract. The choice depends on the number of consumers and how fast the data goes stale.
📖 Answer
Two extremes, as an example.
Notification event:
{"type": "CustomerUpdated", "customerId": 42, "version": 7}
The consumer: "aha, it changed" — then GET /customers/42. With 10 consumers, every change brings the customer service 10 extra requests. If the customer service goes down, the consumers can't work either — the benefit of being asynchronous shrinks. But the consumer always gets the latest state.
Event-carried state transfer:
{"type": "CustomerUpdated", "customerId": 42, "version": 7,
"name": "Aziz", "city": "Toshkent", "tier": "GOLD"}
The consumer stores the fields it needs in its own database and calls nobody. The cost: every field is now a public contract, and changing them is subject to schema evolution rules. Personal data spreads to many places — on a "right to erasure" request, you'll have to find all of it.
How I decide:
- when there are many consumers and they shouldn't depend on the producer being available — full state;
- when the data is confidential or very large (a file, a long history) — an ID and a link;
- in both cases a
version(orupdatedAt) field — so that if an old event arrives late, the consumer doesn't overwrite newer state with older state.
There's a middle option too: only the changed fields in the event (a delta). It's very sensitive to ordering — if one event is lost or the order is broken, the consumer's state goes wrong. So I prefer the full state.
Sign up to mark studied questions and save your progress.