How can I send large messages with Kafka (over 15MB)?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Sending large messages with Kafka (over the default limit of 15MB) involves adjustments to the producer, broker, and consumer configurations, as well as some best practices to handle large payloads efficiently. Here's how you can achieve this:
1. Increase Kafka's Maximum Message Size
Kafka has configurable limits for message size. You need to increase these limits in the producer, broker, and consumer.
Producer Configuration
Set the max.request.size to a larger value (in bytes) in the producer to allow sending large messages.
Broker Configuration
Set the message.max.bytes in the broker to a value slightly larger than the producer’s max.request.size.
- Edit the
server.propertiesfile on the broker:
Consumer Configuration
Set the fetch.max.bytes in the consumer to a value larger than the largest expected message size.
Topic-Specific Configuration
Optionally, set the topic-specific limit:
2. Split Large Messages into Smaller Chunks
For very large messages, splitting the payload into smaller chunks and reassembling them on the consumer side is a better practice. Kafka is optimized for small, high-throughput messages, so handling large payloads this way improves performance.
- Producer Side: Split the payload into smaller parts and send them with a unique key or identifier.
- Consumer Side: Reassemble the chunks based on the unique key or identifier.
Example:
3. Use an External Storage for Large Payloads
Instead of sending the entire payload through Kafka, you can store the large message in an external storage system (e.g., Amazon S3, HDFS) and send a reference or URL through Kafka.
Producer Side
- Upload the large payload to external storage.
- Send the URL or reference in the Kafka message.
Example:
Consumer Side
- Read the reference or URL from Kafka.
- Fetch the payload from the external storage.
4. Optimize Compression
Enable compression in Kafka to reduce the size of messages before they are sent. Supported compression codecs include:
gzipsnappylz4zstd(Kafka 2.1 and above)
Producer Configuration:
Compression can significantly reduce the size of text-based or highly compressible payloads.
5. Monitor and Tune Performance
When dealing with large messages, ensure that Kafka’s performance and resource utilization remain optimal:
- Monitor Broker Memory Usage: Large messages increase memory usage due to buffering.
- Adjust
replica.fetch.max.bytes: Ensure that the replica brokers can handle large messages by settingreplica.fetch.max.bytesinserver.propertiesto a value larger thanmessage.max.bytes. - Increase Network Buffering: Adjust the network buffer size to accommodate larger messages:
Summary
| Action | Configuration/Practice |
| Increase Producer Limit | max.request.size |
| Increase Broker Limit | message.max.bytes |
| Increase Consumer Limit | fetch.max.bytes |
| Split Large Messages | Manually split and reassemble on consumer side |
| Use External Storage | Store large payloads externally and send a reference in Kafka messages |
| Enable Compression | Use compression.type to reduce message size |
| Tune Replica Fetch Size | Adjust replica.fetch.max.bytes for replicas |
Recommendation
For messages slightly larger than 15MB, increasing Kafka's size limits is sufficient. However, for significantly larger payloads, splitting messages or using external storage with Kafka as a metadata transport is the most robust and efficient solution.

