Controller broker in Kafka - Step-by-Step Execution
1. Start Kafka cluster 2. Elect controller broker 3. Controller monitors brokers 4. Handle leader changes 5. Update metadata 6. Detect failures and reassign
| Step | Action | Controller State | Cluster Metadata | Output |
|---|---|---|---|---|
| 1 | Start Kafka cluster | No controller | Empty | Cluster starts |
| 2 | Elect controller broker | Controller elected (Broker 1) | Empty | Controller ready |
| 3 | Controller monitors brokers | Active | Metadata updated with brokers | Monitoring brokers |
| 4 | Handle leader changes | Active | Partition leaders updated | Leaders assigned |
| 5 | Update metadata | Active | Metadata refreshed | Metadata broadcasted |
| 6 | Detect broker failure | Active | Metadata marks broker down | Trigger reassignment |
| 7 | Trigger reassignment | Active | Partition leadership reassigned | Cluster stable |
| 8 | Controller continues managing | Active | Metadata current | Waiting for events |
| 9 | Controller failure | Controller lost | Metadata frozen | New controller election triggered |
| 10 | New controller elected | Controller elected (Broker 2) | Metadata refreshed | Cluster managed again |
| Variable | Start | After Step 2 | After Step 5 | After Step 7 | After Step 9 | After Step 10 |
|---|---|---|---|---|---|---|
| Controller State | No controller | Controller elected (Broker 1) | Active | Active | Controller lost | Controller elected (Broker 2) |
| Cluster Metadata | Empty | Empty | Metadata refreshed | Partition leadership reassigned | Metadata frozen | Metadata refreshed |
| Broker Status | Unknown | Known | Known | Broker 3 down | Broker 3 down | Known |
Controller broker manages Kafka cluster metadata and broker states. It is elected at cluster start and handles leader changes and failures. Detects broker failures and triggers partition reassignment. If controller fails, a new one is elected to continue management. Ensures cluster stability and metadata consistency.
Practice
What is the main role of the controller broker in a Kafka cluster?
Solution
Step 1: Understand controller broker responsibilities
The controller broker is responsible for managing cluster-wide tasks such as leader election and metadata updates.Step 2: Compare other options
Storing messages, producing messages, and handling consumer offsets are roles of other brokers or clients, not the controller.Final Answer:
Manage leader election and cluster metadata updates -> Option AQuick Check:
Controller broker = leader election manager [OK]
- Confusing controller with brokers that store data
- Thinking controller handles only consumer offsets
- Assuming controller is a client
Which of the following is the correct way to identify the controller broker using Kafka's command line tool?
kafka-metadata-quorum.sh --bootstrap-server localhost:9092 status
Solution
Step 1: Understand the command output
Thekafka-metadata-quorum.sh statustool shows metadata quorum details including LeaderId, which is the ID of the current controller broker.Step 2: Identify controller in output
The controller broker is identified by the LeaderId field, not by broker ID, port number, or generic leader role.Final Answer:
Look for the LeaderId in the output -> Option CQuick Check:
LeaderId = controller broker ID [OK]
- Assuming highest broker ID means controller
- Using port number to find controller
- Confusing generic leader role with LeaderId
Given the following code snippet using Kafka AdminClient in Java, what will controllerId hold after execution?
AdminClient admin = AdminClient.create(props); Node controller = admin.describeCluster().controller().get(); int controllerId = controller.id(); admin.close();
Solution
Step 1: Analyze AdminClient describeCluster method
ThedescribeCluster().controller()returns the current controller broker as a Node object.Step 2: Understand Node.id()
Theid()method returns the broker ID of that Node, which is the controller broker's ID.Final Answer:
The broker ID of the current controller broker -> Option DQuick Check:
controller().id() = controller broker ID [OK]
- Confusing controller ID with number of brokers
- Thinking it returns port or partition count
- Ignoring the blocking get() call
Consider this snippet to get the controller broker ID using Kafka AdminClient in Java:
AdminClient admin = AdminClient.create(props); Node controller = admin.describeCluster().controller().get(); int controllerId = controller.id(); admin.close();
What is the likely cause if this code throws a java.util.concurrent.ExecutionException?
Solution
Step 1: Understand ExecutionException cause
An ExecutionException usually wraps an error during the async call, often connection failure.Step 2: Check AdminClient connectivity
If AdminClient cannot connect to any broker, theget()call oncontroller()future will fail with this exception.Final Answer:
The AdminClient failed to connect to any broker -> Option BQuick Check:
ExecutionException = connection failure [OK]
- Thinking method does not exist causes ExecutionException
- Confusing return types of id() method
- Assuming close() causes this error
You want to write a Kafka monitoring tool that automatically detects when the controller broker changes and logs the new controller's broker ID. Which approach is best?
- Use AdminClient to poll
describeCluster().controller()periodically. - Subscribe to Kafka internal topics to listen for controller changes.
- Manually check broker logs for controller election messages.
- Restart all brokers to force controller re-election and detect changes.
Solution
Step 1: Evaluate monitoring approaches
PollingdescribeCluster().controller()via AdminClient is a reliable, programmatic way to detect controller changes.Step 2: Assess other options
Subscribing to internal topics is complex and not guaranteed; checking logs is manual and slow; restarting brokers is disruptive and impractical.Final Answer:
Use AdminClient to polldescribeCluster().controller()periodically -> Option AQuick Check:
Polling controller() is best for monitoring [OK]
- Trying to read internal topics for controller changes
- Relying on manual log inspection
- Restarting brokers unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
