Summary:
Cache plays a crucial role in maintaining responsiveness and reducing latency. Our current caching framework, although decent, required us to write a lot of code and had certain possibilities of showing stale data. That’s when our search began and finalized with cache2k.
The game changer here was cache2k’s features. From the ability to support null values so the cache loader doesn’t keep searching for non-existing data to automatically expiring and refreshing the cache before expiry to ensure it maintains the latest value, there’s so much more. Read on to learn it all.
Article:
Our Home to Office module caches various application data, such as routes, trip sheets, configurations, etc., to ensure the responsiveness of various functionalities. Without this cache, every request would have to either go to the database or a backend service that stores this information, which in turn adds to the application’s latency.
Until now, we used basic Map structures or Key-Value stores to maintain the cache in the module. While this approach is good enough, we had to write a lot of supporting code around the Map structures to achieve good caching functionality. Moreover, caching can lead to the application showing stale data when not implemented properly.
Our current approach makes implementing advanced caching concepts like cache loading on cache miss, cache expiry, and cache resilience impossible.
Therefore, we explored popular caching frameworks with these features and chose cache2k because it provides the necessary features to build proper caching. Here is a summary of the same:
Cache Loader
The ability to define a loader for a cache that will automatically load or reload the cache from a source (either a database or an API) in case of a cache miss.
Benefits:
Reduces the amount of code needed by eliminating the need to write single or multi-key loaders to implement a cache.
Organizes the cache and cache loader functionality separately.
Resilience and Smart Exception Handling
Cached values are continually served when the cache loader fails to load with a Retry Policy.
Benefits:
When caching values from an external API, such as a Configuration Service or Routing Service, temporary unavailability of these services won’t affect the ETS functionality accessing the cache. The cache will continue to serve the currently cached values while the external API is invoked behind the scenes for an updated value.
Automatic Expiry and Refresh
The ability to expire the cache after a specific duration and refresh it before expiry ensures that it always has the latest value.
Benefits:
For caches such as the vehicle’s current location, which are temporary and need frequent refreshing, this functionality can be used to expire the cache every few minutes. The location service can be invoked behind the scenes to get the new location and update the cache, ensuring the most recent location is available for whichever functionality needs it.
Cache Event Listeners
The ability to listen to cache events, such as cache entries added or removed, update dependent caches, or take any relevant action.
Benefits:
When multiple cache structures for an object are needed to quickly look up the cache with multiple keys (e.g., looking up a driver by its ID and phone number), cache event listeners simplify dependency management. Listeners can be written to update all dependent caches when the underlying primary cache is updated.
Null Value Support
When the cache loader does not find a value for a given key, a null value is added, thus avoiding repeated hits to the database or API for non-existing data.
Benefits:
When a corresponding value does not exist in the underlying source (e.g., trying to get a driver by ID who has been deleted), the external service is repeatedly called as there is no value to cache. Null value support in cache2k helps mark the cache entry as null, returning this to the query and avoiding repeated backend calls.

To Conclude
With these significant features and other value-added benefits that cache2k provides, we have revamped all the caches in ETS using cache2k functions that best suit each type of cache. This will help maintain the application’s performance, ensure data accuracy, and enhance the application’s availability and resilience when a dependent cache data source, such as an API, is temporarily unavailable.
Found this interesting? Subscribe to our newsletter and get the latest tech blogs delivered straight to your inbox.