发送大消息报 RecordTooLargeException 时,网上的建议是同时调 max.request.size、message.max.bytes、replica.fetch.max.bytes、fetch.max.bytes……一大串。这篇用 Kafka 4.3.1 把每个限制单独测一遍:两个 RecordTooLargeException 的原文怎么区分、各自的精确边界、能不能不重启放开、压缩怎么影响、消费端到底要不要改。
全部默认(max.request.size=1048576):
最大能发送的 value 长度 = 1048489 字节
客户端拒绝时的原文:RecordTooLargeException: The message is 1048577 bytes when serialized which is larger than 1048576, which is the value of the max.request.size configuration.
topic t1 的 max.message.bytes(服务端查询):max.message.bytes=1048588 sensitive=false synonyms={DEFAULT_CONFIG:message.max.bytes=1048588}客户端 max.request.size 先拦,服务端 max.message.bytes 在后。
客户端 max.request.size=5242880,broker 与 topic 默认:
最大能发送的 value 长度 = 1048516 字节
服务端拒绝时的原文:RecordTooLargeException: The request included a message larger than the max message size the server will accept.含 max.request.size 的是客户端,含 server will accept 的是服务端。
Completed updating config for topic t1.
topic max.message.bytes=3145728(3 MiB),客户端 max.request.size=5242880:
最大能发送的 value 长度 = 3145654 字节
服务端拒绝时的原文:RecordTooLargeException: The request included a message larger than the max message size the server will accept.
Completed updating config for topic t1.
对照:另一个没改的 topic t2,客户端 max.request.size=5242880:
最大能发送的 value 长度 = 1048516 字节
服务端拒绝时的原文:RecordTooLargeException: The request included a message larger than the max message size the server will accept.kafka-configs 改 topic 的 max.message.bytes,不重启,只影响该 topic。
gzip + 全是字母 a,max.request.size=默认:
最大能发送的 value 长度 = 1048489 字节
客户端拒绝时的原文:RecordTooLargeException: The message is 1048577 bytes when serialized which is larger than 1048576, which is the value of the max.request.size configuration.
gzip + 全是字母 a,max.request.size=8388608:
最大能发送的 value 长度 = 8388520 字节
客户端拒绝时的原文:RecordTooLargeException: The message is 8388609 bytes when serialized which is larger than 8388608, which is the value of the max.request.size configuration.
不压缩 + 全是字母 a,max.request.size=8388608(对照):
最大能发送的 value 长度 = 1048516 字节
服务端拒绝时的原文:RecordTooLargeException: The request included a message larger than the max message size the server will accept.客户端按压缩前检查,服务端按压缩后批大小检查。
已发送 1 条 3145728 字节和 1 条 100 字节的消息 max.partition.fetch.bytes = fetch.max.bytes = 1000 -> 读到的消息长度:[3145728, 100] max.partition.fetch.bytes = fetch.max.bytes = 1048576 -> 读到的消息长度:[3145728, 100]
fetch 限制不是硬上限,第一批大消息仍会返回。