0
0
Kafkadevops~20 mins

Event sourcing pattern in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Event Sourcing 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 event sourcing consumer code?

Consider a Kafka consumer that reads events from a topic and reconstructs the current state by applying all events in order. What will be the final state printed?

Kafka
from kafka import KafkaConsumer
import json

consumer = KafkaConsumer('user-events', bootstrap_servers='localhost:9092', auto_offset_reset='earliest')

state = {}
for message in consumer:
    event = json.loads(message.value)
    if event['type'] == 'UserCreated':
        state[event['user_id']] = {'name': event['name'], 'active': True}
    elif event['type'] == 'UserDeactivated':
        if event['user_id'] in state:
            state[event['user_id']]['active'] = False
    if len(state) == 2:
        break
print(state)
A{"u1": {"name": "Alice", "active": true}, "u2": {"name": "Bob", "active": true}}
B{"u1": {"name": "Alice", "active": false}, "u2": {"name": "Bob", "active": true}}
C{"u1": {"name": "Alice", "active": true}}
DSyntaxError
Attempts:
2 left
💡 Hint

Remember that events are applied in order and can change the state of existing users.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes event sourcing in Kafka?

Choose the statement that correctly explains the event sourcing pattern using Kafka.

AEvent sourcing stores only the latest state snapshot in Kafka topics to reduce storage.
BEvent sourcing uses Kafka to send commands directly to update the database state.
CEvent sourcing requires Kafka to delete old events after processing to save space.
DEvent sourcing stores all changes as events in Kafka topics, allowing state reconstruction by replaying events.
Attempts:
2 left
💡 Hint

Think about how event sourcing preserves history.

🔧 Debug
advanced
2:00remaining
Why does this Kafka event sourcing consumer code raise an error?

Identify the cause of the error in this Kafka consumer code snippet that processes events for state reconstruction.

Kafka
from kafka import KafkaConsumer
import json

consumer = KafkaConsumer('orders', bootstrap_servers='localhost:9092', auto_offset_reset='earliest')

state = {}
for message in consumer:
    event = json.loads(message.value)
    if event['type'] == 'OrderPlaced':
        state[event['order_id']] = event['details']
    elif event['type'] == 'OrderCancelled':
        del state[event['order_id']]
print(state)
AKeyError when deleting a non-existent order_id in state
BJSONDecodeError due to invalid JSON in message.value
CKafkaConsumer raises TimeoutError due to no messages
DTypeError because event['details'] is not a dictionary
Attempts:
2 left
💡 Hint

Check what happens if an OrderCancelled event arrives for an order_id not in state.

📝 Syntax
advanced
2:00remaining
Which Kafka consumer code snippet correctly applies event sourcing to update user balances?

Choose the code snippet that correctly updates user balances by applying 'Deposit' and 'Withdrawal' events from Kafka.

A
for msg in consumer:
    event = json.loads(msg.value)
    if event['type'] == 'Deposit':
        state[event['user_id']] +=+ event['amount']
    elif event['type'] == 'Withdrawal':
        state[event['user_id']] -=- event['amount']
B
for msg in consumer:
    event = json.loads(msg.value)
    if event['type'] == 'Deposit':
        state[event['user_id']] =+ event['amount']
    elif event['type'] == 'Withdrawal':
        state[event['user_id']] =- event['amount']
C
for msg in consumer:
    event = json.loads(msg.value)
    if event['type'] == 'Deposit':
        state[event['user_id']] = state.get(event['user_id'], 0) + event['amount']
    elif event['type'] == 'Withdrawal':
        state[event['user_id']] = state.get(event['user_id'], 0) - event['amount']
D
for msg in consumer:
    event = json.loads(msg.value)
    if event['type'] == 'Deposit':
        state[event['user_id']] += event['amount']
    elif event['type'] == 'Withdrawal':
        state[event['user_id']] -= event['amount']
Attempts:
2 left
💡 Hint

Check for correct use of operators and handling missing keys.

🚀 Application
expert
2:30remaining
How many events are stored in Kafka to reconstruct the state after these operations?

A system uses Kafka for event sourcing. The following events are produced in order:
1. UserCreated (user_id: u1)
2. UserUpdated (user_id: u1, name changed)
3. UserDeactivated (user_id: u1)
4. UserCreated (user_id: u2)
5. UserDeleted (user_id: u1)

How many events must be replayed from Kafka to reconstruct the current state of all users?

A5
B4
C3
D2
Attempts:
2 left
💡 Hint

Event sourcing requires replaying all events to get the current state.