Kafka
JVM Settings
Best Practices
System Configuration
Technology Solutions

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:

  • Number of Partitions=(Max Expected ThroughputThroughput per Partition)\text{Number of Partitions} = \left(\frac{\text{Max Expected Throughput}}{\text{Throughput per Partition}}\right)

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 meet fetch.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.

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.
 
  -Xms6g -Xmx6g

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.
 
  -XX:+UseG1GC
  • GC logs: GC logging helps diagnose memory use and GC performance problems by writing GC statistics to the specified file.
 
  -Xlog:gc*:file=/var/log/kafka/gc.log:time,tags:filecount=10,filesize=100M

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:
 
  -XX:+UseCompressedOops -XX:+DisableExplicitGC -XX:+ExitOnOutOfMemoryError

These optimize pointer sizes, prevent explicit GC calls, and make the JVM fail fast on memory exhaustion.

Key parameters at a glance

ParameterRecommended SettingPurpose
Number of PartitionsThroughputPartition Throughput\frac{\text{Throughput}}{\text{Partition Throughput}}Increases throughput and parallel processing
Replication Factor3Ensures data durability and high availability
log.dirsMultiple disks as neededImproves disk I/O distribution and performance
num.network.threadsBased on the number of producers/consumersOptimizes network I/O processing
fetch.min.bytesHigher values as neededReduces load with larger batch size
fetch.max.wait.msAdjust based on consumer rate demandsBalances latency and throughput
acks'all'Ensures data is replicated for durability
compression.typegzip, snappy or lz4 (based on need)Improves producer throughput and bandwidth usage
JVM Heap Size6GB (-Xms6g -Xmx6g)Allocates sufficient memory for Kafka operations
Garbage CollectorG1 GC (-XX:+UseG1GC)Optimizes garbage collection for large heaps

Treat these as a production baseline and tune them to your workload.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design