06 Cloudevents And Message Contracts

CloudEvents And Message Contracts

Every message on the Klok broker should be wrapped in a CloudEvents envelope. The envelope gives routers, middleware, observability tools, and operators standard metadata without requiring them to understand the business payload.

Why CloudEvents

CloudEvents provide a shared vocabulary for:

  • Event identity.
  • Event type.
  • Event source.
  • Timestamp.
  • Content type.
  • Correlation and tracing metadata.
  • Payload schema or model identity.

This makes message handling more uniform across route types and systems.

Example Envelope

{
  "specversion": "1.0",
  "type": "com.acme.finance.InvoiceCreated",
  "source": "urn:klok:acme:ACME001:ACME001I1_InvoiceOnramp",
  "id": "8b77d8b6-8c7d-4a1d-8b3e-1f8f17a0f1d0",
  "time": "2026-05-11T10:15:00Z",
  "datacontenttype": "application/json",
  "data": {
    "invoiceId": "INV-1001"
  }
}

The framework usually wraps business payloads for you. Developers should understand the envelope, but should not build custom envelope behavior unless they are implementing platform-level framework code.

Event Type And Payload Model

The event type describes what happened. The payload model describes the data shape.

Good examples:

  • InvoiceCreated event using InvoiceV1 payload.
  • CustomerChanged event using CustomerGlobalV1 payload.
  • SchedulePublished event using PublishedScheduleV2 payload.

Avoid generic event types like DataChanged unless the context is extremely narrow.

Correlation

Correlation IDs allow a single business flow to be followed across onramp, broker, processing, and offramp. Logs, traces, route events, task runs, and failure events should preserve correlation.

Operational fields often include:

  • tenant.
  • domain.
  • environment.
  • solutionId.
  • integrationId or routeId.
  • correlationId.
  • e2eId.
  • topic.
  • modelName and modelVersion.

Contract Checklist

  • [ ] Event type is meaningful.
  • [ ] Payload model includes major version.
  • [ ] Topic name points to the right tenant, environment, domain, model, and purpose.
  • [ ] CloudEvent source identifies the emitting route.
  • [ ] Correlation can be followed in logs and traces.
  • [ ] Failure events are standardized and observable.