Kafka best practices and recommended JVM settings
Codemia Knowledge Hub ·
For the JVM, a good starting point for a Kafka broker is a fixed heap of around 6 GB (-Xms6g -Xmx6g) with the G1 garbage collector (-XX:+UseG1GC) and GC logging turned on. Beyond the JVM, Kafka's performance depends on partition count, replication, and broker, consumer and producer settings, covered below.
Topic design and partitioning
The number of partitions per topic directly affects scalability, parallelism and performance. More partitions allow more parallelism but add metadata and replication overhead. A starting formula based on expected throughput:
Replication and durability
Replication provides high availability and durability. A common choice is a replication factor of 3, so each message is stored on three brokers, which balances availability against resource usage.
Broker configuration
log.dirs: the disk or disks where Kafka stores data. Spreading it across multiple disks can improve performance.num.network.threads: the number of threads the server uses to process network requests. It should correlate with the number of producers and consumers.
Consumer configuration
Tuning fetch sizes and client-side batching can raise consumer throughput and reduce load on brokers.
fetch.min.bytes: the minimum amount of data a consumer receives per fetch request. Increasing it reduces the number of fetch calls.fetch.max.wait.ms: the maximum time the server blocks before answering a fetch request when there is not enough data to meetfetch.min.bytes.
Producer configuration
acks: the number of acknowledgments the producer requires from brokers. Setting it to 'all' ensures full replication for durability.compression.type: compressing messages can significantly improve performance for high-throughput producers. Common types are gzip, snappy and lz4.
Recommended JVM settings for Kafka brokers
Kafka runs on the JVM, so broker performance depends heavily on JVM configuration.
- Heap size: size the heap based on the broker's requirements and available system memory. A good starting point is between 4 GB and 8 GB. Kafka uses heap space carefully and relies on the operating system's file system cache for message data, so leave the rest of the machine's memory to that cache rather than giving it to the heap.
This sets the initial and maximum heap size to 6 GB.
- Garbage collection: Kafka works well with the Garbage-First collector (G1 GC), which is designed to handle heaps larger than 4 GB better.
- GC logs: GC logging helps diagnose memory use and GC performance problems by writing GC statistics to the specified file.
This is the unified logging syntax of JDK 9 and later. The older -XX:+PrintGCDetails -XX:+PrintGCDateStamps flags were removed, and JDK 17 refuses to start with them.
- Other performance flags:
These optimize pointer sizes, prevent explicit GC calls, and make the JVM fail fast on memory exhaustion.
Key parameters at a glance
| 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 |
Treat these as a production baseline and tune them to your workload.
Related reading
- kafka binding to ipv6 port even though ipv4 address specified in config
- Kafka bootstrap-servers vs zookeeper in kafka-console-consumer
- kafka bootstrap.servers as DNS A-Record with multiple IPs
- Kafka broker auto scaling
- Kafka broker JVM settings below 1GB
- Kafka ClassCastException class org.apache.avro.generic.GenericData$Record cannot be cast to class
- Kafka broker constantly ISR shrinking and expanding?
- Kafka Broker doesn't find cluster id and creates new one after docker restart
System design interview problems on this topic
- Design a Distributed Messaging System: solution and walkthrough
- Design a Log Collection and Analysis System: solution and walkthrough
- Design an Ad Click Aggregation System: solution and walkthrough
- Design a Webhook Notification Service: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.