Today’s applications need to scale and handle extreme levels of transaction loads. But, databases are unable to scale and therefore become a bottleneck. To resolve this, many people are turning to in-memory distributed cache because it scales linearly and removes the database bottlenecks.
A distributed cache typically contains two types of data, transactional data, and reference data. Transactional data changes very frequently and is therefore cached for a very short time. But, caching still provides a considerable boost to performance and scalability.
Reference data on the other hand does not change very frequently. It may be static data or dynamic data that changes perhaps every hour, day, etc. At times, this data can be huge (in gigabytes). It would be nice if this reference data could be preloaded into a distributed cache upon cache start-up because then your applications would not need to load it at runtime. Loading reference data at runtime would slow down your application performance especially if it is a lot of data.
NCache Details NCache Docs Cache Loader and Refresher Docs
How Should Reference Data be Preloaded into a Distributed Cache?
One approach is to design your application in such a way that during application startup, it fetches all the required reference data from the database and puts it in the distributed cache.
However, this approach raises some other issues. First, it slows down your application startup because your application is now involved in preloading the cache. Second, if you have multiple applications sharing a distributed cache, then you either have code duplication in each application or all your applications depend on one application preloading the distributed cache. Finally, embedding cache preloading code inside your application corrupts your application design because you’re adding code that does not belong in your application. Of course, neither of these situations is very desirable.
What if we give this preloading responsibility to the distributed cache itself? In this case, preloading could be part of the cache startup process and therefore does not involve your application at all. You can configure the cache to preload all the required data upon startup so it is available for all the applications to use from the beginning. This simplifies your application because it no longer has to worry about preloading logic.
NCache Details NCache Docs Cache Loader and Refresher Docs
Preloading Reference Data in Cache
NCache provides a very powerful and flexible feature for preloading your cache on startup. You can write custom code for your cache loader and register it with NCache. Once this is done, NCache calls the cache loader (with your custom code) on cache startup. Before moving on to implementation, let’s first get to know the basic components of NCache Cache Startup Loader.
- Loader Service: For a clustered topology, a dedicated loader service runs on each node to load data according to the asshttps://www.alachisoft.com/resources/docs/ncache/prog-guide/cache-startup-loader-overview.html#loader-serviceigned data set. This improves the loading performance by reducing the burden on the cache process.
- Datasets: Data to load is divided into logical datasets by the user and distributed to the nodes using the round-robin algorithm. This speeds up the loading process by achieving parallelism.
To dig deeper into the NCache preloading feature, you can see the cache loader properties. Besides, NCache also facilitates refreshing the loaded cache data by providing the Cache Refresher feature.
Implement ICacheLoader Interface
To enable preloading using NCache, you need to implement a simple interface named ICacheLoader
. It is called to assist the cache in answering the question “How and which data to load?”. Here is how you can simply custom code the following methods of ICacheLoader
interface based on your business requirements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
class CacheLoader : ICacheLoader { public void Init(IDictionary<string, string> parameters, string cacheName) { //connect to cache cache = CacheManager.GetCache(cacheName); //Connection string is passed as parameters at the time of configuration // connectionString = parameters.ContainsKey("ConnectionString") ? parameters["ConnectionString"] : null; if (connectionString != null) { //Let's connect to the database connection = new SqlConnection(connectionString); } } public object LoadDatasetOnStartup(string dataSet) { // Create a list of datasets to load at cache startup IList<object> datasetToLoad; if (dataSet != null) { switch (dataSet.ToLower()) { // If dataset is "products", fetch products from data source to load in cache case "products": datasetToLoad = FetchProductsFromDataSource(); // Insert fetched product in the cache // You can also use NCache bulk API to insert data into cache foreach (var product in datasetToLoad) { string key = $"ProductID:{product.Id}"; cache.Insert(key, product); } break; // You can add more cases for different datasets as per requirement and fetch them from the data source default: // Invalid dataset throw new Exception($"Unknown dataset is configured. Dataset {dataSet}"); break; } } return null; } } |
Any exception that occurred during startup loader processing is logged without creating any problem for your application. Simple and effective!
Configure Startup Loader
Once you have implemented Cache Startup Loader, you can configure it for your cache, using NCache Web Manager. You can enable cache loader from detailed configuration page for the cache in NCache Web Manager. For detailed steps, you can refer to how to configure cache startup loader using NCache Web Manager.
You can also add and remove datasets to a cache with cache loader configured using the Add-StartupLoader
and Remove-StartupLoader
PowerShell cmdlets, respectively. You can see the relevant examples for add loader dataset and remove startup loader for better understanding.
Conclusion!
As you can see, NCache provides you with a powerful mechanism to preload your distributed cache and keep the performance of your applications always high. Besides, NCache also facilitates refreshing the loaded cache data by providing the Cache Refresher feature. So, download NCache now and get ready to use its vast features!
It’s in reality a nice and helpful piece of information. I am satisfied that you shared this helpful info with us.
Please stay us informed like this. Thank you for sharing.
Hello there, I found your web site by means of
Google while searching for a comparable topic, your web site got here
up, it seems to be good. I’ve bookmarked it in my google bookmarks.
Hi there, simply became alert to your weblog via Google, and found that it is truly informative.
I am gonna watch out for brussels. I will appreciate should you proceed this
in future. A lot of people will probably be benefited
from your writing. Cheers!