Skip to content

Event contracts and changes

Event contracts

Define what a record reports, what its fields mean, and how readers can use it.

Learn with an AI assistant

Copy the prompt and paste it into a chat with an AI assistant.

You are a tutor for the course "Stream Processing".
Topic: "Event contracts"
In short: Define what a record reports, what its fields mean, and how readers can use it.
Topic text in Markdown: https://stream-processing-systems.github.io/contracts/event-contracts.md
Base your explanations on the topic text. First ask how I want to study: a short summary, a step-by-step walkthrough, or a deep dive with questions that check my understanding. Use the text's example and terms. If you add anything the text does not say, point it out.

The map for electric-car drivers receives a reading from charger-7 with a power value of 2,200. The map uses readings to track periods of use. A report built from the same feed also wants to show how much power each charger was delivering. Does 2,200 describe what the charger delivered at that moment or how much it can deliver at full load?

Both values could be numbers in watts. To use the field, the map and the report need the same answer as the team publishing the readings. An event contract is the agreement between the producer and the readers about what an event reports and how to interpret its fields.

The reading that opened a new session at 09:24 came through feed A, which carries measurements from the city’s chargers. The source field identifies that feed as station-feed-a.

{
"source": "station-feed-a",
"event_id": "obs-724",
"station_id": "charger-7",
"observed_at": "2026-10-04T09:24:00Z",
"status": "charging",
"power_w": 2200
}

Feed A reports that charger-7 was charging at 09:24 and delivering 2,200 watts at the time of the measurement. The report can show that value as a reading. To estimate energy over a period, it would need to account for how much time the readings represent. Adding the readings gives a sum of power values in watts, while energy is measured in units such as watt-hours.

The map received the record at 09:24:20. It puts the reading in a session using observed_at, the event time recorded by the feed. A delayed reading can still belong to an earlier session.

The map groups readings by charger. Each feed assigns its own station IDs, so feed B could also publish a reading for a charger named charger-7. The map groups by the pair (source, station_id) to keep those chargers apart. If two feeds describe the same physical charger, the map needs a separate mapping to combine their readings.

The pair (source, event_id) identifies a particular reading. If feed A sends obs-724 again, that pair stays the same. The next reading from the same charger keeps its station ID and gets a new event ID. The map can use one pair to group readings and the other to recognize repeated deliveries.

CloudEvents, a specification for carrying event context, uses the same scoping rule for the attributes source and id. Producers use a unique pair for each distinct event.1 Our feeds follow that idea with the field names shown above.

A schema describes the structure of a record. It can require station_id and say that power_w, when present, contains a number. Readers also need the team’s rules for using the values.

Field or inputRule for the charger feedHow a reader uses it
status: "charging"The charger was delivering power when measuredThe map uses the reading as evidence of activity at that time
power_w: 2200Instantaneous power in wattsThe report can display 2,200 W for that reading
No power_wThe feed has no power measurement for this readingThe report shows that power is unavailable
No new readingThe map has received nothing more recentThe map can show the last reading and its time. Current availability is unknown without other evidence

Feed A can publish a status while its power measurement is unavailable. That reading can still be useful to the map. A faulty sensor can also send a plausible number of watts. A type check can accept the number without detecting the faulty measurement.

Now suppose the feed team wants to attach the firmware version to each reading. Firmware is the software that runs on the charger. The report could group measurements by firmware, but the archive contains readings recorded without it. The feed and its readers need a rule for both sets of records.

  • Write down the fact a record reports, including when and where it applies.
  • Group readings by (source, station_id). Identify each reading by (source, event_id).
  • A schema checks structure. The contract also defines units and what missing values mean.
  1. CloudEvents 1.0.2, context attributes id and source.

© 2026 Grig Alex · CC BY-NC-SA 4.0