📖 Scenario: You are working with Apache Kafka and using Avro schemas to manage your data formats. You want to ensure that when you update your data schema, it remains compatible with previous versions so that your Kafka consumers do not break.
🎯 Goal: Build a simple example to understand how schema compatibility rules work in Kafka by defining schemas and checking compatibility.
📋 What You'll Learn
Create an initial Avro schema as a JSON string
Define a compatibility rule variable
Write a function to check if a new schema is compatible with the old schema based on the rule
Print the compatibility check result
💡 Why This Matters
🌍 Real World
In real Kafka projects, schema compatibility ensures that producers and consumers can evolve independently without breaking data processing.
💼 Career
Understanding schema compatibility is essential for data engineers and backend developers working with Kafka and schema registries to maintain reliable data pipelines.
Progress0 / 4 steps
1
Create the initial Avro schema
Create a variable called old_schema and assign it this exact JSON string representing an Avro schema: {"type": "record", "name": "User", "fields": [{"name": "name", "type": "string"}, {"name": "age", "type": "int"}]}
Kafka
Hint
Use a string variable to store the JSON schema exactly as shown.
2
Define the compatibility rule
Create a variable called compatibility_rule and set it to the string BACKWARD to represent the schema compatibility mode.
Kafka
Hint
Set the compatibility rule variable to the exact string 'BACKWARD'.
3
Write a function to check schema compatibility
Write a function called is_compatible that takes two parameters: old_schema and new_schema. Inside the function, return True if compatibility_rule is BACKWARD and the new schema adds a new optional field called email. Otherwise, return False. Use simple string checks to simulate compatibility.
Kafka
Hint
Check if the compatibility rule is 'BACKWARD' and if the new schema string contains the field 'email'.
4
Check and print schema compatibility
Create a variable called new_schema with this exact JSON string: {"type": "record", "name": "User", "fields": [{"name": "name", "type": "string"}, {"name": "age", "type": "int"}, {"name": "email", "type": ["null", "string"], "default": null}]}. Then print the result of calling is_compatible(old_schema, new_schema).
Kafka
Hint
Use the exact new schema string and print the result of the compatibility function.
Practice
(1/5)
1. What does the BACKWARD compatibility rule in Kafka Schema Registry ensure?
easy
A. Both old and new schemas can read each other's data
B. Old schema can read data produced with the new schema
C. No compatibility checks are enforced
D. New schema can read data produced with the previous schema
Solution
Step 1: Understand BACKWARD compatibility meaning
BACKWARD compatibility means new schema can read data written with the old schema.
Step 2: Compare options with definition
New schema can read data produced with the previous schema matches this definition exactly, others describe different rules.
Final Answer:
New schema can read data produced with the previous schema -> Option D
Quick Check:
BACKWARD = New reads old [OK]
Hint: Backward means new schema reads old data [OK]
Common Mistakes:
Confusing backward with forward compatibility
Thinking backward means old reads new
Assuming no compatibility checks
2. Which of the following is the correct way to set schema compatibility to FULL using Kafka Schema Registry REST API?
easy
A. POST /config -d '{"compatibilityLevel": "FULL"}'
B. PUT /config -d '{"compatibilityLevel": "FULL"}'
C. PUT /config -d '{"compatibility": "FULL"}'
D. GET /config -d '{"compatibility": "FULL"}'
Solution
Step 1: Identify correct HTTP method and endpoint
Setting compatibility uses PUT on /config endpoint.
Step 2: Check correct JSON field name
The field is "compatibilityLevel", not "compatibility".
Final Answer:
PUT /config -d '{"compatibilityLevel": "FULL"}' -> Option B
Quick Check:
PUT + compatibilityLevel = FULL [OK]
Hint: Use PUT and compatibilityLevel field [OK]
Common Mistakes:
Using POST instead of PUT
Using wrong JSON field name
Using GET method to set config
3. Given the following schemas, which is the most restrictive compatibility rule that will allow the new schema to be registered without error?
5. You have a Kafka topic with schema compatibility set to FORWARD. You want to add a new optional field to the schema without breaking existing consumers. Which approach should you take?
hard
A. Add the new field with a default value or make it nullable
B. Remove an existing required field to simplify the schema
C. Add the new field without a default value
D. Change the type of an existing field to a different primitive type
Solution
Step 1: Understand FORWARD compatibility
FORWARD compatibility means old schema can read data written with new schema.
Step 2: Determine safe schema changes
Adding optional fields with defaults or nullable types preserves forward compatibility.
Step 3: Evaluate options
Only Add the new field with a default value or make it nullable adds a field safely; others break compatibility or cause errors.
Final Answer:
Add the new field with a default value or make it nullable -> Option A
Quick Check:
Forward compatible additions need defaults or nullable [OK]
Hint: Add optional fields with defaults for forward compatibility [OK]
Common Mistakes:
Adding required fields without defaults
Removing fields under forward compatibility
Changing field types without compatibility checks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.