Kafka Memory Management
Memory architecture and optimization for Apache Kafka brokers and clients.
Memory Architecture Overview
Section titled “Memory Architecture Overview”JVM Heap
Section titled “JVM Heap”Heap Components
Section titled “Heap Components”| Component | Description | Memory Impact |
|---|---|---|
| Request buffers | Incoming/outgoing request data | Proportional to connections |
| Metadata cache | Topic/partition metadata | Proportional to partitions |
| Index structures | In-memory index pointers | Proportional to partitions |
| Producer state | Idempotent producer tracking | Proportional to producers × partitions |
| Group coordinator | Consumer group state | Proportional to groups/members |
Heap Footprint Estimates (Repository Guidance)
Section titled “Heap Footprint Estimates (Repository Guidance)”| Area | Rule of Thumb |
|---|---|
| Broker heap per partition replica | ~1-2 MB |
| Controller metadata heap | ~5 GB for typical clusters |
Heap Sizing (Repository Guidance)
Section titled “Heap Sizing (Repository Guidance)”| Cluster Size | Partitions | Heap Size |
|---|---|---|
| Small | < 1,000 | 4-6 GB |
| Medium | 1,000-10,000 | 6-8 GB |
| Large | 10,000-50,000 | 8-12 GB |
| Very Large | > 50,000 | 12-16 GB |
JVM Configuration
Section titled “JVM Configuration”# Example JVM settings (tune per workload)export KAFKA_HEAP_OPTS="-Xms6g -Xmx6g"
# GC settings (example values)export KAFKA_JVM_PERFORMANCE_OPTS="-server \ -XX:+UseG1GC \ -XX:MaxGCPauseMillis=20 \ -XX:InitiatingHeapOccupancyPercent=35 \ -XX:+ExplicitGCInvokesConcurrent \ -XX:G1HeapRegionSize=16M \ -XX:MetaspaceSize=96m \ -XX:MinMetaspaceFreeRatio=50 \ -XX:MaxMetaspaceFreeRatio=80"OS Page Cache
Section titled “OS Page Cache”Page Cache Role
Section titled “Page Cache Role”Kafka relies heavily on the OS page cache for performance. The page cache stores recently accessed disk data in RAM.
Page Cache Sizing
Section titled “Page Cache Sizing”Rule of thumb: Reserve at least as much RAM for page cache as data you want to keep “hot” (typically last few hours of data).
Page Cache = Total RAM - JVM Heap - OS Overhead
Example: Total RAM: 64 GB JVM Heap: 6 GB OS/Other: 2 GB Page Cache: ~56 GB availableMonitoring Page Cache
Section titled “Monitoring Page Cache”# Check memory usagefree -g
# Check page cache usagecat /proc/meminfo | grep -E "Cached|Buffers|MemFree|MemTotal"
# Monitor disk I/O (high I/O = cache misses)iostat -x 1Buffer Pools
Section titled “Buffer Pools”Network Buffer Pool
Section titled “Network Buffer Pool”Kafka uses buffer pools to reduce garbage collection overhead.
# Broker network buffer sizingsocket.send.buffer.bytes=102400socket.receive.buffer.bytes=102400socket.request.max.bytes=104857600Default Memory/Buffer Limits (Kafka Defaults)
Section titled “Default Memory/Buffer Limits (Kafka Defaults)”| Component | Setting | Default |
|---|---|---|
| Broker | socket.send.buffer.bytes | 102400 |
| Broker | socket.receive.buffer.bytes | 102400 |
| Broker | socket.request.max.bytes | 104857600 |
| Producer | buffer.memory | 33554432 |
| Producer | batch.size | 16384 |
| Producer | linger.ms | 0 |
| Producer | max.block.ms | 60000 |
| Consumer | fetch.min.bytes | 1 |
| Consumer | fetch.max.bytes | 52428800 |
| Consumer | fetch.max.wait.ms | 500 |
| Consumer | max.partition.fetch.bytes | 1048576 |
| Topic | index.interval.bytes | 4096 |
Producer Buffer Pool
Section titled “Producer Buffer Pool”# Producer buffer configuration (example tuning)buffer.memory=33554432 # 32MB total buffer poolbatch.size=16384 # 16KB per batchlinger.ms=5 # Wait time for batchingConsumer Buffer Pool
Section titled “Consumer Buffer Pool”# Consumer fetch sizingfetch.min.bytes=1 # Minimum bytes to fetchfetch.max.bytes=52428800 # Maximum per fetch (50MB)max.partition.fetch.bytes=1048576 # Per partition (1MB)Memory Pressure Scenarios
Section titled “Memory Pressure Scenarios”Heap Exhaustion
Section titled “Heap Exhaustion”Symptoms:
OutOfMemoryError- GC taking > 10% of time
- Request latency spikes
Causes:
| Cause | Solution |
|---|---|
| Too many partitions | Reduce partitions or increase heap |
| Large metadata cache | Reduce topic count |
| Producer state buildup | Reduce idempotent producers |
| Memory leak | Update Kafka version |
Page Cache Exhaustion
Section titled “Page Cache Exhaustion”Symptoms:
- High disk read I/O
- Consumer latency increases
awaittime in iostat high
Causes:
| Cause | Solution |
|---|---|
| Heap too large | Reduce heap, leave more for cache |
| Too much data | Add more brokers |
| Random access patterns | Improve consumer patterns |
Garbage Collection
Section titled “Garbage Collection”G1GC Tuning
Section titled “G1GC Tuning”# Recommended G1GC settings-XX:+UseG1GC-XX:MaxGCPauseMillis=20-XX:InitiatingHeapOccupancyPercent=35-XX:G1HeapRegionSize=16M| Parameter | Purpose |
|---|---|
MaxGCPauseMillis | Target pause time (20ms recommended) |
InitiatingHeapOccupancyPercent | When to start concurrent GC |
G1HeapRegionSize | Region size (16M for larger heaps) |
GC Monitoring
Section titled “GC Monitoring”# Enable GC logging-Xlog:gc*:file=/var/log/kafka/gc.log:time,tags:filecount=10,filesize=100M
# Monitor GCjstat -gc <pid> 1000
# Analyze GC log# Look for: pause times, frequency, throughputGC Monitoring Targets (Repository Guidance)
Section titled “GC Monitoring Targets (Repository Guidance)”Use GC pause time, frequency, and throughput as trend indicators rather than fixed SLAs.
Direct Memory
Section titled “Direct Memory”Off-Heap Buffers
Section titled “Off-Heap Buffers”Kafka uses direct memory for network I/O operations.
# Configure direct memory limit (example)-XX:MaxDirectMemorySize=2gMemory-Mapped Files
Section titled “Memory-Mapped Files”Index files use memory-mapped I/O:
# These files are memory-mapped# .index - offset index# .timeindex - timestamp indexClient Memory Management
Section titled “Client Memory Management”Producer Memory
Section titled “Producer Memory”# Total memory for bufferingbuffer.memory=33554432
# Memory allocation behaviormax.block.ms=60000 # Block when buffer fullMemory calculation:
Required memory = buffer.memory + (partitions × batch.size overhead) + compression buffersConsumer Memory
Section titled “Consumer Memory”# Fetch sizingfetch.max.bytes=52428800max.poll.records=500Memory calculation:
Required memory = fetch.max.bytes + deserialization buffers + record processing buffersMemory Tuning Checklist
Section titled “Memory Tuning Checklist”Broker
Section titled “Broker”- Set heap size appropriately (6-12GB typical)
- Leave sufficient RAM for page cache
- Configure G1GC with appropriate pause target
- Monitor GC pause times and frequency
- Watch for page cache evictions
Producer
Section titled “Producer”- Size
buffer.memoryfor throughput needs - Set appropriate
batch.size - Monitor
buffer-available-bytesmetric
Consumer
Section titled “Consumer”- Configure fetch sizes appropriately
- Set
max.poll.recordsfor processing capacity - Monitor memory usage in application
Related Documentation
Section titled “Related Documentation”- Architecture Overview - System architecture
- Performance Internals - Performance tuning
- Brokers - Broker configuration
- Operations - Operational procedures