Bird
Raised Fist0
Kafkadevops~20 mins

First message (produce and consume) in Kafka - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kafka First Message 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?

Consider this Kafka producer code snippet in Python using confluent_kafka. What will it print?

Kafka
from confluent_kafka import Producer

conf = {'bootstrap.servers': 'localhost:9092'}
producer = Producer(conf)

producer.produce('test-topic', key='key1', value='Hello Kafka!')
producer.flush()

print('Message sent')
ATimeoutError
BMessage sent
CKeyError
DHello Kafka!
Attempts:
2 left
💡 Hint

The produce method queues the message, and flush sends it. The print statement runs after sending.

❓ Predict Output
intermediate
2:00remaining
What does this Kafka consumer print after consuming one message?

Given this Kafka consumer code in Python, what will it print after receiving one message?

Kafka
from confluent_kafka import Consumer

conf = {
    'bootstrap.servers': 'localhost:9092',
    'group.id': 'mygroup',
    'auto.offset.reset': 'earliest'
}
consumer = Consumer(conf)
consumer.subscribe(['test-topic'])

msg = consumer.poll(1.0)
if msg is None:
    print('No message received')
elif msg.error():
    print(f'Error: {msg.error()}')
else:
    print(f'Received: {msg.value().decode("utf-8")}')
consumer.close()
AError: KafkaError{code=_TIMED_OUT}
BNo message received
CReceived: key1
DReceived: Hello Kafka!
Attempts:
2 left
💡 Hint

The consumer reads the message value, decoding bytes to string.

🔧 Debug
advanced
3:00remaining
Why does this Kafka consumer code raise an error?

This Kafka consumer code raises an exception. What is the cause?

Kafka
from confluent_kafka import Consumer

conf = {
    'bootstrap.servers': 'localhost:9092',
    'group.id': 'mygroup'
}
consumer = Consumer(conf)
consumer.subscribe(['test-topic'])

msg = consumer.poll(1.0)
print(msg.value().decode('utf-8'))
consumer.close()
ATimeoutError because poll timeout is too short
BKeyError because 'auto.offset.reset' is missing
CAttributeError because msg is None and msg.value() is called
DTypeError because decode is called on a string
Attempts:
2 left
💡 Hint

Check what happens if poll returns None.

📝 Syntax
advanced
2:00remaining
Which option correctly produces a message with key and value in Kafka?

Which of these Python code snippets correctly produces a Kafka message with key 'user1' and value 'data'?

Aproducer.produce('topic', key='user1', value='data')
Bproducer.produce('topic', 'user1', 'data')
Cproducer.produce(topic='topic', key='user1', value='data')
Dproducer.produce('topic', key=user1, value=data)
Attempts:
2 left
💡 Hint

Remember that key and value must be strings or bytes, and keyword arguments are required.

🚀 Application
expert
3:00remaining
How many messages will be consumed by this Kafka consumer loop?

Given this Kafka consumer code, how many messages will it consume before stopping?

Kafka
from confluent_kafka import Consumer

conf = {
    'bootstrap.servers': 'localhost:9092',
    'group.id': 'mygroup',
    'auto.offset.reset': 'earliest'
}
consumer = Consumer(conf)
consumer.subscribe(['test-topic'])

count = 0
while True:
    msg = consumer.poll(0.5)
    if msg is None:
        break
    if msg.error():
        break
    count += 1
consumer.close()
print(count)
ANumber of messages currently in 'test-topic' partition(s)
B0 because poll timeout is too short
CInfinite loop because break conditions are wrong
D1 because it stops after first message
Attempts:
2 left
💡 Hint

The loop stops when poll returns None, which means no more messages are available.

Practice

(1/5)
1. What is the main purpose of producing a message in Kafka?
easy
A. To send data to a Kafka topic
B. To read data from a Kafka topic
C. To delete messages from a Kafka topic
D. To create a new Kafka topic

Solution

  1. Step 1: Understand Kafka producing

    Producing means sending messages to a Kafka topic as bytes.
  2. Step 2: Differentiate producing from consuming

    Consuming reads messages, but producing sends them.
  3. Final Answer:

    To send data to a Kafka topic -> Option A
  4. Quick Check:

    Producing = Sending messages [OK]
Hint: Producing means sending messages to Kafka topics [OK]
Common Mistakes:
  • Confusing producing with consuming
  • Thinking producing deletes messages
  • Assuming producing creates topics
2. Which of the following is the correct way to create a Kafka producer in Python using the kafka-python library?
easy
A. producer = KafkaConsumer(topic='mytopic')
B. producer = KafkaConsumer(bootstrap_servers='localhost:9092')
C. producer = KafkaProducer(topic='mytopic')
D. producer = KafkaProducer(bootstrap_servers='localhost:9092')

