0
0
Kafkadevops~20 mins

Dead letter queue pattern in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Dead Letter Queue 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 consumer code snippet?

Consider a Kafka consumer configured to process messages from a topic. If a message processing fails, it is sent to a dead letter queue (DLQ). What will be the output after processing the following messages?

Kafka
from kafka import KafkaConsumer, KafkaProducer

consumer = KafkaConsumer('main_topic', bootstrap_servers='localhost:9092')
producer = KafkaProducer(bootstrap_servers='localhost:9092')

def process_message(msg):
    if msg == b'bad_message':
        raise ValueError('Processing failed')
    return msg.decode('utf-8').upper()

for message in consumer:
    try:
        result = process_message(message.value)
        print(f'Processed: {result}')
    except Exception as e:
        producer.send('dead_letter_queue', message.value)
        print(f'Message sent to DLQ: {message.value.decode("utf-8")}')
    break  # For this example, process only one message
AProcessed: bad_message
BProcessed: GOOD_MESSAGE
CMessage sent to DLQ: bad_message
DNo output, consumer hangs
Attempts:
2 left
💡 Hint

Think about what happens when process_message raises an exception.

🧠 Conceptual
intermediate
1:00remaining
What is the main purpose of a dead letter queue in Kafka?

Choose the best description of why a dead letter queue (DLQ) is used in Kafka message processing.

ATo store all successfully processed messages for auditing
BTo capture messages that cannot be processed due to errors
CTo temporarily hold messages before processing
DTo speed up message consumption by parallel processing
Attempts:
2 left
💡 Hint

Think about what happens to messages that cause errors during processing.

🔧 Debug
advanced
2:30remaining
Why does this Kafka consumer fail to send messages to the dead letter queue?

Examine the following Kafka consumer code snippet. It is supposed to send failed messages to a dead letter queue, but it never does. What is the cause?

Kafka
from kafka import KafkaConsumer, KafkaProducer

consumer = KafkaConsumer('main_topic', bootstrap_servers='localhost:9092')
producer = KafkaProducer(bootstrap_servers='localhost:9092')

def process_message(msg):
    if msg == b'error':
        raise Exception('Error')
    return msg.decode('utf-8')

for message in consumer:
    try:
        print(process_message(message.value))
    except Exception:
        producer.send('dead_letter_queue', message.value)
producer.flush()
AThe producer.send() is asynchronous and flush() is never called inside the loop, so messages remain buffered
BThe producer.flush() is called outside the loop, so messages are never sent
CThe consumer is not subscribed to the dead letter queue topic
DThe process_message function does not raise exceptions properly
Attempts:
2 left
💡 Hint

Consider how KafkaProducer sends messages asynchronously and when they are actually sent.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a Kafka consumer with a dead letter queue handler in Python?

Choose the code snippet that correctly sets up a Kafka consumer that sends failed messages to a dead letter queue.

A
consumer = KafkaConsumer('topic', bootstrap_servers='localhost:9092')
producer = KafkaProducer(bootstrap_servers='localhost:9092')
for msg in consumer:
    try:
        process(msg.value)
    except:
        producer.send('dlq', msg.value)
        producer.flush()
B
consumer = KafkaConsumer('topic', bootstrap_servers='localhost:9092')
producer = KafkaProducer(bootstrap_servers='localhost:9092')
for msg in consumer:
    try:
        process(msg.value)
    except:
        producer.send('dlq', msg.value)
producer.flush()
C
consumer = KafkaConsumer('topic', bootstrap_servers='localhost:9092')
producer = KafkaProducer(bootstrap_servers='localhost:9092')
for msg in consumer:
    try:
        process(msg.value)
    except Exception as e:
        producer.send('dlq', msg.value)
        producer.flush()
D
consumer = KafkaConsumer('topic', bootstrap_servers='localhost:9092')
producer = KafkaProducer(bootstrap_servers='localhost:9092')
for msg in consumer:
    try:
        process(msg.value)
    except Exception:
        producer.send('dlq', msg.value)
        producer.flush()
Attempts:
2 left
💡 Hint

Look for correct exception handling syntax and proper flush usage inside the loop.

🚀 Application
expert
3:00remaining
How many messages will be in the dead letter queue after processing this batch?

A Kafka consumer processes a batch of 5 messages: ['ok', 'fail', 'ok', 'fail', 'fail']. The process_message function raises an exception for 'fail' messages, which are sent to the dead letter queue. How many messages will the dead letter queue contain after processing all 5?

Kafka
messages = [b'ok', b'fail', b'ok', b'fail', b'fail']
failed_messages = []

def process_message(msg):
    if msg == b'fail':
        raise Exception('Failed')
    return msg.decode('utf-8')

for msg in messages:
    try:
        process_message(msg)
    except Exception:
        failed_messages.append(msg)

print(len(failed_messages))
A3
B1
C2
D0
Attempts:
2 left
💡 Hint

Count how many messages cause exceptions.