Bird
Raised Fist0
Kafkadevops~20 mins

JSON Schema and Protobuf support in Kafka - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kafka Schema Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Kafka JSON Schema validation code?

Consider a Kafka producer configured to use JSON Schema for message validation. The schema requires a field age of type int and name of type string. What happens when the following message is sent?

Kafka
message = {"name": "Alice", "age": "twenty"}  # age is a string, not int
# Producer sends message to topic with JSON Schema validation enabled
AThe message is rejected with a schema validation error because 'age' is not an integer.
BThe message is accepted and sent successfully despite the type mismatch.
CThe producer crashes with a runtime error due to type mismatch.
DThe message is sent but the 'age' field is automatically converted to integer 0.
Attempts:
2 left
💡 Hint

JSON Schema validation enforces field types strictly before sending.

❓ Predict Output
intermediate
2:00remaining
What is the output of this Protobuf message deserialization in Kafka consumer?

A Kafka consumer receives a Protobuf-encoded message with a field id (int32) and active (bool). The message bytes are corrupted and missing the active field. What happens when the consumer tries to deserialize?

Kafka
message_bytes = b'\x08\x96\x01'  # Only 'id' field present
# Consumer deserializes using Protobuf schema expecting 'id' and 'active'
AThe consumer successfully deserializes with 'active' set to its default value false.
BThe consumer throws a deserialization error due to missing 'active' field.
CThe consumer sets 'active' to true by default.
DThe consumer ignores the message and skips processing.
Attempts:
2 left
💡 Hint

Protobuf fields can be optional and have default values.

🧠 Conceptual
advanced
2:00remaining
Which Kafka schema registry feature supports both JSON Schema and Protobuf?

Kafka Schema Registry supports multiple schema types. Which feature allows seamless use of both JSON Schema and Protobuf schemas in Kafka topics?

AConverting all schemas to Avro format before registration.
BUsing separate schema registries for JSON Schema and Protobuf.
CDisabling schema validation to allow any format.
DMulti-format schema registry with compatibility settings per schema type.
Attempts:
2 left
💡 Hint

Schema Registry supports multiple schema types with compatibility controls.

❓ Predict Output
advanced
2:00remaining
What error does this Kafka Protobuf producer code raise?

Given this Kafka producer code snippet using Protobuf serialization, what error occurs?

Kafka
from kafka import KafkaProducer
from protobuf_example_pb2 import User

producer = KafkaProducer(value_serializer=lambda v: v.SerializeToString())
user = User(id=123)  # 'name' field is required but missing
producer.send('users', user)
AKafkaTimeoutError because producer cannot connect.
BNo error; message is sent with missing 'name' field as empty string.
CRuntimeError due to missing required 'name' field in Protobuf message.
DTypeError because 'user' is not bytes.
Attempts:
2 left
💡 Hint

Protobuf required fields must be set before serialization.

🧠 Conceptual
expert
3:00remaining
How does Kafka handle schema evolution with JSON Schema and Protobuf?

In Kafka, when evolving schemas for topics using JSON Schema and Protobuf, which statement best describes the compatibility guarantees?

AOnly Protobuf supports schema evolution; JSON Schema requires full schema replacement.
BBoth JSON Schema and Protobuf support backward and forward compatibility controlled by Schema Registry settings.
CJSON Schema supports compatibility but Protobuf does not support schema evolution.
DNeither JSON Schema nor Protobuf support schema evolution in Kafka.
Attempts:
2 left
💡 Hint

Schema Registry manages compatibility for multiple schema types.

Practice

(1/5)
1. What is the main purpose of using JSON Schema or Protobuf with Kafka messages?
easy
A. To define the structure and format of messages so producers and consumers agree
B. To encrypt Kafka messages for security
C. To speed up Kafka message delivery by skipping validation
D. To store Kafka messages permanently in a database

Solution

  1. Step 1: Understand message format agreement

    JSON Schema and Protobuf define how messages look so both sender and receiver understand the data.
  2. Step 2: Identify the main purpose

    This agreement prevents errors and confusion when reading messages.
  3. Final Answer:

    To define the structure and format of messages so producers and consumers agree -> Option A
  4. Quick Check:

    Message format agreement = C [OK]
