0
0
Kafkadevops~20 mins

CQRS pattern in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CQRS Kafka Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
AReceived command: create_order with order_id None
BReceived command: create_order with order_id '123'
CNo output, consumer times out
DReceived command: create_order with order_id 123
Attempts:
2 left
💡 Hint
Remember that JSON serialization converts integers as numbers, not strings.
🧠 Conceptual
intermediate
1:30remaining
CQRS pattern separation of concerns
In a CQRS pattern using Kafka, which statement best describes the role of the write and read sides?
AThe write side handles queries; the read side handles commands and updates state.
BThe write side handles commands and updates state; the read side handles queries and returns data.
CBoth write and read sides handle commands and queries interchangeably.
DThe write side only stores data; the read side only deletes data.
Attempts:
2 left
💡 Hint
Think about separating responsibilities for updating and reading data.
🔧 Debug
advanced
2: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']}")
AKeyError: 'id'
BKafkaTimeoutError: Failed to fetch messages
CTypeError: 'bytes' object is not subscriptable
DNo error, prints the id correctly
Attempts:
2 left
💡 Hint
Check how KafkaConsumer returns message values by default.
📝 Syntax
advanced
1: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?
Aproducer.send('commands', key=b'order-1', value=json.dumps({'action': 'update', 'order_id': 1}).encode('utf-8'))
Bproducer.send('commands', key='order-1', value=json.dumps({'action': 'update', 'order_id': 1}))
Cproducer.send('commands', key=b'order-1', value={'action': 'update', 'order_id': 1})
Dproducer.send('commands', key='order-1', value={'action': 'update', 'order_id': 1})
Attempts:
2 left
💡 Hint
Kafka keys and values must be bytes; JSON strings must be encoded.
🚀 Application
expert
3: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?
AWrite side produces events to a Kafka topic; read side consumes events and updates read models asynchronously.
BBoth write and read sides produce and consume events from the same Kafka topic simultaneously.
CWrite side updates database directly; read side queries database without Kafka involvement.
DRead side produces events to Kafka; write side consumes events and updates state synchronously.
Attempts:
2 left
💡 Hint
Think about how event sourcing captures changes and how read models stay updated.