Bird
Raised Fist0
Kafkadevops~20 mins

Exactly-once semantics (EOS) in Kafka - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kafka EOS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Kafka producer code snippet with EOS enabled?

Consider the following Kafka producer code snippet configured for exactly-once semantics (EOS). What will be the output printed after sending the message?

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("enable.idempotence", "true");
props.put("acks", "all");
props.put("transactional.id", "txn-1");

KafkaProducer<String, String> producer = new KafkaProducer<>(props);
producer.initTransactions();

try {
    producer.beginTransaction();
    producer.send(new ProducerRecord<>("my-topic", "key1", "value1"));
    producer.commitTransaction();
    System.out.println("Message sent successfully");
} catch (ProducerFencedException | OutOfOrderSequenceException | AuthorizationException e) {
    producer.close();
    System.out.println("Fatal error, producer closed");
} catch (KafkaException e) {
    producer.abortTransaction();
    System.out.println("Transaction aborted");
}
AFatal error, producer closed
BNo output printed
CTransaction aborted
DMessage sent successfully
Attempts:
2 left
💡 Hint

Think about what happens when the transaction commits successfully without exceptions.

❓ Predict Output
intermediate
2:00remaining
What error does this Kafka consumer code raise when using EOS incorrectly?

Given the following Kafka consumer code snippet that attempts to read committed messages with isolation level set incorrectly, what error will it raise?

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "group1");
props.put("enable.auto.commit", "false");
props.put("isolation.level", "read_uncommitted");

KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Collections.singletonList("my-topic"));

while (true) {
    ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
    for (ConsumerRecord<String, String> record : records) {
        System.out.println(record.value());
    }
}
ANo error, messages may include uncommitted data
Borg.apache.kafka.common.errors.InvalidConfigurationException
Corg.apache.kafka.clients.consumer.CommitFailedException
Dorg.apache.kafka.common.errors.TransactionAbortedException
Attempts:
2 left
💡 Hint

Consider what happens when the isolation level is set to read_uncommitted.

🧠 Conceptual
advanced
2:00remaining
Which Kafka feature ensures exactly-once semantics in stream processing?

Which Kafka feature is primarily responsible for enabling exactly-once semantics (EOS) in Kafka Streams applications?

AConsumer offset auto-commit
BIdempotent producer combined with transactional writes
CPartition key hashing
DLog compaction
Attempts:
2 left
💡 Hint

Think about how Kafka prevents duplicate writes and partial commits.

❓ Predict Output
advanced
2:00remaining
What is the output of this Kafka Streams code with EOS enabled?

Given the following Kafka Streams code snippet with exactly-once processing enabled, what will be the output printed?

Kafka
Properties props = new Properties();
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "app1");
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(StreamsConfig.PROCESSING_GUARANTEE_CONFIG, StreamsConfig.EXACTLY_ONCE_V2);

StreamsBuilder builder = new StreamsBuilder();
KStream<String, String> input = builder.stream("input-topic");

input.mapValues(value -> value.toUpperCase())
     .to("output-topic");

KafkaStreams streams = new KafkaStreams(builder.build(), props);
streams.start();

System.out.println("Streams started with EOS");
AStreams started with EOS
BStreams started without EOS
CRuntimeException due to missing transactional.id
DNo output printed
Attempts:
2 left
💡 Hint

Check the processing guarantee configuration and what the code prints.

🧠 Conceptual
expert
2:00remaining
What happens if a Kafka producer with EOS loses its transactional.id?

In Kafka exactly-once semantics, what is the consequence if a producer loses or changes its transactional.id between restarts?

AThe consumer offsets are reset automatically to earliest
BThe producer continues normally without any impact on EOS guarantees
CThe producer is fenced and cannot commit previous transactions, preventing duplicates
DThe broker deletes all previous transactional data for that producer
Attempts:
2 left
💡 Hint

Consider how Kafka prevents duplicate transactions from multiple producers with the same transactional.id.

Practice

(1/5)
1. What is the main goal of Exactly-once semantics (EOS) in Kafka?
easy
A. To compress messages for storage efficiency
B. To prevent duplicate or lost messages during data processing
C. To allow multiple consumers to read the same message simultaneously
D. To increase the speed of message delivery

Solution

  1. Step 1: Understand the purpose of EOS

    Exactly-once semantics ensures that each message is processed exactly one time, avoiding duplicates or losses.
  2. Step 2: Compare options with EOS goal

    Options A, B, and C describe other Kafka features but not EOS's main goal.
  3. Final Answer:

    To prevent duplicate or lost messages during data processing -> Option B
  4. Quick Check:

    EOS = prevent duplicates/losses [OK]
Hint: EOS means no duplicates or lost messages [OK]
Common Mistakes:
  • Confusing EOS with message speed optimization
  • Thinking EOS allows multiple consumers to read simultaneously
  • Mixing EOS with message compression features
2. Which configuration must be enabled in a Kafka producer to support Exactly-once semantics?
easy
A. max.poll.records = 500
B. acks = 0
C. compression.type = gzip
D. enable.idempotence = true

Solution

  1. Step 1: Identify EOS required producer setting

    Enabling idempotence (enable.idempotence = true) is necessary for EOS to avoid duplicates.
  2. Step 2: Review other options

    Options B, C, and D relate to acknowledgments, compression, and consumer polling, not EOS.
  3. Final Answer:

    enable.idempotence = true -> Option D
  4. Quick Check:

    Idempotence enabled = EOS support [OK]
Hint: Enable idempotence for EOS in producer config [OK]
Common Mistakes:
  • Setting acks to 0 disables guarantees
  • Confusing compression with EOS
  • Using consumer settings instead of producer
3. Given the following Kafka producer code snippet with transactions enabled, what will happen if producer.commitTransaction() is not called after producer.send()?
producer.initTransactions();
producer.beginTransaction();
producer.send(record);
// Missing commitTransaction call
producer.close();
medium
A. The transaction will be aborted and messages will not be committed
B. Messages will be committed automatically on close
C. Messages will be duplicated in the topic
D. Producer will throw a syntax error

Solution

  1. Step 1: Understand transaction lifecycle in Kafka

    After beginning a transaction and sending messages, commitTransaction() must be called to finalize the transaction.
  2. Step 2: Effect of missing commitTransaction()

    If commitTransaction() is not called, the transaction is aborted on close, so messages are not committed.
  3. Final Answer:

    The transaction will be aborted and messages will not be committed -> Option A
  4. Quick Check:

    Missing commitTransaction = abort transaction [OK]
Hint: Always commit or abort transactions explicitly [OK]
Common Mistakes:
  • Assuming close commits automatically
  • Expecting duplicates without commit
  • Thinking missing commit causes syntax error
4. You have enabled EOS in your Kafka producer but notice duplicate messages in the topic. Which of the following is the most likely cause?
medium
A. Producer did not call initTransactions() before starting transactions
B. Producer set enable.idempotence to true
C. Consumer group is reading messages multiple times
D. Topic has replication factor set to 3

Solution

  1. Step 1: Check EOS setup requirements

    initTransactions() must be called before using transactions to ensure EOS works properly.
  2. Step 2: Analyze other options

    Producer set enable.idempotence to true is correct config, A relates to consumers not producers, D is unrelated to duplicates.
  3. Final Answer:

    Producer did not call initTransactions() before starting transactions -> Option A
  4. Quick Check:

    Missing initTransactions() causes duplicates [OK]
Hint: Call initTransactions() before transactions for EOS [OK]
Common Mistakes:
  • Confusing consumer behavior with producer duplicates
  • Assuming replication factor affects duplicates
  • Thinking idempotence alone fixes all issues
5. You want to implement a Kafka producer that writes messages exactly once to multiple partitions atomically. Which combination of settings and steps is required to achieve this?
hard
A. Disable idempotence, send messages asynchronously, then flush producer
B. Set acks=0, send messages to partitions, then close producer
C. Enable idempotence, initialize and begin transactions, send messages, then commit transaction
D. Use synchronous sends without transactions and rely on consumer deduplication

Solution

  1. Step 1: Identify EOS requirements for atomic writes

    Enable idempotence and use transactions to ensure atomic writes across partitions.
  2. Step 2: Follow correct transaction steps

    Initialize transactions, begin transaction, send messages, then commit transaction to finalize.
  3. Step 3: Evaluate other options

    Options B, C, and D lack idempotence or transactions, so they cannot guarantee exactly-once atomic writes.
  4. Final Answer:

    Enable idempotence, initialize and begin transactions, send messages, then commit transaction -> Option C
  5. Quick Check:

    Idempotence + transactions = atomic exactly-once writes [OK]
Hint: Use idempotence + transactions for atomic exactly-once writes [OK]
Common Mistakes:
  • Ignoring transaction lifecycle steps
  • Relying on consumer deduplication only
  • Disabling idempotence for performance
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