0
0
Kafkadevops~30 mins

Static group membership in Kafka - Mini Project: Build & Apply

Choose your learning style9 modes available
Static Group Membership in Kafka Consumer
📖 Scenario: You are building a Kafka consumer application that needs to join a consumer group with static membership. This helps Kafka remember your consumer instance even if it disconnects temporarily, avoiding unnecessary rebalances.
🎯 Goal: Create a Kafka consumer configuration that uses static group membership by setting the group.instance.id. Then, subscribe to a topic and print the assigned partitions.
📋 What You'll Learn
Create a Kafka consumer configuration dictionary with group.id set to my-static-group
Add group.instance.id set to consumer-1 to enable static membership
Subscribe the consumer to the topic my-topic
Print the assigned partitions after subscription
💡 Why This Matters
🌍 Real World
Static group membership is useful in real-world Kafka consumer applications where you want to avoid unnecessary rebalances when consumers restart or temporarily disconnect.
💼 Career
Understanding static group membership helps you build more stable and efficient Kafka consumer applications, a valuable skill for backend developers and data engineers working with Kafka.
Progress0 / 4 steps
1
Create Kafka consumer configuration
Create a dictionary called consumer_config with these exact entries: 'bootstrap.servers': 'localhost:9092' and 'group.id': 'my-static-group'.
Kafka
Need a hint?

Use a Python dictionary with keys 'bootstrap.servers' and 'group.id'.

2
Add static group membership ID
Add the entry 'group.instance.id': 'consumer-1' to the existing consumer_config dictionary.
Kafka
Need a hint?

Add the key 'group.instance.id' with the value 'consumer-1' inside the dictionary.

3
Subscribe consumer to topic
Create a Kafka consumer using consumer_config and subscribe it to the topic 'my-topic'. Use variable name consumer for the consumer instance.
Kafka
Need a hint?

Use consumer.subscribe(['my-topic']) to subscribe to the topic.

4
Print assigned partitions
Print the assigned partitions of the consumer by calling consumer.assignment() and printing the result.
Kafka
Need a hint?

Use print(consumer.assignment()) to show the assigned partitions. It may print an empty list if no partitions are assigned yet.