Bird
Raised Fist0
Kafkadevops~10 mins

Dead letter queue pattern 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 send a message to the dead letter queue topic.

Kafka
producer.send("[1]", record);
Drag options to blanks, or click blank then click option'
Amain-topic
Bdead-letter-queue
Cretry-topic
Derror-topic
Attempts:
3 left
💡 Hint
Common Mistakes
Sending the message to the main topic instead of the dead letter queue.
Using a retry topic instead of the dead letter queue.
2fill in blank
medium

Complete the code to check if the message processing failed and send it to the dead letter queue.

Kafka
if (processingFailed) {
    producer.send("[1]", message);
}
Drag options to blanks, or click blank then click option'
Adead-letter-queue
Bretry-topic
Cmain-topic
Dsuccess-topic
Attempts:
3 left
💡 Hint
Common Mistakes
Sending failed messages back to the main topic.
Sending failed messages to a retry topic instead of dead letter queue.
3fill in blank
hard

Fix the error in the consumer code to consume messages from the dead letter queue topic.

Kafka
consumer.subscribe(Collections.singletonList("[1]"));
Drag options to blanks, or click blank then click option'
Adead-letter-queue
Bmain-topic
Cretry-topic
Derror-topic
Attempts:
3 left
💡 Hint
Common Mistakes
Subscribing to the main topic instead of the dead letter queue.
Using a retry topic for dead letter messages.
4fill in blank
hard

Fill both blanks to create a dead letter queue producer and send a failed message.

Kafka
KafkaProducer<String, String> producer = new KafkaProducer<>(props);
producer.send(new ProducerRecord<String, String>("[1]", "[2]"));
Drag options to blanks, or click blank then click option'
Adead-letter-queue
BfailedMessage
Cmain-topic
DsuccessMessage
Attempts:
3 left
💡 Hint
Common Mistakes
Sending success messages to the dead letter queue.
Using the main topic for failed messages.
5fill in blank
hard

Fill all three blanks to filter messages and send only failed ones to the dead letter queue.

Kafka
for (var record : consumer.poll(Duration.ofMillis(100))) {
    if (record.value().[1]("error")) {
        producer.send(new ProducerRecord<>("[2]", record.value()));
    } else {
        producer.send(new ProducerRecord<>("[3]", record.value()));
    }
}
Drag options to blanks, or click blank then click option'
Acontains
Bdead-letter-queue
Cmain-topic
DstartsWith
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'startsWith' instead of 'contains' to check for errors.
Sending all messages to the dead letter queue.

Practice

(1/5)
1. What is the main purpose of a Dead Letter Queue (DLQ) in Kafka?
easy
A. To store messages that failed processing for later analysis
B. To speed up message processing by duplicating messages
C. To permanently delete messages after processing
D. To encrypt messages before sending to consumers

Solution

  1. Step 1: Understand the role of DLQ

    A Dead Letter Queue is used to hold messages that cannot be processed successfully by the main consumer.
  2. Step 2: Identify the purpose of storing failed messages

    Storing failed messages allows developers to analyze and fix issues without blocking the main processing flow.
  3. Final Answer:

    To store messages that failed processing for later analysis -> Option A
  4. Quick Check:

    DLQ = store failed messages [OK]
Hint: DLQ holds failed messages to avoid blocking main flow [OK]
Common Mistakes:
  • Thinking DLQ speeds up processing by duplicating messages
  • Confusing DLQ with message deletion
  • Assuming DLQ encrypts messages
2. Which Kafka configuration is commonly used to specify the Dead Letter Queue topic for a consumer?
easy
A. enable.auto.commit
B. auto.offset.reset
C. max.poll.records
D. dead.letter.queue.topic.name

Solution

  1. Step 1: Identify DLQ related configuration

    The configuration dead.letter.queue.topic.name is used to specify the topic where failed messages are sent.
  2. Step 2: Differentiate from other common configs

    Other options like enable.auto.commit and auto.offset.reset control offset behavior, not DLQ.
  3. Final Answer:

    dead.letter.queue.topic.name -> Option D
  4. Quick Check:

    DLQ topic config = dead.letter.queue.topic.name [OK]
Hint: DLQ topic config usually contains 'dead.letter.queue' phrase [OK]
Common Mistakes:
  • Confusing offset configs with DLQ topic config
  • Using unrelated consumer configs for DLQ
  • Assuming DLQ is automatic without config
3. Given the following Kafka consumer code snippet, what happens when message processing fails?
try {
  processMessage(record);
} catch (Exception e) {
  producer.send(new ProducerRecord<>("dlq-topic", record.key(), record.value()));
}
medium
A. The message is silently dropped without notification
B. The message is retried immediately in the same consumer loop
C. The failed message is sent to the "dlq-topic" for later review
D. The consumer stops processing all messages

Solution

  1. Step 1: Analyze the catch block behavior

    When an exception occurs, the code sends the failed message to the "dlq-topic" using a producer.
  2. Step 2: Understand the effect on message processing

    The message is not retried or dropped silently; it is stored in the DLQ topic for later analysis.
  3. Final Answer:

    The failed message is sent to the "dlq-topic" for later review -> Option C
  4. Quick Check:

    Exception handling sends message to DLQ [OK]
Hint: Catch block sends failed messages to DLQ topic [OK]
Common Mistakes:
  • Assuming immediate retry happens automatically
  • Thinking consumer stops on failure
  • Believing message is dropped silently
4. Identify the error in this Kafka consumer code snippet that tries to send failed messages to a DLQ:
try {
  process(record);
} catch (Exception e) {
  producer.send("dlq-topic", record.key(), record.value());
}
medium
A. The catch block should rethrow the exception
B. The producer.send method is missing a ProducerRecord object
C. The process method should not be inside try-catch
D. The record.key() cannot be used as a message key

Solution

  1. Step 1: Check the producer.send method usage

    The producer.send method requires a ProducerRecord object, not separate parameters.
  2. Step 2: Identify correct usage

    The correct call is producer.send(new ProducerRecord<>("dlq-topic", record.key(), record.value()));
  3. Final Answer:

    The producer.send method is missing a ProducerRecord object -> Option B
  4. Quick Check:

    producer.send needs ProducerRecord object [OK]
Hint: producer.send requires ProducerRecord, not separate args [OK]
Common Mistakes:
  • Passing topic and data as separate arguments to send
  • Ignoring exception handling best practices
  • Misusing record key as invalid
5. You want to implement a Dead Letter Queue pattern in Kafka that retries failed messages up to 3 times before sending them to the DLQ. Which approach correctly achieves this?
hard
A. Use a retry counter in message headers; on failure increment and retry, else send to DLQ after 3 tries
B. Send all failed messages immediately to DLQ without retries
C. Configure Kafka broker to automatically retry messages 3 times before DLQ
D. Use a separate consumer that deletes messages after 3 retries

Solution

  1. Step 1: Understand retry logic with DLQ

    Retries require tracking how many times a message was processed; using headers to count retries is common.
  2. Step 2: Implement conditional retry or DLQ send

    If retry count is less than 3, retry processing; if 3 or more, send message to DLQ topic.
  3. Step 3: Evaluate other options

    Kafka broker does not handle retries automatically; immediate DLQ send skips retries; deleting messages after retries is incorrect.
  4. Final Answer:

    Use a retry counter in message headers; on failure increment and retry, else send to DLQ after 3 tries -> Option A
  5. Quick Check:

    Retry count in headers controls DLQ after 3 tries [OK]
Hint: Track retries in headers; send to DLQ after max retries [OK]
Common Mistakes:
  • Expecting Kafka broker to retry automatically
  • Sending all failures immediately to DLQ
  • Deleting messages instead of sending to DLQ
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