Skip to main content
Version: 22.3

Schema Registry

Redpanda's schema registry provides the interface to store and manage event schemas. Producers and consumers register and retrieve the schemas they use from the registry. Schemas are versioned, and the registry supports configurable compatibility modes between schema versions. When a producer or a consumer makes a request to register a schema change, the registry checks for schema compatibility and returns an error for an incompatible change.

The schema registry has API endpoints that allow you to perform the following tasks:

  • Query supported serialization formats.
  • Register schemas for a subject.
  • Retrieve schemas of specific versions.
  • Retrieve a list of subjects.
  • Retrieve a list of schema versions for a subject.
  • Configure schema compatibility checking.
  • Delete schemas from the registry.

Schema

A schema is an external mechanism to describe the structure of data and its encoding.

Using schemas provides multiple benefits:

  • Enables a loosely coupled, data-centric architecture that minimizes dependencies in code, between teams, and between producers and consumers.
  • Provides human-readable documentation for an API.
  • Verifies that data conforms to an API.
  • Supports the generation of serializers for data.
  • Manages the compatibility of evolving APIs, allowing new versions of services to be rolled out independently.

Serialization format

A data serialization format defines how data is converted to the bits that are stored and transmitted.

Redpanda's schema registry supports the following data serialization formats for its schemas:

  • Avro
  • Protobuf

Redpanda design overview

Redpanda built the schema registry directly into the Redpanda binary. The schema registry runs out-of-the-box with Redpanda's default configuration, and it requires no new binaries to install and no new services to deploy or maintain.

Every broker allows mutating REST calls, so there is no need to configure leadership or failover strategies. Schema are stored in a compacted topic, and the registry uses optimistic concurrency control at the topic level to detect and avoid collisions.

Use schema registry

The following examples cover the basic functionality of the Redpanda schema registry based on an example Avro schema called sensor_sample. This schema contains fields that represent a measurement from a sensor for the value of the sensor topic, as defined below.

{
"type": "record",
"name": "sensor_sample",
"fields": [
{
"name": "timestamp",
"type": "long",
"logicalType": "timestamp-millis"
},
{
"name": "identifier",
"type": "string",
"logicalType": "uuid"
},
{
"name": "value",
"type": "long"
}
]
}

Prerequisites

To run the sample commands and code in each example, follow these steps to set up Redpanda and other tools:

  1. Download the jq utility.

  2. Install curl or Python.

    • If using Python, install the Requests module, then create an interactive Python session:

      import requests
      import json
      def pretty(text):
      print(json.dumps(text, indent=2))

      base_uri = "http://localhost:8081"
  3. Install Redpanda with Docker.

  4. Start Redpanda with Docker:

    docker network create redpanda-sr
    docker volume create redpanda-sr
    docker run \
    --pull=always \
    --name=redpanda-sr \
    --net=redpanda-sr \
    -v "redpanda-sr:/var/lib/redpanda/data" \
    -p 8081:8081 \
    -p 8082:8082 \
    -p 9092:9092 \
    --detach \
    docker.vectorized.io/vectorized/redpanda start \
    --overprovisioned \
    --smp 1 \
    --memory 1G \
    --reserve-memory 0M \
    --node-id 0 \
    --check=false \
    --pandaproxy-addr 0.0.0.0:8082 \
    --advertise-pandaproxy-addr 127.0.0.1:8082 \
    --kafka-addr 0.0.0.0:9092 \
    --advertise-kafka-addr redpanda-sr:9092
    • Once Redpanda is running, endpoints are documented at http://localhost:8081/v1.

Query supported schema formats

To get the supported data serialization formats in the schema registry, make a GET request to the /schemas/types endpoint:

curl -s "http://localhost:8081/schemas/types" | jq .

If the request is successful, it returns the supported serialization formats:

[
"PROTOBUF",
"AVRO"
]

Register a schema

A schema is registered in the registry with a subject, which is a name that is associated with the schema as it evolves. Subjects are typically in the form <topic-name>-key or <topic-name>-value.

To register the sensor_sample schema, make a POST request to the /subjects/sensor-value/versions endpoint with the Content-Type application/vnd.schemaregistry.v1+json:

curl -s \
-X POST \
"http://localhost:8081/subjects/sensor-value/versions" \
-H "Content-Type: application/vnd.schemaregistry.v1+json" \
-d '{"schema": "{\"type\":\"record\",\"name\":\"sensor_sample\",\"fields\":[{\"name\":\"timestamp\",\"type\":\"long\",\"logicalType\":\"timestamp-millis\"},{\"name\":\"identifier\",\"type\":\"string\",\"logicalType\":\"uuid\"},{\"name\":\"value\",\"type\":\"long\"}]}"}' \
| jq

If the request is successful, it returns a version id unique for the schema in the Redpanda cluster:

{
"id": 1
}

When you register an evolved schema for an existing subject, the version id is incremented by 1.

Retrieve a schema

To retrieve a registered schema from the registry, make a GET request to the /schemas/ids/<id> endpoint:

curl -s \
"http://localhost:8081/schemas/ids/1" \
| jq .

If the request is successful, it returns the schema:

{
"schema": "{\"type\":\"record\",\"name\":\"sensor_sample\",\"fields\":[{\"name\":\"timestamp\",\"type\":\"long\",\"logicalType\":\"timestamp-millis\"},{\"name\":\"identifier\",\"type\":\"string\",\"logicalType\":\"uuid\"},{\"name\":\"value\",\"type\":\"long\"}]}"
}

List registry subjects

To list all registry subjects, make a GET request to the /subjects endpoint:

curl -s \
"http://localhost:8081/subjects" \
| jq .

If the request is successful, it returns the subject:

[
"sensor-value"
]

Retrieve schema versions of a subject

To query the schema versions of a subject, make a GET request to the /subjects/<subject-name>/versions endpoint.

For example, to get the schema versions of the sensor-value subject:

curl -s \
"http://localhost:8081/subjects/sensor-value/versions" \
| jq .

If the request is successful, it returns the version ID:

[
1
]

Retrieve a schema of a subject

To retrieve a schema associated with a subject, make a GET request to the /subjects/<subject-name>/versions/<version-id> endpoint:

curl -s \
"http://localhost:8081/subjects/sensor-value/versions/1" \
| jq .

If the request is successful, it returns the subject and its associated schema:

{
"subject": "sensor-value",
"id": 1,
"version": 1,
"schema": "{\"type\":\"record\",\"name\":\"sensor_sample\",\"fields\":[{\"name\":\"timestamp\",\"type\":\"long\",\"logicalType\":\"timestamp-millis\"},{\"name\":\"identifier\",\"type\":\"string\",\"logicalType\":\"uuid\"},{\"name\":\"value\",\"type\":\"long\"}]}"
}

To get the latest version, use latest as the version ID:

curl -s \
"http://localhost:8081/subjects/sensor-value/versions/latest" \
| jq .

To get only the schema, append /schema to the endpoint path:

curl -s \
"http://localhost:8081/subjects/sensor-value/versions/latest/schema" \
| jq .

If the request is successful, it returns the schema:

{
"type": "record",
"name": "sensor_sample",
"fields": [
{
"name": "timestamp",
"type": "long",
"logicalType": "timestamp-millis"
},
{
"name": "identifier",
"type": "string",
"logicalType": "uuid"
},
{
"name": "value",
"type": "long"
}
]
}

Configure schema compatibility

As applications change and their schemas evolve, you may find that producer schemas and consumer schemas are no longer compatible. You'll need to decide how you want a consumer to handle data from a producer that uses an older or newer schema.

You can configure different types of schema compatibility, which are applied to a subject when a new schema is registered. The schema registry supports the following compatibility types:

  • BACKWARD (default) - consumers using the new schema (for example, version 10) can read data from producers using the previous schema (for example, version 9).
  • BACKWARD_TRANSITIVE - consumers using the new schema (for example, version 10) can read data from producers using all previous schemas (for example, versions 1-9).
  • FORWARD - consumers using the previous schema (for example, version 9) can read data from producers using the new schema (for example, version 10).
  • FORWARD_TRANSITIVE - consumers using any previous schema (for example, versions 1-9) can read data from producers using the new schema (for example, version 10).
  • FULL - the new schema is compatible with all previous schemas.
  • NONE - no schema compatibility checks are done.

To set the compatibility type for a subject, make a PUT request to /config/<subject-name> with the specific compatibility type:

curl -s \
-X PUT \
"http://localhost:8081/config/sensor-value" \
-H "Content-Type: application/vnd.schemaregistry.v1+json" \
-d '{"compatibility": "BACKWARD"}' \
| jq .

If the request is successful, it returns the new compatibility type:

{
"compatibility": "BACKWARD"
}

If you POST an incompatible schema change, the request returns an error. For example, if you try to register a new schema with the value field's type changed from long to int, and compatibility is set to BACKWARD, the request returns an error due to incompatibility:

curl -s \
-X POST \
"http://localhost:8081/subjects/sensor-value/versions" \
-H "Content-Type: application/vnd.schemaregistry.v1+json" \
-d '{"schema": "{\"type\":\"record\",\"name\":\"sensor_sample\",\"fields\":[{\"name\":\"timestamp\",\"type\":\"long\",\"logicalType\":\"timestamp-millis\"},{\"name\":\"identifier\",\"type\":\"string\",\"logicalType\":\"uuid\"},{\"name\":\"value\",\"type\":\"int\"}]}"}' \
| jq

The request returns this error:

{
"error_code": 409,
"message": "Schema being registered is incompatible with an earlier schema for subject \"{sensor-value}\""
}

For an example of a compatible change, register a schema with the value field's type changed from long to double:

curl -s \
-X POST \
"http://localhost:8081/subjects/sensor-value/versions" \
-H "Content-Type: application/vnd.schemaregistry.v1+json" \
-d '{"schema": "{\"type\":\"record\",\"name\":\"sensor_sample\",\"fields\":[{\"name\":\"timestamp\",\"type\":\"long\",\"logicalType\":\"timestamp-millis\"},{\"name\":\"identifier\",\"type\":\"string\",\"logicalType\":\"uuid\"},{\"name\":\"value\",\"type\":\"double\"}]}"}' \
| jq

A successful registration returns the schema's id:

{
"id": 2
}

Delete a schema

The schema registry API provides DELETE endpoints for deleting a single schema or all schemas of a subject:

  • /subjects/<subject>/versions/<version>
  • /subjects/<subject>

A schema can be soft deleted (impermanently) or hard deleted (permanently), based on the boolean query parameter permanent. A soft deleted schema can be retrieved and re-registered. A hard deleted schema cannot be recovered.

Soft delete a schema

To soft delete a schema, make a DELETE request with the subject and version ID (where permanent=false is the default parameter value):

curl -s \
-X DELETE \
"http://localhost:8081/subjects/sensor-value/versions/1" \
| jq .

If the request is successful, it returns the ID of the soft deleted schema:

1

Doing a soft delete for an already deleted schema returns an error. For example:

{
"error_code": 40406,
"message": "Subject 'sensor-value' Version 1 was soft deleted.Set permanent=true to delete permanently"
}

To list subjects of soft-deleted schemas, make a GET request with the deleted parameter set to true, /subjects?deleted=true:

curl -s \
"http://localhost:8081/subjects?deleted=true" \
| jq .

If the request is successful, it returns all subjects, including deleted ones:

[
"sensor-value"
]

To undo a soft deletion, first follow the steps to retrieve the schema, then register the schema.

Hard delete a schema

caution

Redpanda doesn't recommend hard (permanently) deleting schemas in a production system.

The DELETE APIs are primarily used during the development phase, when schemas are being iterated and revised.

To hard delete a schema, make two DELETE requests with the second request setting the permanent parameter to true, /subjects/<subject>/versions/<version>?permanent=true:

curl -s \
-X DELETE \
"http://localhost:8081/subjects/sensor-value/versions/1" \
| jq .
curl -s \
-X DELETE \
"http://localhost:8081/subjects/sensor-value/versions/1?permanent=true" \
| jq .

If the requests are successful, each request returns the version ID of the deleted schema:

1
1

A request for a hard-deleted schema returns an error. For example:

{
"error_code": 40401,
"message": "Subject 'sensor-value' not found."
}

Cleanup

When you're finished, stop and remove the containers in Docker running Redpanda:

docker stop redpanda-sr
docker rm redpanda-sr
docker volume remove redpanda-sr
docker network remove redpanda-sr

What do you like about this doc?




Optional: Share your email address if we can contact you about your feedback.

Let us know what we do well: