Skip to content

Kafka Design: Page Cache & Performance

lyx2000 edited this page Apr 23, 2025 · 1 revision

Overview

Apache Kafka's exceptional performance and reliability as a distributed event streaming platform are largely attributed to its intelligent use of the operating system's page cache. This core design decision enables Kafka to achieve high throughput and low latency without requiring extensive application-level caching. This blog explores how Kafka leverages the page cache, its performance implications, configuration best practices, and common challenges.

Although Kafka Page Cache brings many advantages to Kafka, it can cause serious performance issues due to Page Cache pollution when disk reads occur. If you are looking for a solution to this problem, you can read: How AutoMQ addresses the disk read side effects in Apache Kafka

Understanding the Page Cache in Kafka's Architecture

The page cache is a transparent buffer maintained by the operating system that keeps recently accessed file data in memory. Kafka's architecture is specifically designed to take advantage of this system feature, rather than implementing its own complex caching mechanisms.

Zero Copy Principle

At its core, Kafka operates on the "Zero Copy" principle, which is fundamental to its performance[8]. This means:

  1. Kafka transfers data in byte format without inspecting or modifying it

  2. No data verification occurs at the cluster level

  3. Data moves directly from disk to network (or vice versa) without unnecessary copying

This approach provides significant performance improvements compared to traditional methods that involve multiple data copies between channels[8]. The zero-copy mechanism allows Kafka to avoid copying data into user space, reducing CPU overhead and improving throughput[10].

How Kafka Utilizes the Page Cache

Kafka brokers make heavy use of the operating system's page cache to maintain performance[11]. When data is written to Kafka:

  1. Data is written to the page cache first

  2. The OS eventually flushes these pages to disk asynchronously

  3. Kafka doesn't explicitly issue commands to ensure messages are persisted (sync)

  4. It relies on the OS to efficiently manage when data is written to physical storage

For reads, Kafka similarly leverages the page cache:

  1. Consumer requests are served from the page cache when possible

  2. The OS handles prefetching data through techniques like readahead

  3. Sequential read patterns are automatically detected and optimized by the kernel

This approach differs from many databases that implement application-level caches. Kafka's reliance on the page cache is particularly effective because messaging workloads typically involve sequential reads and writes, which the Linux kernel has been optimized to handle efficiently[10].

Performance Implications of Page Cache Usage

Kafka's page cache utilization directly impacts its performance metrics, particularly throughput and latency.

Throughput Benefits

In benchmark tests, Kafka has demonstrated impressive throughput capabilities:

  • Achieving 200K messages/second or 200 MB/second on appropriate hardware[10]

  • Performance scaling with increased disk throughput (from HDD to SSD to NVMe)[13]

Table 1: Kafka performance scaling with storage technology (benchmark with 600M 100-byte messages)[13]

Latency Benefits

The page cache significantly reduces latency for Kafka operations:

  • Reads served from the cache have consistently low latency

  • When consumers keep up with producers, almost all reads come from the cache rather than disk

  • End-to-end latency can be kept in the millisecond range even at high throughput[10]

Configuring Systems for Optimal Page Cache Usage

To maximize Kafka's performance, proper system configuration is essential, particularly related to memory management and virtual memory behavior.

Critical Linux Kernel Parameters

These parameters can be set persistently by adding them to /etc/sysctl.conf [11].

Memory Allocation Considerations

For optimal Kafka performance:

  1. Dedicated Resources : Run Kafka standalone on its own VM or physical server so available RAM is primarily used for page cache[12]

  2. JVM Heap Size : Configure the JVM heap size appropriately

    • Too large: Reduces memory available for page cache

    • Too small: Increases garbage collection overhead

  3. Memory Mapping : Each log segment requires index files that need memory map areas

    • Production deployments with many partitions may exceed default OS limits

    • Increase vm.max_map_count accordingly[11]

Common Challenges and Solutions

Running Kafka in Kubernetes

Kubernetes environments present unique challenges for Kafka's page cache utilization:

  1. Shared Resources : In Kubernetes, the page cache is shared between multiple applications, meaning the amount of data Kafka can cache is never guaranteed[14]

  2. Resource Competition : When other applications run on the same node as Kafka, they can evict Kafka's data from the page cache

  3. Pod Isolation : Kubernetes cgroups limit memory but don't provide isolation for page cache usage

Solutions:

  • Use node affinity/anti-affinity rules to isolate Kafka pods

  • Consider dedicated nodes for Kafka brokers

  • Implement resource limits that account for both heap and page cache needs

Interference from Other Applications

When Kafka coexists with other disk-intensive applications, performance can degrade due to page cache competition:

  1. Cassandra Co-location Issue : Running Cassandra alongside Kafka can increase page faults in Kafka pods even when memory resources appear available[7]

  2. Increased Disk I/O : More page faults lead to more writes to disk, hampering sequential I/O benefits and potentially depleting storage burst capacity[7]

Solutions:

  • Isolate Kafka from other disk-intensive applications

  • Consider application-specific tuning (e.g., Cassandra uses fadvise to optimize its page cache footprint)[7]

  • For AWS EBS or similar volumes, monitor burst balance carefully

Best Practices for Kafka and Page Cache

Hardware Recommendations

  1. Storage Selection :

    • SSDs significantly outperform HDDs for Kafka workloads

    • NVMe drives provide even greater performance benefits[13]

    • Higher drive throughput directly translates to higher sustainable message rates

  2. Memory Sizing :

    • Allocate sufficient RAM for both JVM heap and page cache

    • General guideline: 32GB+ RAM for production Kafka brokers

    • More memory allows more data to remain cached

  3. Network Configuration :

    • Ensure network bandwidth isn't a bottleneck

    • In test environments with 10Gb NICs, storage was typically the limiting factor[13]

Operational Recommendations

  1. Monitoring Page Cache Efficiency :

    • Track cache hit ratios

    • Monitor disk I/O patterns

    • Watch for unexpected page faults

  2. Scaling Considerations :

    • Scale horizontally when individual broker performance reaches limits

    • Add brokers when page cache pressure becomes too high

    • Consider partition reassignment to balance load across brokers

  3. Maintenance Operations :

    • Schedule maintenance operations during low-traffic periods

    • Be aware that operations like partition reassignment can flush cache contents

    • Allow time for page cache to "warm up" after maintenance

Swap Space Management

Despite common advice to disable swap entirely for Kafka, some research suggests that maintaining a small amount of swap can be beneficial:

  • Setting vm.swappiness=1 (not 0) allows the kernel to swap out truly inactive pages

  • This can free up more memory for the page cache

  • Only completely unused applications or libraries get swapped, not active Kafka data[1]

Alternative Approaches: Beyond Page Cache

Redpanda's Custom Memory Management

Redpanda, a Kafka-compatible streaming platform, uses a different approach:

  1. Allocates RAM specifically for the Redpanda process instead of relying on the page cache[14]

  2. Implements hyper-efficient caching with buffers adjusted according to hardware performance[14]

  3. Uses Direct Memory Access (DMA) and aligns cache with the filesystem[14]

  4. Shares cache across all open files, allowing heavily used partitions to access additional buffer space during spikes[14]

This approach potentially offers advantages in containerized environments where page cache behavior is less predictable.

In-Memory Solutions

For specific use cases where extreme performance is required:

  • Some specialized systems use memory-mapped files to keep latency low

  • However, these require careful consideration of durability guarantees

  • May require fsync() calls to ensure data is persisted[3]

Conclusion

Kafka's intelligent use of the Linux page cache is a key architectural decision that enables its high performance and efficiency. By leveraging the operating system's existing mechanisms rather than implementing complex application-level caching, Kafka achieves impressive throughput and latency characteristics while maintaining reliability.

Proper configuration of both the operating system and Kafka itself is essential to maximize the benefits of page cache usage. This includes tuning Linux kernel parameters, allocating appropriate resources, and implementing operational best practices.

As workloads grow and environments evolve, particularly with the rise of containerization, understanding Kafka's page cache utilization becomes increasingly important. Whether optimizing existing Kafka deployments or considering alternative platforms like Redpanda, knowledge of how these systems interact with memory and storage is critical for achieving optimal performance.

If you find this content helpful, you might also be interested in our product AutoMQ. AutoMQ is a cloud-native alternative to Kafka by decoupling durability to S3 and EBS. 10x Cost-Effective. No Cross-AZ Traffic Cost. Autoscale in seconds. Single-digit ms latency. AutoMQ now is source code available on github. Big Companies Worldwide are Using AutoMQ. Check the following case studies to learn more:

References:

  1. Linux Performance: Almost Always Add Swap Space

  2. In-Memory Cache Middleware for PostgreSQL

  3. Fsync Memory Mapped Files: Speed vs Accuracy

  4. Help: Realtime Searchable Table Handling Large Data

  5. Explain Why Kubernetes and Containers Are Better

  6. How We've Saved 98% in Cloud Costs

  7. How to Manage Page Cache Resources When Running Kafka in Kubernetes

  8. What is the Schema Registry and Why Do You Need to Use It

  9. The mmap Pattern

  10. Kafka Performance Guide

  11. Kafka Performance Tuning Guide

  12. Kafka Consumers: Reading from Disk vs Cache

  13. Improve Apache Kafka Performance with Flash Storage

  14. What Makes Redpanda Fast

  15. Memory Usage Increases in Service While Reading

  16. Strategies for Caching and Improving Performance

  17. You Don't Need a Dedicated Cache Service

  18. The Case for Shared Storage

  19. Can You Query for All Events in a Topic Similar

  20. Searching 1TB/sec: Systems Engineering Before Optimization

  21. Does Kafka Make Sense for Real Time Stock Quote

  22. 6 Event-Driven Architecture Patterns Breakdown

  23. Analysing up to 100k Messages per Second

  24. Which Protocol is More Complex to Implement

  25. Reimplemented Go Service in Rust: Throughput

  26. Kafka Streams: Track Website Activity Users at a Time

  27. Dozer: The Future of Data APIs

  28. Event-Driven Architectures vs Request Response

  29. File System Design: Constant Time

  30. Apache Kafka Best Practices

  31. Why Kafka Index Files Use Memory Mapped Files but Log Files Don't

  32. Kafka Post-Deployment Guide

  33. Why Kafka Has High Throughput

  34. Redpanda vs Kafka Performance Benchmark

  35. Page Cache in Kafka

  36. Kafka Design Guide

  37. What Makes Kafka So Performant

  38. Kafka Deployment Guide

  39. Kafka Consumer Important Settings: Poll and Internal Threads Behavior

  40. Write Caching Performance Benchmark

  41. Kafka Performance Tuning Guide

  42. Understanding Kafka System Design

  43. Kafka to Fetch Data from Another Microservices

  44. HSR Characters Discussion

  45. Feature Complete Kafka Client Written in Go

  46. Apache Kafka log.dirs Best Practices Question

  47. Favorite and Least Favorite Java Features

  48. Apache Flink Subreddit

  49. The Egregious Costs of Cloud with Kafka

  50. ETL in .NET

  51. Database or Query Engine for Heavy Read

  52. Configuring Conduktor

  53. Kafka Design Guide

  54. Optimizing Kafka Performance: Advanced Tuning Tips for High Throughput

  55. Conduktor Gateway 2.6.0 Changelog

  56. Kafka Performance Tuning Guide

  57. Best Practices for Gateway Cases

  58. How to Clear the Cache of Your Browser

  59. Kafka Deployment Guide

  60. Conduktor Gateway 2.1.6 Changelog

  61. Best Practices for Right-Sizing Your Apache Kafka Clusters

  62. Conduktor Changelog

  63. Java Consumer Rebalance Listener

  64. Advanced Kafka Producers

  65. Why is Kafka So Fast?

  66. Choosing Replication Factor and Partitions Count

  67. Kafka Options Explorer

  68. Consumer Incremental Rebalance and Static Group Membership

  69. Kafka KRaft Mode

  70. Gateway Encryption Configuration

  71. Conduktor Console Reference

  72. What is Your Opinion of Google?

  73. Apache Kafka Important Designs

  74. Kafka Streams Memory Management

  75. Kafka Performance Optimization

  76. Do You Skip the Service Layer?

  77. Specific Cache Problem in Rails

  78. How to Handle Event State in Event-Driven Architecture

  79. When Should You Not Use Kafka?

  80. Tuning Kafka for High Performance and Scalability

  81. Kafka on Kubernetes: Deploy Best Practices

  82. Gain Visibility into Your Amazon MSK Cluster with Conduktor

  83. Top 5 Tips for Building Robust Kafka Applications

  84. Kafka Security Manager

  85. Conduktor Metric Reference

  86. Does Kafka Lose Messages?

  87. Sled: A Modern Embedded Database

  88. Rustaceans Q&A - 13/2023

  89. Rustaceans Q&A - 16/2023

  90. Does mmap Directly Access the Page Cache?

  91. Tuning Default mmap Advice in Apache Pinot

  92. Understanding mmap and Page Cache in Kafka

  93. How AutoMQ Addresses Disk Read Side Effects in Kafka

AutoMQ Wiki Key Pages

What is automq

Getting started

Architecture

Deployment

Migration

Observability

Integrations

Releases

Benchmarks

Reference

Articles

Clone this wiki locally