Redis Pub/Sub : Keep your InMemory Cache Fresh

Redis has a very nice feature called Pub/Sub. You can publish an event when a key value change and other applications can subscribe to this event. You can use this feature to keep your in memory cache always fresh.

Problem

InMemory cache is the fastest cache and very critical for performance of your application. But the problem with in memory cache is its not possible to clear your cache when the data change in original source (i:e database).

I am trying to explain the whole idea using an example. Suppose your application displays list of categories with count (number of books in each category) in websites to allow users browse books by category. As categories do not change that much and we don’t add books every minute so your team decided to put this in memory cacheĀ for 30 minutes. Which works fine but sometimes when you add a new book under a category the count didn’t update in website immediately as they cached in memory for 30 minutes. The update could take maximum 30 minutes to display in browser. Because your application deployed in web farm all individual instance has its own in memory cache of this data.
Continue reading

Multilevel Caching using Bolt.Cache – InMemory and Redis

Caching is a very important part of scaling your website. In this blog post I am trying to show how you can implement a two level caching. One Redis for distributed caching and the other one is inmemory caching. So the idea is first you look whether data exists in memory. If not then check whether the data exists in Redis. If not then load from your Sql Data source. And then save this in memory and Redis. So for next request we can serve the data from memory, if not from Redis. Continue reading