Bird
Raised Fist0

What is the issue with the following Python confluent-kafka producer code?

medium📝 Debug Q6 of Q15
Kafka - with Java/Python
What is the issue with the following Python confluent-kafka producer code?
from confluent_kafka import Producer

p = Producer({'bootstrap.servers': 'localhost:9092'})
p.produce('topic1', value='message')
# Program ends here without flush()
AThe producer configuration is invalid without specifying <code>group.id</code>.
BThe <code>produce()</code> method requires a key parameter, so this code will raise an error.
CMessages may not be sent because <code>flush()</code> was not called before program exit.
DCalling <code>produce()</code> without <code>poll()</code> causes a deadlock.
Step-by-Step Solution
Solution:
  1. Step 1: Produce message

    The message is queued asynchronously by produce().
  2. Step 2: Missing flush()

    Without calling flush(), the program may exit before messages are sent.
  3. Step 3: Consequence

    Messages remain in the internal buffer and are lost if the program terminates immediately.
  4. Final Answer:

    Messages may not be sent because flush() was not called. -> Option C
  5. Quick Check:

    Always flush before exit to send all messages [OK]
Quick Trick: Flush before program exit to avoid message loss [OK]
Common Mistakes:
MISTAKES
  • Assuming produce() sends messages synchronously
  • Not calling flush() or poll() before exit
  • Believing key is mandatory for produce()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes