Prompt
How do I fix schema compatibility issues in Kafka?
Latest observation
Schema compatibility issues in Kafka usually happen when producers and consumers disagree on the shape of the data in the topic, especially when using a schema registry with Avro, Protobuf, or JSON Schema.
Here’s how to fix them:
1) Identify the exact incompatibility
Check:
- What changed in the schema
- Who broke first: producer or consumer
- What compatibility mode is set in Schema Registry:
BACKWARDFORWARDFULL- and their transitive variants
Common breaking changes:
- Renaming a field without a default/alias
- Removing a required field
- Changing field type incompatibly
- Changing union/enum values incorrectly
- Making a previously optional field required
2) Use backward-compatible schema changes
The safest approach is to evolve schemas in a way older consumers can still read.
Safe-ish changes
- Add a new field with a default value
- Make a field optional instead of required
- Add enum values carefully if supported by your schema rules
- Keep field names stable
Usually breaking changes
- Delete fields that old consumers expect
- Rename fields without aliases
- Change
stringtointor similar incompatible type changes - Change requiredness in a way that old readers can’t handle
3) Fix the schema versioning strategy
If a new schema breaks compatibility:
- Revert to the last compatible schema
- Create a new schema version that preserves old fields
- Use aliases/defaults where supported
- Avoid “big-bang” schema redesigns on shared topics
4) Check Schema Registry compatibility settings
If compatibility is too strict or too loose, adjust it intentionally.
Examples:
BACKWARDis common for producers evolving while old consumers remain functionalFULLis stricter and checks both backward and forward compatibilityNONEdisables checks, but this is risky
You can set compatibility at:
- Global level
- Subject level
- Per-topic subject
5) Regenerate and redeploy producers/consumers
After fixing the schema:
- Regenerate client classes if using code generation
- Redeploy producers with the corrected schema
- Update consumers to handle the new version
- Test against a staging schema registry if possible
6) Validate with compatibility tooling
Before deploying, test schema changes with:
- Schema Registry compatibility API
- CI/CD checks
- Unit tests with sample serialized records
- Consumer integration tests
7) If data is already in the topic
If bad records are already published:
- Create a consumer that can handle both old and new schema versions
- Use schema aliases/defaults for migration
- Reprocess or backfill data if needed
- In worst cases, write to a new topic and migrate consumers gradually
Example
If you have this Avro schema:
{
"type": "record",
"name": "User",
"fields": [
{ "name": "id", "type": "string" },
{ "name": "email", "type": "string" }
]
}
And you want to add age, do:
{
"type": "record",
"name": "User",
"fields": [
{ "name": "id", "type": "string" },
{ "name": "email", "type": "string" },
{ "name": "age", "type": "int", "default": 0 }
]
}
That is typically backward compatible.
Best practices
- Never assume all consumers upgrade at the same time
- Prefer additive schema evolution
- Use schema registry compatibility checks in CI
- Document schema ownership and change process
- Version messages carefully for shared topics
If you want, I can also help you diagnose a specific schema error message or show how to fix compatibility for Avro, Protobuf, or JSON Schema.
Brands