You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

There have been some questions and discussions about how to efficiently let users to configure their memory usage in Kafka Streams since 0.10.0 release, and how that will affect our current development plans regarding caching, buffering, and state store management, etc. In this page we summarize the memory usage background in Kafka Streams as of 0.10.0, and discuss what would be the "end goal" for Kafka Stream's memory management. This is not used as an implementation design and development plan for memory management, but rather as a guidance for related feature developments that may be correlating to the memory usage.

 

Background

There are a few modules inside Kafka Streams that allocate memory during the runtime:

  1. Kafka Producer: each thread of a Kafka Streams instance maintains a producer client. The client itself maintains buffer for batching records that are going to be sent to Kafka. This is completely controllable by producer's
    buffer.memory config.
     
  2. Kafka Consumer: each thread of a Kafka Streams instance maintains two consumer client, one for normal data fetching and one for state store replication and restoration only. Each client maintains buffer fetched messages before they are returned to user from the poll call. Today it is not controllable yet, but in the near future we are going to add similar memory bound controls like we have in producers:  Unable to render Jira issues macro, execution error. .

    * Both producer and consumer also has separate TCP send / receive buffers that are not counted as the buffering memory, which are controlled by the
     send.buffer.bytes / receive.buffer.bytes
      configs; these are usually small (100K) and hence neglected most of the time.

  3. Triggering based Caches: as summarized in KIP-63, we will be adding a cache for each of the aggregation and KTable.to operators, and we are adding a StreamsConfig to bound the total number of bytes used for all caches. BUT we are caching them as deserialized objects in order to avoid serialization costs.

  4. Deserialized Objects Buffering: within each thread's running loop, after the records are returned in raw bytes from consumer.poll, the thread will deserialize each one of them into typed objects and buffer them, and process them one record at-a-time. This is mainly used for extracting the timestamps (which may be in the message's value payload) and reason about streams time to determine which stream to process next (i.e. synchronizing streams based on their current timestamps, see this for details).
     
  5. Persistent State Store Buffering: currently we are using RocksDB by default as persistent state stores for stateful operations such as aggregation / joins, and RocksDB have their own buffering and caching mechanism which allocate memory both off-heap and on-heap. And RocksDB has its own configs that controls their sizes (we plan to expose these configs separately from StreamsConfig: Unable to render Jira issues macro, execution error. ), to name a few:
    • block_cache_size: amount of cache in bytes that will be used by RocksDB. NOTE this is off-heap.
    • write_buffer_size: the size of a single memtable in RocksDB.
    • max_write_buffer_number: the maximum number of memtables, both active and immutable. 

      So a rough calculation about the amount of memory: block_cache_size + write_buffer_size * max_write_buffer_number.

 

Memory Management: The End Goal

In the ideal world, Kafka Streams should provide very simple configuration for its memory management. More concretely, for example, users should be able to just specifying a couple of configs specifying the memory limit on the Producer, Consumer and Streams client as sth. like:

  • producer.memory.bytes (P), covering 1) above.
  • consumer.memory.bytes (C), covering 2) above.
  • streams.memory.bytes (S), covering 1) + 2) + 3) + 4) + 5) above. Hence the streams library's pure usage (case 3, 4, and 5 above) will be S - P - C.

 

With P and C having some reasonable default values, most of the time users only need to specify S above while keeping in mind that it needs to be at least P + C, and the library is then guaranteed to use less memory than S. And hence if users start their Streams application in a container with bounded memory usage as X, they know that their coded application can use up to the amount of memory allowed by the container minus allocable library usage, i.e. X - S. And even under task migration scenarios upon failures, or rebalancing, the immigrated tasks which will then allocate memory for its own caching and state stores, etc, will not suddenly increase the libraries memory usage since its total is still bounded, and hence not causing OOM (note that in case of task migration due to one instance failure, without memory bounding it may cause cascading OOMs, which is really bad user experience).

 

 

 

 

 

 

 

 

 

 

 

 

 

  • No labels