liveBook · Manning

Schema

As explained in Apache Kafka in Action

A schema is a formal blueprint that defines the structure, fields, types, and rules of data.

A supermarket publishes product records to a Kafka topic. Each record has an ID, name, price, and stock level. A schema makes that expected structure explicit: it is a blueprint for the data that producers write and consumers read. Even without a documented schema, a consumer still assumes one. If the data violates that assumption, the consumer may fail—or silently calculate a wrong result. section 13.1.1 section 13.1

Schemas matter for binary formats such as Avro and Protobuf, which need one for interpretation. They can also describe JSON or XML. Choose a consistent data format across topics, and keep each topic’s schema discoverable and documented, whether in a wiki, Git repository, or dedicated platform. section 3.2.2 section 13.1.1

A product schema becomes a producer–consumer contract when requirements change:

  • Adding a category may be harmless to consumers that ignore unknown fields, but a new consumer reading older records will not find it there. section 13.1.1
  • Removing stock can break existing consumers; adding a currency to price can affect both old and new consumers. section 13.1.1
  • Decide early which changes are compatible and which require agreement. Optional fields can legitimately appear in some messages and be absent from others. section 13.1.1 section 3.2.2
{
  "type": "record",
  "name": "Product",
  "namespace": "com.example.supermarket",
  "fields": [
    {"name": "id", "type": "string"},
    {"name": "name", "type": "string"},
    {"name": "price", "type": "float"},
    {"name": "stock", "type": "int"},
    {"name": "origin", "type": ["null", "string"], "default": null}
  ]
}

This Avro product schema names each field and its type. Its origin field permits null and defaults to null, illustrating how a schema can accommodate an optional value. section 13.1.4

Avro can pair a schema with each payload, making a message self-describing. Repeating that schema is costly: one illustrated payload is 35 bytes while its schema is 292 bytes. A schema registry avoids sending the full schema each time; the Kafka message carries a schema ID instead. This keeps messages smaller while identifying the schema used for each message, even when versions coexist in a topic. section 13.1.4

How Apache Kafka in Action covers it

How deep it goes
Explains implicit schemas, field-level changes, compatibility concerns, and Avro schema IDs; stops short of a schema-registry configuration procedure.
Examples
Uses supermarket product records, proposed field changes, and a typed Avro product schema with an optional field.
What it assumes
Assumes familiarity with records, fields, and Kafka producers and consumers, while defining why even undocumented data has a schema.
What you can do after
Evaluate/understand schema changes against consumer compatibility and choose whether documented schemas and a registry address topic needs.
How it connects
Connects schemas to Kafka topics, data formats, producer–consumer contracts, schema evolution, and schema registries.

Also explained in

Compare how 44 books explain schema →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Apache Kafka in Action. The section links point to the parts of the book it draws on.