Goal
Make async message changes safe and fast by treating each event type/topic as a versioned contract that is:
- Defined (schema registry),
- Exemplified (golden events),
- Executable (replay harness),
- Enforced (CI compatibility gates).
1) Schema registry (the contract source of truth)
What to put in the registry
- One subject per event type (preferred) or per topic-value (common in Kafka).
- A stable envelope plus payload:
event_type, event_version (semantic), id, occurred_at, producer, trace, etc.
- payload schema: Avro / Protobuf / JSON Schema.
Rules that make evolution work
- Prefer additive changes:
- Add optional fields with defaults (Avro) / optional fields (Proto3 with care) / nullable+default (JSON).
- Never repurpose meaning of existing fields.
- Deprecate fields before removal; removal only after consumers stop reading.
- Document invariants not captured by schema (e.g., “amount >= 0”, “currency ISO-4217”).
Tooling
- Avro + Confluent/Apicurio/Karapace registry compatibility checks.
- Protobuf:
buf breaking is effectively a schema-compat gate.
- JSON Schema:
ajv validation + a diff/breaking-check tool (less standardized; you may rely more on golden/replay tests).
2) Golden events (canonical fixtures)
Golden events are real serialized messages (not just JSON “examples”), stored in git and versioned with the contract.
What to include
- At least:
- “Happy path” message
- Boundary values (empty arrays, long strings, zero, etc.)
- Nullability/optional-field permutations
- Older versions still expected in the wild (if retention/replay exists)
- Store both:
- Payload bytes (e.g., Avro/Proto binary or JSON text) and
- Headers/metadata your consumers depend on (content-type, schema id, event_version, correlation id).
Format suggestion (repo layout)
contracts/
user.created/
schema/...
golden/
v1/
001-happy.bin
002-missing-optional.bin
v2/
001-added-field.bin
invariants.md
How to generate
- Producer-side “fixture generator” test that:
- Builds event objects,
- Serializes with the real serializer,
- Writes fixtures (and fails if they would change unexpectedly unless explicitly updated).
3) Replay harness (executable contract tests)
A replay harness is a test runner that:
- Spins up an ephemeral broker (Kafka/Rabbit/etc.),
- Publishes golden events,
- Runs the consumer service (or its message handler) against them,
- Asserts on outcomes.
Key design choices
- Run consumer as close to production as possible:
- Prefer running the actual service container with real config, DB mocked/ephemeral as needed.
- Assertions:
- DB state changes,
- emitted downstream calls (captured via WireMock),
- emitted output events (assert published messages),
- logs/metrics only if necessary.
Common implementation
- Docker Compose or Testcontainers for broker + schema registry.
- A small runner that:
- loads golden fixtures,
- publishes to topic/queue with expected headers,
- waits for consumer to acknowledge/process,
- verifies side effects.
4) Compatibility gates (CI enforcement)
You want multiple, fast gates—schema-level first, executable next.
Gate A — Schema compatibility (fast, mandatory)
On producer PRs:
- Check new schema vs registry “latest” using required mode:
- BACKWARD (new consumers can read old data) is often minimum.
- FULL (backward + forward) if you need bi-directional safety.
- Fail build if incompatible.
On consumer PRs:
- Validate consumer’s deserializer can accept latest schema(s) + any still-in-flight older ones.
Gate B — Golden-event replay (consumer safety)
On consumer PRs:
- Replay all golden events for that topic/event type.
- Must process without errors and satisfy assertions.
This catches real-world breakages that schema checks miss:
- required headers missing,
- enum mapping behavior,
- “stringly typed” fields,
- semantic invariants.
Gate C — “Can I break an old consumer?” (producer safety, optional but strong)
Two common patterns:
1) Compile-and-run previous consumer in CI
- Keep a “consumer snapshot” container tag (e.g., last released).
- In producer PR pipeline:
- start old consumer container,
- publish producer’s newly generated events,
- assert old consumer doesn’t fail (at least no deserialization/handler crash).
2) Rely on FULL compatibility + invariants
- Less coverage than actually running old consumer, but simpler.
Gate D — Contract publication/versioning
- Merge to main publishes:
- schema to registry,
- golden events to an artifact store (or git main),
- a contract version tag.
- Services pin to a contract version (or “latest compatible”) depending on your release model.
Recommended workflow (practical)
When changing an event
- Update schema (add fields, defaults, doc).
- Add/refresh golden events (including prior versions if still relevant).
- Producer PR must pass:
- schema-compat gate,
- fixture generation (golden updates intentional),
- optional old-consumer check.
- Consumer PR must pass:
- replay harness over the full golden suite,
- any semantic assertions.
Notes that prevent pain later
- Keep an explicit
event_version even if using registry schema IDs—humans/debugging need it.
- Make consumers tolerant: ignore unknown fields, don’t crash on new enum values (map to
UNKNOWN).
- Prefer “don’t validate too much at the boundary”: deserialize + basic checks, push deeper validation into domain logic where you can handle errors gracefully.
- Golden events should be sanitized (no PII) but structurally realistic.
If you tell me your broker (Kafka/Rabbit/SNS-SQS), schema format (Avro/Proto/JSON), and stack (JVM/Node/Go/.NET), I can outline a concrete CI pipeline and a minimal replay harness structure for that ecosystem.