Most successful e-commerce businesses today have encountered slow website response times at some point. This is partly due to a paradigm shift in user preferences – more inclined towards online shopping. Therefore, traditional mechanisms have failed to sustain the influx of customer requests.
With the advent of in-memory, linearly scalable distributed caching solutions like NCache, website performance and business response times have been revolutionized. By integrating a caching layer into their system architecture, numerous enterprises have significantly enhanced their efficiency and customer satisfaction, fueling their growth and success.
To further capitalize on these benefits, NCache offers Cache Loader – to preload cache data. In this blog, we will explore how using a Cache Loader in NCache can further boost your e-commerce business by pre-loading crucial data, ensuring your system is ready for high-traffic events, and improving overall performance.
What is a Cache Loader?
Imagine preparing for a major flash sale on your e-commerce site that’s scheduled to start at midnight. You’re expecting a huge influx of customers all trying to access the sale simultaneously. To handle this rush smoothly, you need to ensure your website is ready ahead of time. This is where a Cache Loader comes into play.
By using a Cache Loader, you can pre-load your cache with critical information, such as discount details, before the sale begins. This means that when the sale starts and customer traffic spikes, your site can quickly and efficiently serve the necessary data, avoiding delays and improving the user experience.
With that said, NCache provides a powerful feature: the Cache Startup Loader. This feature helps to pre-load your cache with data, ensuring that your system is fully prepared for high-traffic events right from the start. NCache provides an ICacheLoader interface, which you can implement and deploy on your server. This interface allows you to define how data should be loaded into the cache before it’s needed. The ICacheLoader interface includes three key methods:
- Init: Initializes the Cache Loader with any required parameters.
- LoadDatasetOnStartup: Loads the specified data set into the cache when the application starts.
- Dispose: Cleans up any resources used by the Cache Loader.
1 2 3 |
public void Init(IDictionary<string, string> parameters, string cacheName); public object LoadDatasetOnStartup(string dataset); public void Dispose(); |
Cache Loader Features
NCache’s Startup Loader is designed to enhance user experience by addressing common client-side issues and providing effective solutions. With its robust feature set, the NCache Startup Loader ensures a seamless and efficient data loading process, improving overall application performance. Here’s a closer look at the core features of the NCache Loader:
Easily configurable
The ICacheLoader interface is easy to configure. You just have to implement it and then deploy your logic via the NCache Management Center on the server-side. By enabling the Cache Loader feature, your code is automatically executed on cache start-up.
Logical data sets
For applications using a multi-node cache cluster, NCache optimizes data loading by dividing your data into logical sets. This allows for efficient distribution of data across cluster nodes. For instance, if you have a two-node cluster and need to load data sets such as products and suppliers, you can configure these data sets in your interface logic. NCache will then distribute the data loading process across both nodes simultaneously, speeding up the overall process. Below is a sample implementation of the method LoadDatasetOnStartup:
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 |
public object LoadDatasetOnStartup(string dataset) { // Create a list of datasets to load at cache startup IList<object> datasetToLoad; switch (dataSet.ToLower()) { // If dataset is "products", fetch products from data source to load in cache case "products": datasetToLoad = FetchProductsFromDataSource(); // Insert fetched products in the cache foreach (var product in datasetToLoad) { string key = $"ProductID:{product.Id}"; cache.Insert(key, product); } break; // If dataset is "suppliers", fetch suppliers from data source to load in cache case "suppliers": datasetToLoad = FetchSuppliersFromDataSource(); // Insert fetched suppliers in the cache foreach (var supplier in datasetToLoad) { string key = $"SupplierID:{supplier.Id}"; cache.Insert(key, supplier); } break; default: // Invalid dataset } // User context is the time at which datasets were loaded in the cache object userContext = DateTime.Now; return userContext; } |
A Dedicated Execution Service
NCache uses a dedicated Execution Service on each of the server nodes to manage tasks and load data from the data source into caches on cache startup. This helps improve the overall cache speed. This is a critical NCache feature because if a large data set needs pre-loading in the cache, the separate execution service ensures that this does not hinder the regular cache performance.
Use NCache Refresher to Re-load Cache Data
While a preloaded cache can significantly improve performance, there’s a risk of the cache data becoming stale due to periodic updates at the backend data source. When this happens, the cache data loses its value, failing to meet the purpose of pre-loading.
Continuing with the e-commerce example, say that in the first phase, you were providing a flat 25% off on selected items, but suddenly, you decide to increase the discount percentage to 50% and apply it to the entire stock. What happens to the existing cache data? It’s now outdated – which means you need a mechanism to refresh the relevant data set in the cache. NCache addresses this issue by allowing users to refresh the cache periodically with the latest data from the source. You can do this by either scheduling your data sets or by invoking the refresher on demand, ensuring your cache always contains up-to-date information.
Refresh Dataset
If you can predict when your cache will need to be refreshed, you can implement the RefreshDataset method of the interface and choose an appropriate time interval which can be hours, days, weeks, or months. Below is a sample implementation of the method RefreshDataset:
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 |
public object RefreshDataset(string dataset, object userContext) { DateTime? lastRefreshTime; switch (dataset.ToLower()) { // If dataset is "products", fetch updated products from data source case "products": lastRefreshTime = userContext as DateTime?; IList<Product> productsToRefresh = FetchUpdatedProducts(lastRefreshTime) as IList<Product>; // Insert updated products in the cache foreach (var product in productsToRefresh) { string key = $"ProductID:{product.Id}"; CacheItem cacheItem = new CacheItem(product); _cache.Insert(key, cacheItem); } break; // If dataset is "supplier", fetch updated suppliers from data source case "suppliers": lastRefreshTime = userContext as DateTime?; IList<Supplier> suppliersToRefresh = FetchUpdatedSuppliers(lastRefreshTime) as IList<Supplier>; // Insert updated suppliers in the cache foreach (var supplier in suppliersToRefresh) { string key = $"SupplierID:{supplier.Id}"; CacheItem cacheItem = new CacheItem(supplier); _cache.Insert(key, cacheItem); } break; default: // Invalid dataset } // User context is the time at which datasets were refreshed userContext = DateTime.Now; return userContext; } |
GetDatasetstoRefresh
If, on the other hand, you are unsure when your cache data will become outdated, you can implement the GetDatasetsToRefresh method in the ICacheLoader interface to programmatically identify and refresh the datasets that have changed. You can also use the PowerShell Invoke-RefresherDataset cmdlet to refresh the cache on demand.
1 |
public IDictionary<string, RefreshPreference> GetDatasetsToRefresh(IDictionary<string, object> userContexts); |
Conclusion
A Cache Loader is not just an option anymore – it’s a must-have feature for enterprises looking to expand their clientele globally because in this fast-paced, competitive era, the front face of your business can never go down. So, if you are looking for a distributed caching solution like NCache to help grow your business, contact us and let our technical experts help you!