Message Size in Kafka
πΉ Default Maximum Message Size in Kafka
- By default, Kafka has a maximum message size of 1 MB.
- This is controlled by the broker property:
message.max.bytes=1048576
(= 1 MB = 1,048,576 bytes).
πΉ Can it be Increased? β
Yes, Kafka allows larger messages, but you must configure it in multiple places:
-
Broker Side
-
message.max.bytes
(max size a broker will accept for a single message).
-
-
Topic Level (Optional)
-
Can override using:
kafka-topics.sh --create \ --topic large-messages \ --config max.message.bytes=52428800
(e.g., 50 MB for this topic).
-
-
Producer Side
-
max.request.size
(default 1 MB). -
Increase it to match the largest message you want to send.
props.put("max.request.size", 52428800); // 50 MB
-
-
Consumer Side
-
fetch.message.max.bytes
(old name) ormax.partition.fetch.bytes
(new name). -
Controls the maximum size of a message batch the consumer will fetch.
props.put("max.partition.fetch.bytes", 52428800); // 50 MB
-
π All three must align (producer, broker, consumer), or youβll hit errors.
πΉ Practical Considerations
- Kafka is optimized for many small messages, not giant ones.
- Very large messages can:
- Cause high memory usage (serialization, deserialization).
- Lead to longer GC pauses in JVM.
- Break batching efficiency β reduced throughput.
π Best Practice: Keep messages small (KB range). For large payloads:
- Store data in object storage (e.g., S3, HDFS, MinIO).
- Send only a reference (URL, ID, metadata) in Kafka.
πΉ In Short
- Default max message size = 1 MB.
- Can be increased via:
- Broker β
message.max.bytes
- Producer β
max.request.size
- Consumer β
max.partition.fetch.bytes
- Broker β
- β Possible to send 10MB, 50MB, or even moreβ¦ but β not recommended for performance.