Cache types

Neo4j maintains two separate caches for nodes and relationships that can be configured to have a variable upper bound in terms of size, which can be configured by the max_node_cache_size and max_relationship_cache_size options of the database. The implementations of caching techniques in Neo4j can be explored in the org.neo4j.kernel.impl.cache package. Neo4j provides four different types of caches built into the default package:

Cache type

Property

NoCache

This is degenerate and stores nothing

LruCache

This utilizes LinkedHashMap of the java.util package along with a custom method to remove the oldest entry when deallocating space—removeEldestEntry(); this makes it an adaptable LRU cache

SoftLruCache

An LruCache using soft values to store references and queues for garbage-collected reference instances

WeakLruCache

An LruCache using hard values to store references and queues for garbage-collected reference instances

Note

Weak and soft references are the basis of the behavior of Java Garbage Collector (GC) depending on the references of an object. If the GC finds that an object is softly reachable, it might clear these references in an atomic manner to free up memory. However, if the GC finds an object to be weakly reachable, then it will clear all the weak references automatically and atomically.

The soft and weak LruCaches extend org.neo4j.kernel.impl.cache.ReferenceCache, including a pollClearedValues() method to get rid of dead references from the hashmap. If you need to explore the code, the cache contents are managed by the NodeManager class, while AdaptiveCacheManager handles memory consumptions and is configurable.

AdaptiveCacheManager

AdaptiveCacheManager manages the cache sets and their configurations along with adapting them to the size changes. A worker thread is spawned which, on every adaptive_cache_worker_sleep_time milliseconds (which is 3000 by default), wakes to re-adjust the size of the caches. In the case of ReferenceCaches, a call to the pollClearedValues() method is initiated. In the case of LruCache, the adaptSize() method is invoked on every cache and the size is re-adjusted depending upon the JVM memory statistics passed to the resize() method that removes elements until the new size is achieved.

The caching mechanism in Neo4j is mainly used to cache nodes and relationships implementations so that they can be retrieved from persistent storage and are completely abstracted by NodeManager.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset