Sending Large Messages in Apache Kafka: A Guide with Best Practices
Apache Kafka excels at handling high volumes of data. However, it’s not optimized for efficiently processing very large individual messages, typically exceeding 1MB in size. This is because large messages can introduce inefficiencies within the Kafka architecture.
Despite this, there may be scenarios where you require sending large files or data through Kafka. Perhaps you’re working with log files, images, or other bulky data types. In these cases, it’s important to understand the potential drawbacks and explore alternative approaches to ensure optimal performance within your Kafka ecosystem.
This article explores two effective approaches for handling large messages in Kafka:
1. Leverage External Storage and Reference Messages
This approach is generally recommended for large messages (think gigabytes).
Here’s how it works:
- Store the Large Message Externally: Upload your large message (video, archive file, etc.) to a reliable external storage system like HDFS, Amazon S3, Google Cloud Storage, or even an FTP server.
- Send a Reference Message to Kafka: Instead of sending the entire large message through Kafka, send a small message containing a reference (e.g., URL, file path) to the actual data stored externally.
- Develop Custom Producer and Consumer Code: Write custom code on both the producer and consumer sides to handle this pattern. The producer sends the large message to external storage and the reference message to Kafka. The consumer retrieves the reference message from Kafka, locates the large message in external storage, and retrieves it for processing.
Benefits:
- Improved Efficiency: By keeping messages in Kafka small, you optimize throughput and resource utilization.
- Scalability: External storage solutions are typically designed for handling large volumes of data efficiently.
2. Increase Kafka Message Limits (with Caution)
While less recommended, you can increase Kafka’s default message size limit. This approach is suitable for moderately large messages (up to 10MB). Here’s what you need to configure:
Configuration Changes
| Setting | Description | Location |
| message.max.bytes (broker-side) | Maximum message size allowed by the broker | server.properties |
| max.message.bytes (topic-side) | Maximum message size allowed for a specific topic | Dynamically through Kafka APIs or broker configuration |
| replica.fetch.max.bytes (broker-side) | Maximum message size replicated between brokers | server.properties |
| max.partition.fetch.bytes (consumer-side) | Maximum message size a consumer can fetch from a partition | Consumer configuration |
| max.request.size (producer-side) | Maximum size of producer requests | Producer configuration |
Important Considerations
- Performance Impact: Sending larger messages can consume more memory and CPU on producers, brokers, and consumers, potentially impacting performance.
- Increased Latency: Larger messages take longer to transmit and process, leading to higher latency.
- Limited Scalability: Increasing message sizes might not scale well for very large data volumes.
Conclusion
In most cases, the best course of action for handling large messages in Kafka is to leverage external storage. This strategy prioritizes efficiency and scalability. By storing large data in a dedicated system like cloud storage and sending only reference messages through Kafka, you avoid potential constriction and ensure smooth operation within your Kafka environment.
However, there might be situations where sending moderately large messages directly through Kafka is unavoidable. If this is the case, it’s crucial to carefully consider the configuration changes required and the potential drawbacks associated with this approach. Remember, keeping message sizes small is a core principle for achieving optimal performance in Kafka.


Leave a comment