4

I have been studying about Redis (no experience at all - just studied theory), and after doing some research, found out that its also being used as cache. e.g. StackOverfolow it self.

My question is, if I have an asp.net WebApi service, and I use output caching at the WebApi level to cache responses, I am basically storing kind of key/value (request/response) in server's memory to deliver cached responses.

Now as redis is an in memory database, how will it help me to substitute WebApi's output caching with redis cache?

Is there any advantage?

I tried to go through this answer redis-cache-vs-using-memory-directyly, but I guess I didn't got the key line in the answer:

"Basically, if you need your application to scale on several nodes sharing the same data, then something like Redis (or any other remote key/value store) will be required."

Community
  • 1
  • 1
M. Ali Iftikhar
  • 3,125
  • 2
  • 26
  • 36

1 Answers1

4

I am basically storing kind of key/value (request/response) in server's memory to deliver cached responses.

This means that after a server restart, the server will have to rebuild the cache . That won't be the case with Redis. So one advantage of Redis over a homemade in-memory solution is persistence (only if that's an issue for you and that you did not planned to write persistence yourself).

Then instead of coding your own expiring mechanism, you can use Redis EXPIRE or command EXPIREAT or even simply specifying the expire timestamp when putting the api output string into cache with SETEX.

if you need your application to scale on several nodes sharing the same data

What it means is that if you have multiple instances of the same api servers, putting the cache into redis will allow these servers to share the same cache, thus reducing, for instance, memory consumption (1 cache instead of 3 in-memory cache), and so on...

FGRibreau
  • 7,021
  • 2
  • 39
  • 48
  • Thanks. I am now convinced that this will definitely be needed in case of the scaling thing you mentioned. However for the first part of your answer ... I meant I was using the default Asp.Net Output cache feature. Which i think manages most of the things you mentioned above. I am not "Building" output cache feature, am just using the built-in feature. In that context, can you give a quick note on what redis cache has to offer that asp.net output cache doesn't? – M. Ali Iftikhar Mar 24 '14 at 14:10
  • I also get your point that upon server restarts, the built in asp.net cache will get lost, and redis will maintain its cache. Any other advantage? – M. Ali Iftikhar Mar 24 '14 at 14:12
  • 1
    Sorry, I was not familiar with Asp.net output cache. In the case of a caching scenario, Redis offers many advantages: runtime configuration, master/slave, state persistence, optional durability (see RDB & AOF),... – FGRibreau Mar 24 '14 at 14:36
  • Of course you must take into account that with redis/memchached or another key-value store your data would need to be serialized to another application which slows performance. For the best performance stick with is in memory cache of asp.net (memory is shared so no serialization of data is needed). Of course redis/memchached offers many other advantages that you might be interested for. – George Mavritsakis Apr 23 '14 at 21:49