Max number of messages that can be stored in a Kafka topic partition?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka is a robust and highly-efficient distributed event streaming platform used by thousands of companies for high-performance data pipelines, streaming analytics, data integration, and mission-critical applications. Topics, a fundamental part of Kafka architecture, are split into partitions for scalability, each being an ordered and immutable sequence of records that is continually appended to, a commit log.
Maximum Messages per Partition
In Kafka, there is no strict upper limit on the number of messages that can be stored in a partition. Instead, the maximum number of messages per partition is determined by several factors including:
- Log Segment File Size: Each partition in Kafka is divided into segments. Each segment's size is determined by the configuration
log.segment.bytes(defaulting to 1 GB) orlog.segment.mswhich controls the time span of a segment before a new segment is created. - Topic Retention Policy: Kafka allows specifying how data should be retained through ‘retention policies’. Common retention settings include
log.retention.hours,log.retention.bytes, andlog.retention.messagesfor time-based, size-based, and number-based retention, respectively. - Broker Storage Capacity: The physical storage capacity available on the Kafka servers (brokers) also limits the number of messages. If the disk is full, no new messages can be added until space is freed either by deleting old segments as per the retention policy or adding more storage.
- Index Size: Kafka maintains indexes for each partition to ensure quick lookup. There's a size limit for these index files, and when reached, no more messages can be written to that segment.
Calculation Example
Let's calculate hypothetically:
- Assume size per message averages to 1 KB.
- Log file segment size is set to 1 GB.
This configuration means that each partition can hold approximately messages per segment (assuming no overhead). If your topic has multiple segments per partition, the number of messages can grow significantly, bounded by storage capacity and retention policies.
Practical Considerations
Other than the theoretical limits, practical considerations include performance issues:
- Handling Large Partitions: As a partition grows, issues like longer recovery times in case of broker failures or longer times for initial loading might arise.
- Broker Resource Usage: High number of large partitions might lead to extensive CPU, memory, and I/O usage affecting overall system performance.
Best Practices
- Optimal Partition Sizing: Balance the partition sizes against anticipated throughput and data retention needs.
- Monitoring and Management: Use Kafka's monitoring tools to track partition size and performance.
Summary Table
Here's a quick reference to key factors affecting maximum messages in a Kafka partition:
| Factor | Description | Influence on Message Count |
| Log Segment Size | Maximum size in bytes of a log segment. | Direct: More bytes, more messages. |
| Retention Policy | Defines how data is retained before being deleted. | Indirect: Impacts how long messages stay, affecting storage. |
| Broker Storage | Total storage available on a Kafka broker. | Direct: More storage, more potential messages. |
| Message Size | Average size of a single message. | Direct: Smaller messages, more messages per segment. |
Conclusion
While there's no set limit to the number of messages a Kafka partition can handle, effective management depends on understanding and configuring based on factors like segment sizing, retention policies, and overall broker capacity. Planning with performance in mind and adhering to best practices ensures Kafka operates efficiently within the desired scale.