Hint: They define message format so systems agree on data [OK]
Common Mistakes:
  • Thinking JSON Schema encrypts messages
  • Believing Protobuf speeds delivery by skipping validation
  • Confusing schema with storage
2. Which of the following is the correct way to specify a Protobuf schema for a Kafka message?
easy
A. { 'name': 'string', 'id': 'int' }
B. schema { field name: string; field id: int; }
C. stringint
D. message Person { string name = 1; int32 id = 2; }

Solution

  1. Step 1: Recognize Protobuf syntax

    Protobuf uses the 'message' keyword with fields and their types and numbers.
  2. Step 2: Match the correct syntax

    message Person { string name = 1; int32 id = 2; } shows correct Protobuf message definition syntax.
  3. Final Answer:

    message Person { string name = 1; int32 id = 2; } -> Option D
  4. Quick Check:

    Protobuf syntax = B [OK]
Hint: Protobuf uses 'message' keyword and numbered fields [OK]
Common Mistakes:
  • Confusing JSON or XML syntax with Protobuf
  • Missing field numbers in Protobuf
  • Using invalid keywords like 'schema'
3. Given this JSON Schema snippet for a Kafka message:
{
  "type": "object",
  "properties": {
    "userId": {"type": "string"},
    "age": {"type": "integer"}
  },
  "required": ["userId"]
}

Which message is valid according to this schema?
medium
A. {"userId": "abc123", "age": 30}
B. {"age": 30}
C. {"userId": 123, "age": 30}
D. {"userId": "abc123", "age": "30"}

Solution

  1. Step 1: Check required fields and types

    The schema requires 'userId' as a string and 'age' as an integer (optional).
  2. Step 2: Validate each option

    {"userId": "abc123", "age": 30} has 'userId' as string and 'age' as integer, so it is valid.
  3. Final Answer:

    {"userId": "abc123", "age": 30} -> Option A
  4. Quick Check:

    Required string userId and integer age = A [OK]
Hint: Required fields must be present with correct types [OK]
Common Mistakes:
  • Missing required 'userId' field
  • Using wrong data types for fields
  • Confusing string and integer types
4. You have this Protobuf schema:
message Event {
  string id = 1;
  int32 count = 2;
}

Which of the following is CORRECT about using it with Kafka?
medium
A. Using 'int32' instead of 'integer' causes error
B. Field numbers must be unique and start from 1
C. Missing a root 'schema' keyword causes failure
D. Field names cannot be strings

Solution

  1. Step 1: Check Protobuf field numbering rules

    Field numbers must be unique and start from 1, which is true here.
  2. Step 2: Identify the correct statement

    Field numbers must be unique and start from 1 correctly describes the field numbering rule. Options B, C, D are incorrect statements.
  3. Final Answer:

    Field numbers must be unique and start from 1 -> Option B
  4. Quick Check:

    Protobuf field numbering rule = A [OK]
Hint: Field numbers must be unique and start at 1 [OK]
Common Mistakes:
  • Thinking 'int32' is invalid
  • Expecting a 'schema' keyword in Protobuf
  • Confusing field names with types
5. You want to optimize Kafka message size and speed. Which schema choice is best and why?
hard
A. Use JSON Schema because it is human-readable and easy to debug
B. Use XML schema because it supports complex data types
C. Use Protobuf because it is compact and faster to serialize/deserialize
D. Use plain text messages without schema for maximum speed

Solution

  1. Step 1: Compare JSON Schema and Protobuf

    JSON Schema is readable but larger; Protobuf is compact and faster.
  2. Step 2: Choose based on optimization goal

    For smaller size and speed, Protobuf is better.
  3. Final Answer:

    Use Protobuf because it is compact and faster to serialize/deserialize -> Option C
  4. Quick Check:

    Compact and fast = D [OK]
Hint: Protobuf is compact and faster than JSON Schema [OK]
Common Mistakes:
  • Choosing JSON Schema for speed optimization
  • Ignoring schema benefits and using plain text
  • Confusing XML with Protobuf advantages
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard