How to send and consume json messages using confluent-kafka in Python
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Confluent Kafka, which is based on Apache Kafka, is a popular choice for handling real-time data feeds. Its ability to handle high-throughput and low-latency messaging makes it essential in modern data architectures. Python, with its simplicity and powerful libraries, is commonly used to interact with Confluent Kafka. Specifically, the confluent_kafka Python library provides a robust and performant way to produce and consume JSON messages.
Sending JSON Messages with Confluent Kafka in Python
Setup and Configuration
To start, you’ll need to install the Confluent Kafka Python client. You can do this via pip:
Next, you need to configure the producer. Here are the essential configurations:
bootstrap.servers: Specifies the Kafka broker address.client.id: Identifier of the producer/client.
Here is an example configuration:
Preparing the JSON Message
Since Kafka essentially deals with string or byte formats, you will need to serialize JSON data before sending it. You can use Python’s built-in json library for serialization:
Sending the Message
With the producer configured and the JSON message prepared, you can now send this message to a specific Kafka topic:
Consuming JSON Messages with Confluent Kafka in Python
Configuration
Similarly, to receive messages, configure the Kafka consumer:
Fetching and Processing Messages
To read the messages, continuously poll the consumer and deserialize any incoming JSON message:
Key Points Overview
Here is a summary table of key points when working with confluent_kafka:
| Aspect | Producer | Consumer |
| Purpose | Sends messages | Receives messages |
| Installation | pip install confluent_kafka | pip install confluent_kafka |
| Key Configuration | bootstrap.servers, client.id | bootstrap.servers, group.id, auto.offset.reset |
| Serialization | JSON to bytes using json.dumps().encode() | Bytes to JSON using json.loads().decode() |
| Methods | produce(), flush() | poll(), close() |
Additional Considerations
- Error Handling: Implement robust error handling especially for network issues, serialization errors, or Kafka broker downtimes.
- Message Encoding/Decoding: Always ensure that the message encoding during the produce matches the decoding during consume.
- Performance: Handle batch processing when dealing with large volumes of data to optimize performance and resource usage.
Using Confluent Kafka with Python for JSON messaging involves setting up appropriate producers and consumers, careful handling of serialization, and implementing error handling and performance optimizations. This approach enables effective and efficient real-time data integration in your applications.
Related reading
- how to send batched data with Spring Kafka producer
- How to send final kafka-streams aggregation result of a time windowed KTable?
- How to send headers using KStream
- how to send JSON object to kafka from python client
- How to send email to multiple recipients using python smtplib?
- How to SEND multiple requests ASYNC from Flask server?
- How to send JSON payload to RabbitMQ using the web plugin?
- How to send message in rabbitmq on docker?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.