Bird
Raised Fist0
Kafkadevops~10 mins

Leader election in Kafka - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the leader election strategy in Kafka configuration.

Kafka
props.put("leader.election.strategy", "[1]");
Drag options to blanks, or click blank then click option'
Aorg.apache.kafka.controller.KafkaLeaderElectionStrategy
Borg.apache.kafka.controller.LeaderElectionStrategy
Corg.apache.kafka.controller.ZkLeaderElectionStrategy
Dorg.apache.kafka.controller.DefaultLeaderElectionStrategy
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a generic or non-existent class name.
Confusing Zookeeper based strategy with Kafka's internal strategy.
2fill in blank
medium

Complete the code to configure the leader election timeout in milliseconds.

Kafka
props.put("leader.election.timeout.ms", "[1]");
Drag options to blanks, or click blank then click option'
A1000
B60000
C5000
D30000
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the timeout too low causing frequent leader elections.
Using values that are too high causing slow failover.
3fill in blank
hard

Fix the error in the code to correctly start leader election on the controller.

Kafka
controller.[1]LeaderElection();
Drag options to blanks, or click blank then click option'
Astart
Brun
Cbegin
Dinitiate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'initiate' or 'begin' which are not valid method names.
Using 'run' which is unrelated here.
4fill in blank
hard

Fill both blanks to create a map of partition to leader with condition on leader id.

Kafka
Map<Integer, String> leaderMap = partitions.stream()
  .filter(p -> p.leader().id() [1] 0)
  .collect(Collectors.toMap(p -> p.partition(), p -> p.[2]()));
Drag options to blanks, or click blank then click option'
A>
BgetLeader
Cleader
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the filter condition.
Using 'getLeader' which is not a method on partition.
5fill in blank
hard

Fill all three blanks to create a map of topic to leader id for partitions with leader id greater than 0.

Kafka
Map<String, Integer> topicLeaderMap = partitions.stream()
  .filter(p -> p.[1]().id() [2] 0)
  .collect(Collectors.toMap(p -> p.[3](), p -> p.leader().id()));
Drag options to blanks, or click blank then click option'
Aleader
B==
Ctopic
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' in the filter.
Using 'partition()' instead of 'topic()' for the map key.

Practice

(1/5)
1. What is the main purpose of leader election in Kafka?
easy
A. To backup data to external storage
B. To balance the load evenly among all brokers
C. To choose one broker to lead and coordinate tasks
D. To monitor client connections

Solution

  1. Step 1: Understand leader election role

    Leader election picks one broker as the leader to manage partition tasks.
  2. Step 2: Compare options

    Only To choose one broker to lead and coordinate tasks describes choosing a broker to lead and coordinate tasks, which matches Kafka's leader election purpose.
  3. Final Answer:

    To choose one broker to lead and coordinate tasks -> Option C
  4. Quick Check:

    Leader election = choose leader broker [OK]
Hint: Leader election picks one leader broker, not all brokers [OK]
Common Mistakes:
  • Confusing leader election with load balancing
  • Thinking leader election backs up data
  • Assuming leader election monitors clients
2. Which of the following is the correct way to configure Kafka broker IDs for leader election?
easy
A. broker.id=unique_integer_per_broker
B. broker.id=localhost
C. broker.id=auto_assign
D. broker.id=0.0.0.0

Solution

  1. Step 1: Identify broker ID type

    Kafka requires each broker to have a unique integer ID for leader election.
  2. Step 2: Check options

    Only broker.id=unique_integer_per_broker uses a unique integer, while others use invalid or non-integer values.
  3. Final Answer:

    broker.id=unique_integer_per_broker -> Option A
  4. Quick Check:

    Broker ID must be unique integer [OK]
Hint: Broker ID must be a unique integer, not a string or IP [OK]
Common Mistakes:
  • Using hostnames or IPs as broker IDs
  • Setting broker.id to zero or non-unique values
  • Assuming auto assignment works without config
3. Given the following Kafka cluster state, which broker will be the leader for partition 0?
Broker 1: ISR = [1, 2, 3]
Broker 2: ISR = [1, 2, 3]
Broker 3: ISR = [1, 2, 3]
Current leader: Broker 2
Broker 2 fails
medium
A. Broker 2
B. Broker 1
C. Broker 3
D. No leader assigned

Solution

  1. Step 1: Understand ISR and leader failover

    ISR (In-Sync Replicas) are eligible leaders. When current leader fails, next ISR broker becomes leader.
  2. Step 2: Identify next leader after Broker 2 fails

    Broker 1 and 3 are in ISR. Kafka picks the lowest broker ID in ISR as new leader, so Broker 1.
  3. Final Answer:

    Broker 1 -> Option B
  4. Quick Check:

    Next leader = lowest ISR broker after failure [OK]
Hint: Leader election picks lowest ISR broker if current leader fails [OK]
Common Mistakes:
  • Choosing failed broker as leader
  • Picking broker outside ISR
  • Assuming no leader assigned after failure
4. You have a Kafka cluster where leader election is not working properly. Which of the following is the most likely cause?
medium
A. Each broker has a unique broker.id
B. Zookeeper is running and connected
C. Partitions have multiple replicas
D. All brokers have the same broker.id

Solution

  1. Step 1: Identify leader election requirements

    Leader election requires each broker to have a unique broker.id to distinguish them.
  2. Step 2: Analyze causes

    If all brokers share the same broker.id, leader election cannot assign a unique leader, causing failure.
  3. Final Answer:

    All brokers have the same broker.id -> Option D
  4. Quick Check:

    Unique broker IDs needed for leader election [OK]
Hint: Check broker IDs; duplicates break leader election [OK]
Common Mistakes:
  • Assuming Zookeeper connection causes failure
  • Thinking multiple replicas cause election failure
  • Ignoring broker.id uniqueness
5. In a Kafka cluster using KRaft mode (without Zookeeper), how does leader election differ from the traditional Zookeeper-based approach?
hard
A. KRaft uses an internal quorum of controllers to elect leaders without external Zookeeper
B. KRaft requires manual leader assignment by the admin
C. KRaft disables leader election and uses round-robin partition assignment
D. KRaft uses Zookeeper but with a different election algorithm

Solution

  1. Step 1: Understand KRaft mode leader election

    KRaft mode replaces Zookeeper with an internal controller quorum that manages leader election.
  2. Step 2: Compare options

    Only KRaft uses an internal quorum of controllers to elect leaders without external Zookeeper correctly states KRaft uses internal quorum without external Zookeeper for leader election.
  3. Final Answer:

    KRaft uses an internal quorum of controllers to elect leaders without external Zookeeper -> Option A
  4. Quick Check:

    KRaft = internal quorum leader election [OK]
Hint: KRaft replaces Zookeeper with internal controller quorum [OK]
Common Mistakes:
  • Thinking KRaft disables leader election
  • Assuming manual leader assignment in KRaft
  • Believing KRaft still uses external Zookeeper
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard