0
0
Kafkadevops~30 mins

Event sourcing pattern in Kafka - Mini Project: Build & Apply

Choose your learning style9 modes available
Event Sourcing Pattern with Kafka
📖 Scenario: You are building a simple event sourcing system using Kafka. Event sourcing means storing all changes as events in order, so you can rebuild the current state anytime by replaying these events.Imagine a bank account where every deposit and withdrawal is an event. We will create events, configure a topic, process events to get the current balance, and finally print the balance.
🎯 Goal: Build a simple Kafka event sourcing example that stores deposit and withdrawal events, processes them to calculate the current balance, and prints the result.
📋 What You'll Learn
Create a list of events with exact deposit and withdrawal amounts
Create a variable for the Kafka topic name
Write a loop to process events and calculate the balance
Print the final balance
💡 Why This Matters
🌍 Real World
Event sourcing is used in banking, e-commerce, and many systems to keep a full history of changes for auditing and rebuilding state.
💼 Career
Understanding event sourcing and Kafka is valuable for backend developers, data engineers, and system architects working with real-time data and distributed systems.
Progress0 / 4 steps
1
Create the list of events
Create a list called events with these exact dictionaries representing bank transactions: {'type': 'deposit', 'amount': 100}, {'type': 'withdrawal', 'amount': 40}, {'type': 'deposit', 'amount': 60}
Kafka
Need a hint?

Use a list with dictionaries exactly as shown. Each dictionary has keys 'type' and 'amount'.

2
Set the Kafka topic name
Create a variable called topic_name and set it to the string 'bank_account_events'
Kafka
Need a hint?

Assign the exact string 'bank_account_events' to the variable topic_name.

3
Calculate the current balance from events
Create a variable called balance and set it to 0. Then use a for loop with variables event to iterate over events. Inside the loop, if event['type'] is 'deposit', add event['amount'] to balance. If it is 'withdrawal', subtract event['amount'] from balance.
Kafka
Need a hint?

Start balance at 0. Loop through events. Add or subtract amounts based on event type.

4
Print the final balance
Write a print statement to display the text 'Current balance:' followed by the value of balance using an f-string.
Kafka
Need a hint?

Use print(f"Current balance: {balance}") to show the result.