1

Imagine there are to separate apps: producer and consumer.

The code of producer:

import os

from confluent_kafka import avro
from confluent_kafka.avro import AvroProducer

avsc_dir = os.path.dirname(os.path.realpath(__file__))
value_schema = avro.load(os.path.join(avsc_dir, "basic_schema.avsc"))

config = {'bootstrap.servers': 'localhost:9092', 'schema.registry.url': 'http://0.0.0.0:8081'}

producer = AvroProducer(config=config, default_value_schema=value_schema)
producer.produce(topic='testavro', value={'first_name': 'Andrey', 'last_name': 'Volkonsky'})

basic_schema.avsc file is located within producer app. Its content:

{
    "name": "basic",
    "type": "record",
    "doc": "basic schema for tests",
    "namespace": "python.test.basic",
    "fields": [
        {
            "name": "first_name",
            "doc": "first name",
            "type": "string"
        },
        {
            "name": "last_name",
            "doc": "last name",
            "type": "string"
        }
    ]
}

For now it does not matter what's inside consumer.

We run producer once and everything is ok. Then I want to add age field:

basic_schema.avsc:

{
    "name": "basic",
    "type": "record",
    "doc": "basic schema for tests",
    "namespace": "python.test.basic",
    "fields": [
        {
            "name": "first_name",
            "doc": "first name",
            "type": "string"
        },
        {
            "name": "last_name",
            "doc": "last name",
            "type": "string"
        },
        {
             "name": "age",
             "doc": "age",
             "type": "int"
        }
    ]
}

Here I got error:

confluent_kafka.avro.error.ClientError: Incompatible Avro schema:409

They say here https://docs.confluent.io/platform/current/schema-registry/avro.html#summary that for compitability type == BACKWARD consumers should be updated first.

I cannot understand technically. I mean do I have to copy basic_schema.avsc file to consumer and run it?

1 Answers1

2

If you registered schema with BACKWARDS compatibility (the default), confluent schema registry simply wont allow you to make an incompatible change - adding a mandatory field.

you can add optional field or use forward compatibility

the rules about what should be upgraded first is correct regardless of what changes the compatibility rule actually allows you to make.

edit - additional info

  • don't use forward compatibility simply because you might have a need to add mandatory fields. Use the compatibility that makes sense for your case based on who can update first e.g. it may be impossible to make all producers upgrade at the same time.
  • so if using backwards compatibility AND need to add a mandatory field, you probably need a new version of the service e.g. topic.v1 and topic.v2 where v2 of the service uses the schema with the new mandatory field and you can deprecate v1 service...for example
shnplr
  • 230
  • 3
  • 12