Quick revision
The short answer
A schema registry is a service that stores versioned schemas per subject and checks each new version for compatibility with the earlier ones before accepting it. When KafkaAvroSerializer sends a record, it registers or looks up the schema, gets its numeric id, and writes a magic byte (0), the 4-byte schema id and then the compact Avro binary payload. The deserializer reads the id, fetches the writer's schema from the registry once and caches it, and resolves it against the schema the consumer expects.
What to remember
In this answer, we saw that a schema registry stores versioned schemas per subject, with <topic>-value and <topic>-key as the default subjects, and checks compatibility when a new version is registered. KafkaAvroSerializer registers or looks up the schema and writes a magic byte 0, a 4-byte schema id and the Avro binary payload. The deserializer fetches the writer schema by id once, caches it and resolves it against the reader schema. Because field names live in the registry rather than in each record, Avro payloads are much smaller than JSON.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A schema registry is a service that stores versioned schemas per subject and checks each new version for compatibility with the earlier ones before accepting it. When KafkaAvroSerializer sends a record, it registers or looks up the schema, gets its numeric id, and writes a magic byte (0), the 4-byte schema id and then the compact Avro binary payload. The deserializer reads the id, fetches the writer's schema from the registry once and caches it, and resolves it against the schema the consumer expects.
Why this question is asked
Kafka itself knows nothing about the shape of our data: keys and values are just bytes. As soon as several teams share a topic, someone needs to agree what those bytes mean and how that meaning may change over time. Interviewers ask about the schema registry because it is how most Avro-based systems solve this, and because knowing the wire format helps when debugging 'Unknown magic byte' errors. In this answer, we will recap Avro and schemas, see what the registry stores and checks, follow one record through the serializer byte by byte, see what the deserializer does with it, and understand why Avro payloads are so small.
1A Quick Recap of Avro and Schemas
Avro is a data serialization format: a way to turn objects into bytes and back. Every Avro record is described by a schema, written in JSON, that lists its fields in order with their types and optional default values. The binary encoding contains only the values, written one after another in schema order, with no field names and no type markers.
That makes Avro data small and fast, but it also means the bytes are meaningless without the exact schema used to write them, called the writer schema. A consumer that wants to read the data needs that writer schema, plus its own reader schema, the shape its code expects. Avro's schema resolution rules then map one onto the other: fields in both are copied, fields only in the writer are skipped, and fields only in the reader get their defaults.
2What the Registry Stores and Checks
The schema registry, such as Confluent Schema Registry or a compatible service, is a small REST service that stores schemas. Schemas are grouped into subjects, and each subject holds an ordered list of versions: version 1, version 2 and so on. Every distinct schema also gets a globally unique numeric id.
Which subject a record's schema belongs to is decided by a subject naming strategy. The default, TopicNameStrategy, uses the topic name plus -value for values and -key for keys, so the values of topic customers are registered under customers-value. Other strategies use the record's full name, which allows several event types on one topic.
When a new version is registered, the registry checks it against the subject's compatibility setting, for example BACKWARD, meaning the new schema must be able to read data written with the previous one. An incompatible schema is rejected with an error, so a producer cannot start writing data that existing consumers cannot read.
5 messages in order. Play them, or step through them one at a time.
Later sends with the same schema use the cached id and do not call the registry.
3What Goes on the Wire, Byte by Byte
Now we send a record. With the default settings (auto.register.schemas=true), KafkaAvroSerializer takes the schema of our object, registers it under the subject if it is new, or looks up its id if it already exists, and caches the result so later records cost no network call. Then it builds the value bytes in three parts.
The first byte is the magic byte, always 0 in this format, which says 'Confluent wire format, version 0'. The next four bytes are the schema id as a big-endian integer. Everything after that is the Avro binary encoding of the record.
Notice that the payload contains no field names and no braces. The same record as JSON, {"id":42,"name":"Ann","tier":"GOLD"}, is 36 bytes. That difference is why Avro payloads are compact: field names, which JSON repeats in every single record, live once in the registry instead. Small numbers are also written in as few bytes as possible using variable-length encoding.
This also explains a common error. If a consumer configured with KafkaAvroDeserializer reads a record that some other producer wrote as plain JSON, the first byte is 0x7B, the brace, not 0, and the deserializer fails with an 'Unknown magic byte' error. Newer serializer versions can carry the schema id in a record header instead of the payload prefix, so check your version if you see a different layout.
4What the Deserializer Does
On the consumer side, KafkaAvroDeserializer reverses the process. It checks the magic byte, reads the 4-byte schema id, and asks the registry for the schema with that id, which is the writer schema. It caches the result, so in practice the registry is called once per schema id per consumer, not once per record.
Then it decodes the Avro bytes. If the consumer uses generated classes, with specific.avro.reader=true, the reader schema is the one compiled into the class, and Avro resolves the writer schema against it. If the producer has moved to a newer schema with an extra field, an older consumer skips that field; if the producer still uses an older schema without a field the consumer's class has, the consumer fills in the default. Without specific.avro.reader, the deserializer returns a GenericRecord built with the writer schema.
5Follow-up questions
Should producers auto-register schemas in production?
Many teams turn auto.register.schemas off in production and register schemas from CI instead. Then a schema change is reviewed and checked for compatibility before deployment, and a misconfigured producer cannot register an unexpected version at runtime. The producer then only looks up the id, with use.latest.version or an exact match depending on configuration.
What happens if the registry is down?
Producers and consumers keep working for schemas they have already cached. A producer sending a schema it has not seen yet, or a consumer meeting a new schema id, fails until the registry is back. So the registry should run with several instances, and its own data lives in a compacted Kafka topic, _schemas, which makes it recoverable.
Does the registry check compatibility for every record?
No. Compatibility is checked only when a new schema version is registered. Sending records with an already registered schema is just an id lookup, usually from the local cache.
6Conclusion
In this answer, we saw that a schema registry stores versioned schemas per subject, with <topic>-value and <topic>-key as the default subjects, and checks compatibility when a new version is registered. KafkaAvroSerializer registers or looks up the schema and writes a magic byte 0, a 4-byte schema id and the Avro binary payload. The deserializer fetches the writer schema by id once, caches it and resolves it against the reader schema. Because field names live in the registry rather than in each record, Avro payloads are much smaller than JSON.
Open with
A schema registry is a service that stores the schemas for Kafka data, grouped into subjects with numbered versions, and every distinct schema gets a global numeric id.
Then, if they want more
With the default TopicNameStrategy, the subject is the topic name plus -value or -key. When a new version is registered, the registry checks it against the subject's compatibility mode, for example BACKWARD, and rejects it if existing data couldn't be read.
When KafkaAvroSerializer sends a record, it registers the schema or looks up its id, caching the result, and then writes a magic byte of zero, the four-byte schema id in big-endian, and then the Avro binary encoding of the record.
The Avro part has no field names, just the values in schema order with variable-length numbers, so a small record might be fifteen bytes where the JSON would be over thirty.
On the consumer, KafkaAvroDeserializer reads the magic byte and the id, fetches the writer schema from the registry the first time and caches it, and then resolves it against the reader schema, which with specific.avro.reader is the generated class. That resolution is what lets producers and consumers evolve separately: new fields get skipped or defaulted.
And if someone writes plain JSON to the topic, the deserializer fails with an unknown magic byte error, which is a handy clue when debugging.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation, Spring for Apache Kafka reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does a schema registry do in a Kafka system using Avro, and what happens on the...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/e23009ec27/