Most applications today need to handle extreme transaction loads. But, databases are unable to scale to this extent, becoming a bottleneck. To resolve this issue, numerous organizations are turning to in-memory distributed caching because it allows for linear scalability and overcomes the database bottlenecks. A distributed cache typically contains two types of data, transactional data, and reference data.
Transactional data changes very frequently and is cached for a very short time. However, caching still provides a considerable boost to performance and scalability in these cases. Reference data on the other hand does not change very frequently. It may be static data or dynamic data that does not change frequently. There are instances where this data can be substantial, often reaching gigabytes in size. It is advantageous to preload this reference data into a distributed cache at startup using a cache loader, as this eliminates the need for your applications to load it during runtime. Loading large amounts of reference data at runtime can significantly degrade application performance.
Using a Cache Loader to Preload Reference Data into a Distributed Cache?
One approach is to design your application to fetch all the required reference data from the database and put it in the distributed cache during application startup. However, this approach raises some other issues. First, it slows down the application start-up because it is now involved in preloading the cache.
Second, if multiple applications share a distributed cache, you either face code duplication in each application or rely on one application to preload the cache for all. Finally, embedding cache preloading code inside your application corrupts your application design because this preloading code does not belong in your application. Of course, neither of these situations is very desirable.
However, what if we reassign this responsibility to the distributed cache itself? In this case, preloading would become part of the cache start-up process and, therefore, would not involve your application at all.
Preloading Reference Data in Cache
NCache provides a powerful feature, i.e., Cache Loader for preloading your cache on startup, where you can write custom code and register it with NCache. Once this is done, NCache calls the Cache Loader (with your custom code) on cache startup. Before learning about the loader 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 assigned data set. This improves the loader performance by reducing the burden on the cache process.
Datasets: The relevant data is divided into logical datasets by the user and distributed to the nodes using a 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 these datasets 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 you can simply custom code the following methods of the 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 |
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 occurs during the start-up loading process is logged without creating any problem for your application. Simple and effective!
Configure Startup Loader
Once you have implemented the Cache Startup Loader, you can configure it for your cache, using the NCache Management Center. You can enable the loader from the detailed configuration page for the cache in the Management Center. For detailed steps, you can refer to how to configure the cache startup loader using NCache Management Center.
You can also add and remove datasets to a cache with a Cache Loader configured using the Add-StartupLoader and Remove-StartupLoader PowerShell cmdlets, respectively.
Deploy Startup Loader
After you have configured preloading using Cache Loader provider you need to deploy it on your cache servers, to replicate cache loader and refresher assemblies to all server nodes. For detailed steps, you can refer to how to deploy the cache startup loader using NCache Management Center.
Conclusion!
NCache provides a powerful mechanism to preload your distributed cache, ensuring your applications maintain optimal performance from the beginning. Moreover, with the Cache Refresher feature, NCache facilitates the efficient cache data updates. So, sign up for NCache free trial now and get ready to use its various 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!