31

Redis is often used as a cache, although it offers a lot more than just in-memory caching (it supports persistence, for instance).

What are the reasons why one would choose to use Redis rather than the .NET MemoryCache? Persistence and data types (other than key-value pairs) come to mind, but I'm sure there must be other reasons for using an extra architectural layer (i.e. Redis).

Gigi
  • 28,163
  • 29
  • 106
  • 188
  • 2
    Isn't MemoryCache embedded in the app server? Redis is a network server that can be accessed from different clients. – Itamar Haber Mar 10 '15 at 19:06

1 Answers1

29

MemoryCache is embedded in the process , hence can only be used as a plain key-value store from that process. An seperate server counterpart of MemoryCache would be memcached.

Whereas redis is a data structure server which can be hosted on other servers can interacted with over the network just like memcached , but redis supports a long list of complex data types and operations on them, to provide logical and intelligent caching.

DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
  • What benefits do you get by having it in a separate process? – Gigi Mar 11 '15 at 10:53
  • 1
    Not just seperate process, even a seperate server. One simple benefit is seperation of modules in architectures, other is scalability. MemoryCache will be limited to the app server memory, whereas in Redis or Memcached you can add multiple servers or increase memory of the redis server to keep performance good. – DhruvPathak Mar 11 '15 at 11:01
  • 1
    I'm not convinced. As far as I know Redis replicates the keys across instances, so the memory usage would be the same everywhere (roughly speaking). – Gigi Mar 11 '15 at 12:08
  • @Gigi I was never talking about memory usage, I was talking about scalability. Lets assume your cache uses becomes 32GB, you would be able to use 4 different 8GB servers with redis or memcache, still running only 1 web server. But with memory cache you would have to multiply the web servers. Secondly, about the data structures, you cannot use rich data structures like sorted set with MemoryCache. – DhruvPathak Jun 05 '15 at 06:13
  • 10
    I can add one benefit : imagine you have MVC Web app that is caching data, and you have another WebAPI that need to use same cached data, because if each pillar will have its own cache you will face inconsistency when a data cached by the 2 was modified by one, and thus invalidated its cache, the other side still having the old cached values ... – a.boussema Jul 09 '15 at 00:06
  • 6
    another benefit ; if session state is stored Inprocess, and for one reason you have Web server crash, all the data within it will be simply loosed. Not the same when you have the session data cached elsewhere – a.boussema Jul 09 '15 at 00:09
  • In addition, this answer would also add more info. https://stackoverflow.com/questions/19477821/redis-cache-vs-using-memory-directly – Maytham Fahmi Oct 27 '21 at 12:29