Challenge - 5 Problems
CQRS Kafka Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Kafka message flow in CQRS
Given the following Kafka producer and consumer setup in a CQRS pattern, what will be the output printed by the consumer?
Kafka
from kafka import KafkaProducer, KafkaConsumer import json producer = KafkaProducer(bootstrap_servers='localhost:9092', value_serializer=lambda v: json.dumps(v).encode('utf-8')) consumer = KafkaConsumer('write_topic', bootstrap_servers='localhost:9092', value_deserializer=lambda m: json.loads(m.decode('utf-8'))) # Producer sends a command producer.send('write_topic', {'command': 'create_order', 'order_id': 123}) producer.flush() # Consumer reads the command for message in consumer: print(f"Received command: {message.value['command']} with order_id {message.value['order_id']}") break
Attempts:
2 left
💡 Hint
Remember that JSON serialization converts integers as numbers, not strings.
✗ Incorrect
The producer sends a JSON message with order_id as an integer 123. The consumer deserializes it back to a Python dict with order_id as int. So the printed output shows 123 without quotes.
🧠 Conceptual
intermediate1:30remaining
CQRS pattern separation of concerns
In a CQRS pattern using Kafka, which statement best describes the role of the write and read sides?
Attempts:
2 left
💡 Hint
Think about separating responsibilities for updating and reading data.
✗ Incorrect
CQRS stands for Command Query Responsibility Segregation. Commands change state (write side), queries read state (read side).
🔧 Debug
advanced2:00remaining
Identify the error in Kafka consumer code for CQRS read model
What error will this Kafka consumer code produce when trying to read from the 'read_topic' in a CQRS pattern?
consumer = KafkaConsumer('read_topic', bootstrap_servers='localhost:9092')
for message in consumer:
data = message.value
print(f"Read model data: {data['id']}")
Kafka
consumer = KafkaConsumer('read_topic', bootstrap_servers='localhost:9092') for message in consumer: data = message.value print(f"Read model data: {data['id']}")
Attempts:
2 left
💡 Hint
Check how KafkaConsumer returns message values by default.
✗ Incorrect
By default, KafkaConsumer returns message.value as bytes. Accessing data['id'] causes TypeError because bytes are not subscriptable like dicts.
📝 Syntax
advanced1:30remaining
Correct Kafka producer code for sending a command in CQRS
Which option is the correct Kafka producer code snippet to send a command message with key 'order-1' and value {'action': 'update', 'order_id': 1} in CQRS?
Attempts:
2 left
💡 Hint
Kafka keys and values must be bytes; JSON strings must be encoded.
✗ Incorrect
Kafka producer expects key and value as bytes. Option A correctly encodes JSON string to bytes and uses bytes key.
🚀 Application
expert3:00remaining
Designing event sourcing with CQRS and Kafka
You want to implement event sourcing with CQRS using Kafka. Which approach correctly ensures that all state changes are captured and read models are updated asynchronously?
Attempts:
2 left
💡 Hint
Think about how event sourcing captures changes and how read models stay updated.
✗ Incorrect
In event sourcing with CQRS, the write side produces events representing state changes. The read side consumes these events asynchronously to update read models, ensuring eventual consistency.