Solution

  1. Step 1: Identify Kafka producer creation syntax

    KafkaProducer is created with bootstrap_servers parameter specifying Kafka server address.
  2. Step 2: Eliminate consumer and wrong parameters

    KafkaConsumer is for consuming, not producing. Topic is not a parameter for producer creation.
  3. Final Answer:

    producer = KafkaProducer(bootstrap_servers='localhost:9092') -> Option D
  4. Quick Check:

    Producer uses KafkaProducer with bootstrap_servers [OK]
Hint: Producer uses KafkaProducer with bootstrap_servers [OK]
Common Mistakes:
  • Using KafkaConsumer instead of KafkaProducer
  • Passing topic during producer creation
  • Missing bootstrap_servers parameter
3. Consider this Python code snippet using kafka-python:
from kafka import KafkaProducer, KafkaConsumer

producer = KafkaProducer(bootstrap_servers='localhost:9092')
producer.send('test-topic', b'Hello Kafka')
producer.flush()

consumer = KafkaConsumer('test-topic', bootstrap_servers='localhost:9092', auto_offset_reset='earliest', consumer_timeout_ms=1000)
for msg in consumer:
    print(msg.value.decode())
What will be the output when this code runs?
medium
A. Error: topic not found
B. Hello Kafka
C. No output, consumer times out
D. b'Hello Kafka'

Solution

  1. Step 1: Analyze producer sending message

    Producer sends bytes message b'Hello Kafka' to 'test-topic' and flushes to ensure delivery.
  2. Step 2: Analyze consumer reading message

    Consumer reads from 'test-topic' from earliest offset and decodes bytes to string before printing.
  3. Final Answer:

    Hello Kafka -> Option B
  4. Quick Check:

    Consumer prints decoded message string [OK]
Hint: Consumer decodes bytes to string before printing [OK]
Common Mistakes:
  • Expecting raw bytes output
  • Assuming consumer misses message due to timeout
  • Thinking topic does not exist
4. You wrote this code to consume messages but it raises an error:
consumer = KafkaConsumer('my-topic')
for message in consumer:
    print(message.value.decode())
What is the most likely cause of the error?
medium
A. Using decode() on message.value which is already a string
B. Not calling consumer.poll() before iterating
C. Missing bootstrap_servers parameter when creating KafkaConsumer
D. Topic name should be a list, not a string

Solution

  1. Step 1: Check KafkaConsumer creation requirements

    KafkaConsumer requires bootstrap_servers parameter to connect to Kafka cluster.
  2. Step 2: Validate other options

    decode() is needed because message.value is bytes; consumer.poll() is optional; topic can be string or list.
  3. Final Answer:

    Missing bootstrap_servers parameter when creating KafkaConsumer -> Option C
  4. Quick Check:

    KafkaConsumer needs bootstrap_servers [OK]
Hint: Always specify bootstrap_servers when creating KafkaConsumer [OK]
Common Mistakes:
  • Omitting bootstrap_servers parameter
  • Assuming message.value is string without decoding
  • Thinking poll() is mandatory before iteration
5. You want to produce and consume the first message in a Kafka topic named 'orders'. Which sequence of steps correctly achieves this in Python using kafka-python?
hard
A. Create KafkaProducer with bootstrap_servers, send message as bytes, flush; create KafkaConsumer with bootstrap_servers and auto_offset_reset='earliest', then read and decode messages
B. Create KafkaConsumer first, then KafkaProducer; send message as string; consumer reads without decoding
C. Create KafkaProducer without bootstrap_servers, send string message; create KafkaConsumer with bootstrap_servers, read messages as bytes
D. Create KafkaProducer with topic parameter, send message; create KafkaConsumer without bootstrap_servers, read and decode messages

Solution

  1. Step 1: Properly create KafkaProducer and send message

    KafkaProducer must be created with bootstrap_servers; messages sent as bytes and flushed to ensure delivery.
  2. Step 2: Properly create KafkaConsumer and read messages

    KafkaConsumer needs bootstrap_servers and auto_offset_reset='earliest' to read from the first message; decode bytes to string when reading.
  3. Final Answer:

    Create KafkaProducer with bootstrap_servers, send message as bytes, flush; create KafkaConsumer with bootstrap_servers and auto_offset_reset='earliest', then read and decode messages -> Option A
  4. Quick Check:

    Correct producer and consumer setup with bytes and decoding [OK]
Hint: Producer sends bytes and flushes; consumer reads earliest and decodes [OK]
Common Mistakes:
  • Creating producer or consumer without bootstrap_servers
  • Sending string instead of bytes
  • Not setting auto_offset_reset to 'earliest'
  • Not decoding message bytes before printing
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