Cache Eviction to Disk in Cacheonix

Eviction to disk in Cacheonix is a process of storing values evicted from the cache on a disk instead of discarding them completely. The eviction to disk allows storing bigger amounts of data in a cache at the added expense of disk storage access and maintenance. This article gives a high-level overview of working of the eviction to disk in Cacheonix and provides recommendations for using it.

There are two main approaches to the eviction to disk. The first approach is to completely evict a cache element and store the element key and the value on the disk. The advantage of this approach is a predictable heap allocation. Once the element is evicted, a the memory needed for maintaining the element is completely released. The disadvantage of this approach is that for a cache miss there is always a read operation to the hash map and one or more the read operations to access the the slow disk storage. This read operation has to be made even if the key is not present in the disk storage thus making the cache misses expensive. Also, cache hits for the elements on disk always require a read operation to the hash map and one or more read operations to access the slow disk storage because the key is not present in the hash map.

The second approach is to keep the keys in the cache's hash table and to store on disk the element value only. Cacheonix uses this approach. The main advantage of this approach is two-fold. First, the cost of the cache miss is low and is the same as the cost of the miss for a memory-only cache. Second, Cacheonix maintains a cache value stub in the hash map which enables retrieving value from the disk using a single read operation. The disadvantage of this approach is that the hash map holding keys may grow unpredictably large because the only way for the element that were evicted to disk to leave the hash map is a remove operation.

Enabling Eviction to Disk

The eviction to disk is enabled by setting attribute overflowToDisk to "true" of "yes" of the cache element in cacheonix-configuration.xml. Cacheonix uses optional attribute maxOverflowSizeMBytes to limit the byte size of elements that have been evicted to disk. If the size exceeds the value set by maxOverflowSizeMBytes a to-be-evicted element is silently discarded from the cache:

<cache name="cache.with.eviction.to.disk" overflowToDisk="yes" maxOverflowSizeMBytes="10"/>

Recommendations for Using Eviction to Disk

Our general recommendation is to use a memory-only cache because  it is fast and highly concurrent. Use eviction to disk only if it is really needed.

Use eviction to disk when: 

  • you need to store large sizes of data in a local cache and ready to pay by slower access to data and decreased concurrency.

Do not use eviction to disk if:

  • the cache access is a subject to full scans. A full scan is putting data to cache without subsequent get access that exceeds the size of the cache.
  • the size occupied by keys of values stored on disk is prohibitively high. The estimated size can be calculated as: average key size, bytes * (max overflow size, bytes / average value size, bytes).
  • the cache has to meet strict speed and concurrent access requirements. Disk operations are inherently slow so you may be better off with a small but fast cache.

Assessing Benefit of Eviction to Disk

To avoid surprises, always run load tests for your application with and without eviction to disk to see if the benefit  of enabling it is present.

See Also

Questions, Comments or Suggestions?

Please  post your questions, comments or suggestions regarding this technical note to our support forum.

Labels

 
(None)