Prompt
How do I handle event versioning in a real-time pipeline?
Latest observation
Handling event versioning in a real-time pipeline usually comes down to one rule:
Make producers and consumers able to evolve independently without breaking each other.
Here’s a practical way to do it.
1. Prefer backward-compatible changes
When you change an event schema, try to make changes that old consumers can tolerate.
Good changes:
- Add optional fields with defaults
- Add new event types
- Add new fields without removing old ones
- Deprecate fields gradually
Risky changes:
- Renaming a field
- Changing a field’s meaning
- Changing a field’s type
- Removing a field
- Reusing a field for a different purpose
2. Use explicit schema versioning
Include a version in the event itself or in the schema registry.
Common patterns:
eventVersion: 1schemaVersion: 3- Version embedded in the event name, like
order.created.v2
Best practice:
- Keep the version in the payload or metadata
- Don’t rely only on topic names for versioning unless you have a strong operational reason
3. Use a schema registry if possible
If you use Kafka, Pulsar, or similar systems, a schema registry helps a lot.
Benefits:
- Enforces compatibility rules
- Centralizes schema history
- Makes producer/consumer contracts visible
- Prevents accidental breaking changes
Typical compatibility modes:
- Backward compatible: new consumers can read old events
- Forward compatible: old consumers can read new events
- Full compatibility: both directions
For real-time systems, backward compatibility is usually the minimum goal.
4. Design consumers to tolerate unknowns
Consumers should be resilient to:
- Extra fields
- Missing optional fields
- New enum values
- Out-of-order delivery
- Duplicate messages
Implementation tips:
- Ignore fields you don’t recognize
- Use default values
- Treat unknown enum values as “other”
- Validate only what you truly need
5. Avoid breaking changes by creating new event versions when needed
If a change is truly incompatible, introduce a new version side-by-side.
Example:
UserCreatedV1UserCreatedV2
Or:
- Same event type, but
version=2
Then:
- Keep producing v1 and v2 for a transition period if necessary
- Migrate consumers gradually
- Retire old versions after adoption
6. Use translation/adapters at the boundary
A useful pattern is to keep internal processing on one canonical version and translate at ingress/egress.
Examples:
- Ingest v1 and v2, normalize both into a canonical internal model
- Publish only the current version externally
- Use adapters to map old schemas to the new format
This reduces version branching inside business logic.
7. Version based on semantic meaning, not just structure
Sometimes a schema change is technically small but semantically breaking.
Example:
status: "active"changes from meaning “paid and active” to “trial and active”
Even if the field structure is unchanged, that is effectively a version change because consumers may interpret it differently.
8. Keep event names stable when possible
If your topic or stream name is tied to business meaning, changing it too often creates operational pain.
Better:
- Keep topic names stable
- Version the payload or schema
Use new topics only when:
- The event semantics change significantly
- Isolation is needed for scaling or access control
- Migration requires parallel pipelines
9. Add observability and contract tests
Versioning issues are easier to catch early if you:
- Log schema version
- Track consumer lag and decode failures
- Alert on parsing errors
- Run contract tests between producers and consumers
- Test replaying historical events against current consumers
10. Plan for replay and long-lived data
In real-time pipelines, old events often get replayed. That means current consumers may see very old versions.
So:
- Keep old schemas supported for as long as those events can reappear
- Store migration logic where replay can access it
- Don’t assume “nobody will read old messages”
A practical strategy
If you want a simple default approach:
- Add a
versionfield to every event - Only make backward-compatible changes by default
- Use a schema registry with compatibility checks
- Normalize old versions in consumer adapters
- Introduce a new version only for breaking changes
- Deprecate old versions gradually
Example
Suppose OrderPlaced v1 is:
{
"eventType": "OrderPlaced",
"version": 1,
"orderId": "123",
"customerId": "456",
"total": 99.95
}
Later you want to add currency:
{
"eventType": "OrderPlaced",
"version": 2,
"orderId": "123",
"customerId": "456",
"total": 99.95,
"currency": "USD"
}
A v1 consumer can still ignore currency if it doesn’t need it, or you can keep currency optional with a default to preserve compatibility.
Rule of thumb
If a consumer can safely ignore the change, evolve in place. If not, create a new version and migrate carefully.
If you want, I can also show:
- a Kafka-specific versioning strategy,
- how to do this with Avro/Protobuf/JSON Schema,
- or a migration playbook for breaking event changes.