Resolving Large Message Transmission Issues in Apache Kafka

Nov 22, 2025 · Programming · 9 views · 7.8

Keywords: Kafka | Large Message Transmission | MessageSizeTooLargeException | Configuration Optimization | Message Size Limits

Abstract: This paper provides an in-depth analysis of the MessageSizeTooLargeException encountered when handling large messages in Apache Kafka. It details the four critical configuration parameters that need adjustment: message.max.bytes, replica.fetch.max.bytes, fetch.message.max.bytes, and max.message.bytes. Through comprehensive configuration examples and exception analysis, it helps developers understand Kafka's message size limitation mechanisms and offers effective solutions.

Problem Background and Exception Analysis

When using Apache Kafka version 0.8, attempting to send large messages exceeding 15MB often results in kafka.common.MessageSizeTooLargeException. This issue is particularly evident on the producer side, where the exception persists even after setting message.max.bytes to 40MB. Error logs reveal that the producer makes multiple unsuccessful attempts to send messages, eventually throwing FailedToSendMessageException.

Core Configuration Parameters Analysis

To comprehensively resolve large message transmission issues, four key configuration parameters distributed across different layers of the Kafka architecture need simultaneous adjustment:

Producer-Side Configuration

In producer configuration, while users may have set the message.max.bytes parameter, this represents only part of the solution. In Kafka 0.8, producers primarily use message.max.bytes to control the maximum size of sent messages. However, adjusting this single parameter is insufficient because Kafka's message processing involves coordination across multiple stages.

Critical Broker-Side Configurations

Two crucial configuration parameters require synchronous adjustment on the broker side:

The message.max.bytes parameter defines the maximum message size that the broker can accept from producers. This parameter serves as the first checkpoint for messages entering the Kafka system. If a message exceeds this limit, the broker will directly reject it.

The replica.fetch.max.bytes parameter controls the maximum size for message synchronization between replicas. This parameter is often overlooked but holds significant importance. If set too small, even if a message is successfully received by the primary broker, it cannot be properly replicated across cluster replicas. In such cases, the message never gets fully committed, making it invisible to consumers.

Consumer-Side Configuration

On the consumer side, the fetch.message.max.bytes parameter determines the maximum message size that consumers can pull. If this parameter value is smaller than the actual message size, consumers will fail to retrieve the message content.

Topic-Level Configuration

Beyond broker-level global configurations, more granular control can be achieved at the topic level through the max.message.bytes parameter. This parameter is validated before message compression and defaults to inheriting the broker's message.max.bytes setting.

Configuration Parameter Relationships and Coordination

These configuration parameters exhibit tight interdependencies. Ideally, all parameters should maintain coordinated consistency:

The value of message.max.bytes should be less than or equal to replica.fetch.max.bytes to ensure that successfully received messages can be properly replicated. If replica.fetch.max.bytes is set too small, the system won't throw explicit exceptions, but message replication will fail silently, leading to data inconsistency issues.

Complete Configuration Example

Based on the above analysis, a comprehensive large message transmission configuration should include the following parameter settings:

Properties props = new Properties();
props.put("metadata.broker.list", BROKERS);
props.put("serializer.class", "kafka.serializer.StringEncoder");
props.put("request.required.acks", "1");
props.put("message.max.bytes", "41943040"); // 40MB
// Broker side requires synchronous setting of replica.fetch.max.bytes
// Consumer side requires setting fetch.message.max.bytes

Version Compatibility Considerations

It's important to note that different Kafka versions may have variations in configuration parameter naming. In Kafka 0.10 and newer versions, the producer-side max.request.size parameter replaces some functionality, and the consumer-side max.partition.fetch.bytes parameter also requires attention.

Best Practice Recommendations

When handling large messages, besides adjusting configuration parameters, consider the following best practices:

First, evaluate whether transmitting such large messages is truly necessary. If possible, consider splitting large messages into multiple smaller ones, or use external storage systems (like HDFS or S3) to store large files, only passing file references through Kafka.

Second, ensure all relevant configuration parameters are correctly set. A common mistake is adjusting only producer or broker configurations while neglecting corresponding adjustments on the consumer side.

Finally, before deploying to production environments, conduct thorough testing to verify that the complete lifecycle of large messages—from production through replication to consumption—functions properly.

Troubleshooting Guide

When encountering large message transmission issues, follow these troubleshooting steps:

Check that all relevant configuration parameters are correctly set, paying special attention to the often-overlooked replica.fetch.max.bytes parameter.

Verify that configuration parameter values are coordinated and consistent, ensuring no conflicts exist between parameters.

Use monitoring tools to observe message replication status, confirming successful synchronization across all replicas.

Examine system logs for any warnings or errors related to message size.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.