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?
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)
Remember that events are applied in order and can change the state of existing users.
The consumer reads events in order. First, UserCreated for u1 (Alice) sets active true. Then UserDeactivated for u1 sets active false. Then UserCreated for u2 (Bob) sets active true. So final state has u1 inactive and u2 active.
Choose the statement that correctly explains the event sourcing pattern using Kafka.
Think about how event sourcing preserves history.
Event sourcing means storing every change as an event. Kafka topics hold these events, so the current state can be rebuilt by replaying them.
Identify the cause of the error in this Kafka consumer code snippet that processes events for state reconstruction.
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)
Check what happens if an OrderCancelled event arrives for an order_id not in state.
If the code tries to delete an order_id that was never added, Python raises a KeyError because the key does not exist in the dictionary.
Choose the code snippet that correctly updates user balances by applying 'Deposit' and 'Withdrawal' events from Kafka.
Check for correct use of operators and handling missing keys.
Option C correctly initializes missing keys with 0 and uses += and -= logic properly. Options B and D have invalid operators (=+ and +=+), and C does not handle missing keys causing KeyError.
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?
Event sourcing requires replaying all events to get the current state.
All 5 events are stored and must be replayed in order to reconstruct the current state, even if some events delete or deactivate users.