Kafka
Message Sizing
Data Management
Programming
Configuration Settings

How do I set the size of messages in Kafka?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Apache Kafka, a distributed event streaming platform, uses a variety of configuration settings to adjust the performance and behavior of its brokers, producers, and consumers. One significant aspect of tuning Kafka's performance is setting the size of messages that can be produced and fetched by clients.

Understanding Message Size in Kafka

In Kafka, messages are the records that are produced to and consumed from topics. Topics are partitioned, and multiple replicas of these partitions are maintained for fault tolerance. The size of the messages can greatly impact the throughput and stability of the Kafka cluster. Larger messages mean fewer messages can be sent in a single batch and require more memory and network bandwidth, which can potentially increase latency. Conversely, smaller messages increase overhead due to the per-message overhead in Kafka’s network protocol.

Configuring Message Size Limits

1. message.max.bytes

This is a broker-level configuration that defines the largest record batch size allowed by the broker. By default, message.max.bytes is set to 1,048,576 bytes (1 MiB). If a record batch exceeds this size, the broker will reject it.

properties
message.max.bytes=1048576

2. max.message.bytes

For the producer, max.message.bytes controls the maximum size of a message that can be sent to a broker. It also acts as a limit for the maximum record batch size. This value must be set not higher than the broker's message.max.bytes.

properties
max.message.bytes=1000012

3. fetch.message.max.bytes

This setting is used on the consumer side, specifying the maximum number of bytes the server should return on a fetch request. This limits the amount of data a broker will return per fetch request and should be managed according to consumer capacity to handle large data.

properties
fetch.message.max.bytes=1048576

Practical Example

Consider a scenario where you need to send messages that are approximately 900 KB in size. You would configure your Kafka cluster as follows:

  • Set message.max.bytes on the broker to slightly above 900 KB, say 920 KB:
properties
  message.max.bytes=942080
  • Set max.message.bytes in the producer configuration to match or be less than 920 KB:
properties
  max.message.bytes=942080
  • Adjust the fetch.message.max.bytes in the consumer to handle the expected message size efficiently:
properties
  fetch.message.max.bytes=942080

Best Practices and Considerations

  • Monitor and Adjust: Regularly monitor your Kafka system's performance and adjust the size settings as necessary based on observed message patterns and performance metrics.
  • Compatibility: Ensure that the producer’s max.message.bytes is less than or equal to message.max.bytes set on the broker.
  • Network Impact: Consider the impact of increased message size on network throughput and latency, especially in distributed environments.

Summary Table

Configuration SettingDefault Size (Bytes)DescriptionApplicable Component
message.max.bytes1,048,576 (1 MiB)Maximum record batch size broker will accept.Broker
max.message.bytes1,000,000 (~1 MiB)Maximum message size producer can send.Producer
fetch.message.max.bytes1,048,576 (1 MiB)Maximum bytes in a fetch request for consumers.Consumer

In summary, managing Kafka message sizes is critical for ensuring stable and efficient operations in a Kafka ecosystem. Proper tuning of message size settings helps in balancing throughput, latency, and resource usage across the Kafka cluster.


Course illustration
Course illustration

All Rights Reserved.