Indexing for Improved Cache Search
The purpose of using an index is to optimize speed and performance in finding relevant data against cache search. For searching purposes, all searchable attributes should be indexed. Without an index, the search would scan every data, which would require considerable time and computing power. The additional computer storage required to store the index, as well as the considerable increase in the time required for an update to take place, are traded off for the time saved during information retrieval.
NCache internally uses indexing in different modules to boost the performance of internal operations.
Cache Searching Index
NCache provides the Cache Searching Index to boost the execution of queries on the cache. For searching purposes, all that is needed to be done is to just index all the searchable attributes. NCache will not search the non-indexed attribute from the cache because it has to traverse through the whole cache to find related data which will affect the cache performance.
Also, indexes are defined against the actual data types of the cached items. Through the indexing, NCache knows what kind of data will be stored and it will store it in a searchable way.
NCache provides two ways to define indexes.
- Pre-Defined Index (Static Index)
- Runtime Index (Dynamic Index)
Pre-Defined Index (Static Index)
Note
This feature is also available in NCache Professional.
Note
It is recommended to avoid indexing unnecessary fields since indexing has memory and performance overhead.
To search custom class objects in a clustered cache, the query indexes for that class need to be defined in advance. In this way, the cache will save that data in the form where it can be easily queried. You can define indexes through the following configuration process. Indexes can be configured for the public, private, and protected primitive data members.
Note
Indexes are only supported on value types. Reference types can not be indexed.
Indexes can be defined through configuration before the cache start. In case you want to define new indexes through configuration, the cache needs to be stopped first and restarted after modifications for deploying changes. The relevant data added to the cache subsequently is automatically indexed. This data can be queried easily in the future.
Warning
Explicitly marking a reference type as indexable will throw an exception.
Runtime Index (Dynamic Index)
If there are attributes in an application that are defined and used at runtime, then runtime indexing should be used. For example, there may be some calculated values assigned to a product related to its sales. When any product is purchased by any customer, the sale details are saved in the cache for generating daily reports of sales trends through cached values later. This value is defined at runtime so it’s the runtime attribute of the product.
NCache will automatically create indexes for data that are added by the cache client. It means that indexes do not need to be configured before using them for runtime attributes.
When defining indexes programmatically, the classes or fields can be marked indexable using custom attributes in the application code. Once marked, all the primitive properties and fields are automatically indexed. Since you may not need to index all the fields, you can also exclude them from indexing. Besides indexing the whole class, you also have the flexibility to define indexes selectively for the desired fields only.
The runtime index can be further categorized into the following types:
- Group Index
- Tag Index
- Named Tag Index
Group Index
Items can be categorized in the cache using groups. NCache allows you to query data in the cache based on groups, due to which groups need to be indexed as well. When data is added to a group that does not already exist in the cache, a new index is created for the group. Any data belonging to this group is then assigned to the same index for searching.
Tag Index
One or more identification marks can be associated with cache items. These identification marks are called tags. Through tags, the user can associate keywords(s) with cache items. A collection of cached items can be found and removed from the cache by specifying tags too. Tags are only allowed in string format. For every new tag, a new tag entry will be created in the tag index and all related cached items will be associated with that index.
Named Tag Index
If there is a need to use a high level of tagging where tags can have different data types or names and the requirement is to query data related to a specific type of tag, then Named Tags should be used.
“Named Tags" is the enhancement of "Tags". Named tags allows the user to store additional information (of any type) required to query the object stored as a string. For example, the "Highest-Sale-Time" named tag with a value of time can be stored when any product sale is the highest in a whole day. Named tags allow indexing the data according to required attributes at runtime. Later on, a query can be constructed using named tags to fetch the desired result set. Multiple named tags can be associated with one cached item and vice versa.
When data is added with a named tag that does not exist in the cache, a new index is created for this new named tag. All the related data will be assigned to that index for searching.
Unlike Tag, the named tag index can be defined for all primitive types, strings, and date time. So there is more flexibility to add a wide range of searchable data with different data types.
If there are multiple applications that are sharing the same cache and all of them are supposed to add named tags, then make sure that the same named tags have homogenous data types, e.g., if one client is adding the named tag "ProductID" with String data type, then all other clients should add values of "ProductID" in String format not in Integer or other for the same cache.
Eviction Index
Eviction is a useful feature in NCache, where when the cache is full it decides to evict its existing data so as to accommodate incoming data. In this scenario, eviction can smooth cache operations while keeping the cache size limit by removing a configured percentage of data.
NCache provides different policies for eviction. These policies decide which data will be evicted when the cache is full. NCache provides the following policies for eviction:
- Priority Based Eviction
Note
This feature is also available in NCache Professional.
For Priority Based Eviction, the index is kept for priority from high to low and when the cache is full, the eviction thread selects keys from index with low priority and evicts them from cache.
- Least Recently Used (LRU)
For the Least Recently Used (LRU), eviction index is kept for the time of usage of keys and when the cache is full, the eviction thread selects keys from the index that are least recently used and evicts them from the cache.
- Least Frequently Used (LFU)
For Least Frequently Used (LFU), the eviction index is kept for the number of usage of keys and when the cache is full, the eviction thread selects keys from the index that are least frequently used and evicts them from the cache.
Note
In order to perform Eviction, NCache keeps the index of eviction for cache data for eviction.
Expiration Index
Expiration is a property attached with each item that is being cached. This property is attached with each object so that each cache object can be evaluated upon its age, this age of the object will decide upon its expiration time. When that expiration object invalidates, the associated item is removed from the cache.
NCache keeps the DateTime
Expiration index for cache keys in order to remove them efficiently on expiration. Expiration manager expires items after Clean Interval.
Clean interval is the periodic interval after which the expired items are removed from the cache. The clean interval for a cache can be changed in configuration settings.
After a clean interval has elapsed, the expiration manager gets keys from the index and expired items are removed from the cache. After removing expired items from the cache, the expiration thread sleeps for the “clean interval duration” specified in the cache configuration. NCache provides two types of expirations explained in Data Invalidation Strategies:
- Absolute Expiration
- Sliding Expiration
See Also
Configure Query Indexes
Remove-QueryIndex
Eviction
Runtime Data Sharing