Kafka Best Practices + how to set recommended setting for JVM
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 highly efficient and scalable distributed event streaming platform capable of handling trillions of events a day. It is designed to handle large volumes of data and offers high throughput and low latency processing. The backbone of Kafka’s performance lies in its proper configuration and tuning. Below are some key best practices for managing Kafka along with recommendations for JVM settings to ensure optimal performance.
Kafka Best Practices
1. Topic Design and Partitioning
Choosing the right number of partitions per topic is crucial as it directly affects the scalability, parallelism, and performance. More partitions allow greater parallelism but can increase overhead in terms of metadata and replication. A general formula to start determining the number of partitions is based on the expected throughput:
2. Replication and Durability
Replication ensures high availability and durability of data. A common practice is to use a replication factor of 3, meaning each message is stored on three different brokers. This provides a good balance between availability and resource usage.
3. Broker Configuration
Configuration of each Kafka broker can be tuned for optimization:
log.dirs: Determine on which disk or disks Kafka will store data. Distributing this across multiple disks can improve performance.num.network.threads: Sets the number of threads that the server uses for processing network requests. This should correlate with the number of producers and consumers.
4. Consumer Configurations
For consumers, tuning fetch sizes and enabling client-side batching can improve both consumer throughput and reduce load on Kafka brokers.
fetch.min.bytes: Controls the minimum amount of data that a consumer will receive at a fetch request. Increasing this can reduce the number of fetch calls.fetch.max.wait.ms: Maximum time the server will block before answering the fetch request if there isn't sufficient data to meetfetch.min.bytes.
5. Producer Configurations
For producers:
acks: Controls the number of acknowledgments the producer requires from brokers. Setting this to 'all' ensures full replication for durability.compression.type: Compressing messages can significantly improve performance, especially for high-throughput producers. Common types include gzip, snappy, and lz4.
Recommended JVM Settings for Kafka
Kafka runs on the Java platform, and the performance can greatly depend on the proper configuration of the Java Virtual Machine (JVM). Below are recommended settings tailored specifically for Kafka's unique requirements:
- Heap Size: The JVM heap size should be configured based on your Kafka broker's requirements and available system memory. A good starting point for Kafka is between 4GB and 8GB, as Kafka relies heavily on the Java heap for the brokers.
This sets the JVM’s initial and maximum heap size to 6 GB.
- Garbage Collection (GC): Kafka works well with the Garbage-First Collector (G1 GC).
This enables the G1 GC, designed to better support heap sizes exceeding 4 GB.
- GC Logs: Enabling GC logging can help diagnose issues with memory use and GC performance.
This logs GC stats into the specified file which helps in monitoring and debugging.
- JVM Performance: Some additional flags to optimize performance include:
These optimize pointer sizes, prevent explicit GC calls, and ensure JVM fails fast on memory exhaustion.
Summary Table: Key Kafka Configuration Parameters
| Parameter | Recommended Setting | Purpose |
| Number of Partitions | Increases throughput and parallel processing | |
| Replication Factor | 3 | Ensures data durability and high availability |
log.dirs | Multiple disks as needed | Improves disk I/O distribution and performance |
num.network.threads | Based on the number of producers/consumers | Optimizes network I/O processing |
fetch.min.bytes | Higher values as needed | Reduces load with larger batch size |
fetch.max.wait.ms | Adjust based on consumer rate demands | Balances latency and throughput |
acks | 'all' | Ensures data is replicated for durability |
compression.type | gzip, snappy or lz4 (based on need) | Improves producer throughput and bandwidth usage |
| JVM Heap Size | 6GB (-Xms6g -Xmx6g) | Allocates sufficient memory for Kafka operations |
| Garbage Collector | G1 GC (-XX:+UseG1GC) | Optimizes garbage collection for large heaps |
These practices and settings provide a strong foundation for running Kafka in a production environment, ensuring high performance, fault tolerance, and efficient data processing. Tailoring these settings to specific needs can further enhance Kafka's effectiveness in dealing with large-scale message streaming tasks.

