Search code examples
javacachingredismemcacheddistributed-caching

Using Distributed Cache with option to handle Cache Miss


My use case is as following:

Application clients uses Distributed Cache (Memcached/Redis/etc) to cache the DB output. I would want to add one more functionaloty hidden from Application Clients - How to Handle Cache Miss e.g. Application client just asks to get values for key to Distributed Cache (without knowing the DB details). Now, it would want the distributed cache to handle the cache-miss - get the data from Relational DB and cache it.

It seems Redis/Memcached do not provide such feature. Am I missing something or I should be looking at some other tool/framework for this use case.


Solution

  • You need to use read-through/write-through caching pattern where client application will consider cache as main data store and perform all read and write operations on cache. Cache on other hand will be responsible to sync itself with the database using deployed read-through/write-through providers.

    On read operation if data is not present in cache, cache will itself load data from database avoiding cache miss.

    Read this article by Iqbal Khan for further details on read-through/write-behind caching.

    This feature is available in TayzGrid by Alachisoft. Java client of NCache also provides this feature.