Kafka & Messaging Questions with Answers

29 practice questions on Kafka & Messaging (Data Engineering) with answers and step-by-step solutions. Try 10 samples below, then practise all 29 free in the AptiNest app.

Sample questions

Question 1Easy

Which role does a Kafka producer play?

  1. It writes records to topics
  2. It reads records from topics
  3. It elects the partition leader
  4. It stores partitions on disk

Answer: A. It writes records to topics

Explanation

Producers publish; brokers store; consumers read.

Step-by-step solution

  1. A producer is the client that publishes records to a topic.
  2. Brokers store the partitions, and consumers read the records.
  3. Electing a partition leader is done inside the Kafka cluster, not by a producer.
  4. The correct answer is 0: a producer writes records to topics.

Why the other options are wrong

  • B. Reading records is the consumer's role.
  • C. Leader election is handled inside the Kafka cluster, not by producers.
  • D. Brokers store partitions on disk; a producer only sends records.

Tip: Producers write, brokers store, consumers read.

Question 2Easy

A Kafka consumer group rebalance happens when:

  1. A producer sends a message without a key
  2. The topic's retention period expires
  3. Consumers join or leave, or partitions change
  4. A message is larger than one kilobyte

Answer: C. Consumers join or leave, or partitions change

Explanation

Partitions are reassigned among the current members.

Step-by-step solution

  1. A consumer group shares a topic's partitions among its current members.
  2. A rebalance reassigns partitions when a consumer joins or leaves, or when partitions change.
  3. Message keys, message size and retention do not change group membership.
  4. The correct answer is option C: consumers join or leave, or partitions change.

Why the other options are wrong

  • A. A missing key changes how a message is routed, but it does not trigger a rebalance.
  • B. Retention deletes old messages; it does not reassign consumers.
  • D. Message size does not affect group membership.

Tip: A rebalance follows a membership or partition change; plan for it in consumers.

Question 3Easy

Within one consumer group, each partition is read by:

  1. No consumer until a rebalance happens
  2. Only the consumer with the lowest ID
  3. Exactly one consumer at a time
  4. Every consumer in the group at once

Answer: C. Exactly one consumer at a time

Explanation

Partitions are divided among the group's consumers; this is how a group shares the work.

Step-by-step solution

  1. Each partition is assigned to one consumer in the group.
  2. A single consumer reads data from its assigned partition.
  3. Only one consumer can read a partition at any time.
  4. If a consumer leaves, the partition is reassigned, but only one reads it at once.
  5. Exactly one consumer at a time reads each partition.

Why the other options are wrong

  • A. Consumers in a group are assigned partitions straight away, not only after a rebalance happens.
  • B. The lowest consumer ID does not decide who reads a partition; the group coordinator assigns partitions.
  • D. Every consumer reading a partition at once is wrong within one group; each partition has one reader.

Question 4Easy

Kafka retention settings control:

  1. Which users may create new topics
  2. How messages are encrypted in transit
  3. How many consumers may join a group
  4. How long or how much data is kept

Answer: D. How long or how much data is kept

Explanation

Data is deleted by age or size limits (or compacted), independent of consumption.

Step-by-step solution

  1. Kafka retention settings decide when data is removed.
  2. Data is deleted based on time or storage limits.
  3. This controls the amount of data kept in the system.
  4. Retention settings do not affect message encryption.
  5. Retention settings do not control consumer group size.
  6. Retention settings determine how long or how much data is stored.

Why the other options are wrong

  • A. Topic creation rights are access control settings, not retention settings.
  • B. Encryption in transit is set by TLS settings, not by retention settings.
  • C. How many consumers can join a group is about group membership, not how long data is kept.

Question 5Medium

Why do producers often set the message key to the customer ID?

  1. Kafka rejects any message that does not carry a key, so a key is always required
  2. All events for that customer go to one partition, keeping their order
  3. Keys make every message smaller after compression on disk
  4. Keys allow consumers to skip reading other partitions

Answer: B. All events for that customer go to one partition, keeping their order

Explanation

The default partitioner hashes the key, so the same key always maps to the same partition.

Step-by-step solution

  1. Producers use customer ID as message key to ensure all events go to one partition.
  2. Same key always maps to same partition, keeping event order intact.
  3. This avoids out-of-order processing in stream applications.
  4. Partitioning by customer ID groups related data together.
  5. All events for a customer are stored in one place for efficient retrieval.

Why the other options are wrong

  • A. Kafka does not require a key on every message; messages without keys are accepted.
  • C. Keys do not make messages smaller after compression; their purpose is routing to partitions.
  • D. Keys do not let consumers skip partitions; consumers still read their assigned partitions.

Tip: Use the same key for related messages so Kafka puts them in one partition.

Question 6Medium

A consumer group reads 3 partitions with lags of 1,200, 0 and 3,400. What is the group's total lag?

  1. 4,600
  2. 1,200
  3. 3,400
  4. 1,533

Answer: A. 4,600

Explanation

Total lag is the sum over partitions: 1,200 + 0 + 3,400 = 4,600.

Step-by-step solution

  1. Total lag adds partition lags.
  2. 1,200 + 0 + 3,400.
  3. The total is 4,600.

Why the other options are wrong

  • B. 1,200 is one partition.
  • C. 3,400 is one partition.
  • D. 1,533 is an average.

Tip: Total lag = sum.

Question 7Medium

Log compaction on a Kafka topic keeps:

  1. Every record forever with no deletion
  2. At least the latest record for each key
  3. Only the first record ever written
  4. Only records newer than seven days

Answer: B. At least the latest record for each key

Explanation

Compacted topics act like a changelog snapshot, useful for state and CDC tables.

Step-by-step solution

  1. Log compaction removes older records that have the same key as a newer record.
  2. The latest record for each key is always kept.
  3. So compaction keeps at least the latest record per key.
  4. The correct answer is option B: at least the latest record for each key.

Why the other options are wrong

  • A. Compaction removes older records, so not every record is kept forever.
  • C. The first record is the oldest; compaction keeps the newest value for each key.
  • D. Compaction works by key, not by a seven-day age window.

Tip: A compacted topic keeps the latest value per key, like a changelog snapshot.

Question 8Medium

A consumer commits its offset before processing each batch, then crashes mid-batch. On restart, what delivery semantics did it get?

  1. No delivery: the whole topic is read again
  2. Exactly-once: every message is processed one time, thanks to the commit
  3. At-most-once: the unprocessed messages are skipped
  4. At-least-once: the messages are processed twice

Answer: C. At-most-once: the unprocessed messages are skipped

Explanation

The committed offset is already past the lost messages, so they are never processed.

Step-by-step solution

  1. The consumer commits its offset before it processes the batch.
  2. It crashes mid-batch, so the messages in that batch were never processed.
  3. On restart it resumes after the committed offset, so those messages are skipped.
  4. The correct answer is 2: at-most-once, because unprocessed messages are skipped.

Why the other options are wrong

  • A. A restart resumes from the committed offset, not from the start of the topic.
  • B. Exactly-once needs processing and commit to succeed together; committing first does not give that.
  • D. At-least-once needs the commit after processing, so the batch would be read again.

Tip: Commit after processing for at-least-once; commit before processing for at-most-once.

Question 9Hard

Exactly-once processing within Kafka (read from a topic, process, write to another topic) relies on:

  1. Idempotent producers plus transactions that commit outputs and offsets atomically
  2. Writing every message twice and deduplicating on read
  3. Disabling consumer groups so that only a single consumer ever reads the topic at a time
  4. Setting the replication factor of every topic to exactly one

Answer: A. Idempotent producers plus transactions that commit outputs and offsets atomically

Explanation

The transaction makes the output records and the input offsets visible together or not at all.

Step-by-step solution

  1. Idempotent producers prevent duplicate writes.
  2. Transactions ensure both outputs and offsets are committed together.
  3. This guarantees exactly-once processing.
  4. No partial commits occur with transactions.
  5. Final answer: Idempotent producers plus transactions that commit outputs and offsets atomically.

Why the other options are wrong

  • B. Writing every message twice and deduplicating is not how Kafka gets exactly-once processing.
  • C. Disabling consumer groups does not give exactly-once; it only limits who reads the topic.
  • D. Replication factor one reduces durability and does not give exactly-once processing.

Tip: Use transactions with idempotent producers for exactly-once semantics in Kafka.

Question 10Hard

With replication factor 3, min.insync.replicas = 2 and producers using acks=all, how many brokers holding a partition can fail while writes to it still succeed?

  1. 1
  2. 3
  3. 0
  4. 2

Answer: A. 1

Explanation

Writes need at least 2 in-sync replicas; losing 1 of 3 leaves 2, losing 2 leaves 1 and writes are rejected.

Step-by-step solution

  1. Replication factor 3 means three brokers hold a copy of each partition.
  2. min.insync.replicas 2 with acks=all needs two in-sync copies for a write to succeed.
  3. Losing one broker leaves two in-sync replicas, so writes still succeed.
  4. Losing two leaves one, which is below the minimum, so the correct answer is 1.

Why the other options are wrong

  • B. Losing three brokers leaves no replicas at all, so writes fail.
  • C. Zero is too strict; one failure still leaves the minimum of two replicas.
  • D. Losing two leaves one in-sync replica, below the minimum of two, so writes stop.

Tip: Failures tolerated = replicas minus min.insync.replicas, here 3 minus 2 = 1.

Practise all 29 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Data Engineering topics · All topics