Schema Registry Headers: A GUID on the Kafka Record
A local Docker demo of Confluent Platform 8.4 where Avro records carry a schema GUID in a Kafka header, plus the interop, migration, and client gotchas that come with that wire format.
Overview
You replicate a topic into another cluster and the consumers start failing. The bytes still begin with 0x00 and a schema id, but id 6 in the new registry is a different schema, or nothing at all.
A coat-check stub works the same way. The number only means something at the counter that printed it. An ISBN is a fingerprint of the book, so any library that holds that book can find it.
Confluent Schema Registry’s newer wire format is the ISBN. The classic format stamps a magic byte and a 4-byte numeric id on the front of the Avro payload. That id is a counter in that registry’s _schemas topic. The header format leaves the payload as bare Avro and puts a schema GUID in the record header __value_schema_id. The GUID fingerprints the schema content, including references, rules, and metadata, so the same schema has the same GUID in every registry that stores it.
This repo is that format on a laptop: Kafka in KRaft mode, Schema Registry, and a Python container that runs seven scripts. No cloud account and no credentials.
How it works
./run.sh starts the stack and runs all seven parts. Kafka is on localhost:9092. Schema Registry is on http://localhost:8081. Each part resets its own topics and subjects, so you can rerun them in any order.
Two ways a record can name its schema:
- Classic prefix. The payload is
[0x00][schema id, 4 bytes big-endian][Avro body]. Python’s default serializer isprefix_schema_id_serializer. - GUID header. The payload is the Avro body alone. The header
__value_schema_idis[0x01][16-byte GUID]. Keys use__key_schema_id. For Protobuf, message indexes follow the GUID inside the header value. Python opts in withheader_schema_id_serializer.
The deserializer default is dual_schema_id_deserializer. It checks the header first, then falls back to the payload prefix. That fallback is what makes a staged migration possible.
producer
header_schema_id_serializer
|
v
Kafka record
header __value_schema_id = 0x01 + GUID
payload bare Avro
|
v
dual deserializer
header present --> GET /schemas/guids/{guid}
else --> read 0x00 + 4-byte id from the payload
Part 5 keeps one consumer open across a cutover on a single topic:
prefix history --> upgrade consumer (dual) --> flip producer (header) --> replay both formats
|
+ a prefix-only consumer fails at the cutover offset
What it demonstrates
- Registry lifecycle. Register a schema, list subjects and versions, and fetch it by subject, version, numeric id, GUID, or latest. Test compatibility, evolve
Userfrom v1 (id,name,email) to v2 (a nullabledepartment, backward compatible), then soft-delete and hard-delete. The call that is new here isGET /schemas/guids/{guid}. - Both wire formats, in hex. One script prints the magic byte and the numeric id in the payload. The next prints
__value_schema_idand the bare Avro body. - The interop matrix. A prefix producer with a prefix consumer passes. A prefix producer with the dual consumer passes. A header producer with the dual consumer passes. A header producer with a prefix-only consumer fails with
Invalid magic byte: the legacy consumer treats the first byte of the Avro body as the magic byte. - Which side you upgrade first. Already on Schema Registry with the payload prefix (the path this demo models): upgrade consumers first. The dual deserializer keeps reading prefix records, then you flip producers. Flip producers first and a legacy consumer dies mid-stream at the cutover offset, during the read. Raw Avro that never had Schema Registry is the other starting point: the payload is bare Avro before and after, the header is additive, and producers can move first.
- A record that carries both formats. No standard serializer emits both. A proxy, a replicator, a Kafka Streams job, or a custom producer can copy an inbound
__value_schema_idonto a payload it just re-serialized with a prefix. The dual deserializer trusts the header and passes the entire payload, framing included, to the Avro decoder. The demo record{id: 99, name: radia perlman, ...}comes back as{id: 0, name: '', email: ''}. No exception. Avro reads the framing as field data and stops when the record looks complete. Java’sDualSchemaIdDeserializerreturns that full payload too. If the prefix says v1 and the header says v2, the header still wins, and v2’sdepartmentappears on a payload written with v1.
Technical details
- Stack. Docker Compose runs
cp-kafka:8.4.0(KRaft, single broker),cp-schema-registry:8.4.0at backward compatibility, and Python 3.12 withconfluent-kafka[avro,schemaregistry]==2.16.1. Header GUID support landed in Confluent Platform 8.1.1 and in the Python client at 2.10.1. On the JVM the switch isvalue.schema.id.serializer=io.confluent.kafka.serializers.schema.id.HeaderSchemaIdSerializer(and the same property for the key). - Portable identity is the win. A numeric id does not survive a copy into a registry where that counter means something else, so disaster recovery, migration, and multi-region otherwise need id-remapping. A GUID is identical everywhere the schema content is identical. Key bytes stay pure Avro as well, so evolving a key schema leaves the bytes Kafka partitions on unchanged. The README calls that second point narrow, because key schemas rarely evolve.
- The record gets bigger. The payload loses 5 bytes. The header adds a 17-byte key, a 17-byte value, and two length varints, about +32 bytes per record. Over 2000 identical
Userrecords the demo measures 85,988 bytes (prefix) against 149,988 bytes (header) uncompressed, +74%, and 2,363 against 3,241 bytes with zstd, +37%. The header key repeats inside a batch, so compression helps and the overhead remains. - The payload is a bare Avro datum. There is no embedded schema and no sync marker.
avro-tools tojson,avro cat, DuckDB, and Spark refuse it. Fetch the schema by GUID, then use a schemaless reader (fastavro.schemaless_reader, or Java’sGenericDatumReaderwith a binary decoder).kafka-avro-console-consumerunderstands the header format on its own. - Dual-write is not the plan here. Confluent Platform docs describe a dual-write step with
HeaderAndPayloadSchemaIdSerializer. That class is absent from the CP 8.4.0 jars this repo inspected, and a message that carries both formats decodes to the wrong record. The path this demo walks does without it: an upgraded consumer already reads prefix messages.
Challenges & lessons
-
The header wins, and the prefix stays in the bytes. Once
__value_schema_idis present,dual_schema_id_deserializerhas its schema and feeds every payload byte to Avro, so0x00 0x00 0x00becomes id 0 and two empty strings. Strip inbound__key_schema_idand__value_schema_idbefore you re-serialize. On a topic you know is prefix-only, setprefix_schema_id_deserializerso a stray header is ignored. The same bytes decode differently across a mixed consumer fleet. -
Python Avro refuses a header-mode payload of 5 bytes or fewer.
AvroDeserializercheckslen(data) <= 5before it looks at the header and raisesSerializationError, expecting at least 6 bytes of framing. Prefix mode hides this, because the framing alone is 5 bytes. Header mode makes it ordinary: anint, aboolean, an enum, an empty record, or a shortstringencodes to 5 bytes or fewer and is unreadable. A value that encodes to 6 bytes ("abcde", a long epoch millis) passes. The guard lives only in the Python Avro deserializer (_sync/avro.pyand_async/avro.py). The Python JSON Schema and Protobuf deserializers have no such check, and the Java client has no minimum length. Keep keys in prefix mode, pick a schema whose body clears 6 bytes, or callschemaless_readeryourself after resolving the GUID.repro/minimal_repro.pyis the standalone reproduction. -
Two
__value_schema_idheaders mean two different schemas. Python walks the header list and honors the first match. Java callsheaders.lastHeader(key)and honors the last. A proxy that appends a second schema-id header will fan the same record out to different schemas. Strip the inbound header. Do not append another one beside it. -
Consumers move first when the payload bytes change. Prefix records are
[0x00][id][Avro]. Header records are bare Avro, so a prefix-only consumer breaks on the first new record, at that offset. The dual deserializer covers the old log while producers are still on the prefix. If the topic was raw Avro with no Schema Registry, the payload bytes do not change and producers can go first. -
The header has to survive every hop. A proxy or connector that strips headers drops the only schema id the new format has. Kafka Streams dropped headers before CP 8.3. From 8.3,
dsl.store.format=HEADERSkeeps them. The numeric id still exists on the schema in the registry. The GUID is what this format puts on the wire.