12

I am wondering what the benefits of Redis with its C# client over Dictionary/ConcurrentDictionary and otherwise.

I am not sure when using redis is considered overkill for a dictionary storage.

Thanks.

Aviran Cohen
  • 5,581
  • 4
  • 48
  • 75
  • I want to learn how redis should be used, and what are its advantages over regular in memory c# object, its not meant to be a one on one bar fight between these two. – Aviran Cohen Apr 06 '14 at 19:12
  • Yes but you know how these type of questions do end up in SO : subjective answer, so either you have a precise question to ask or you won't get any answer or it will get closed. – aybe Apr 06 '14 at 19:14
  • Did @Ofer Zelig answer help you? – erexo Jul 07 '22 at 07:07

2 Answers2

22

Redis is probably an overkill for a local, one-machine app. Especially when the data is not large.

It's mainly used as an L2 cache layer. Say you've got multiple machines serving your app, each machine can hold its own local cache and Redis can serve as a global cache for all of them.

Let's say a user of your app browses to a page or feature that requires some data from your back end database. Your app will then check its local L1 cache (Dictionary for example). That would be the fastest method as it does not involve any network roundtrip. If the data is not there, it will look for it in Redis as the global app cache. If it's there - great - fetch the data and put in in your local L1 cache. If not, go to the database, fetch the data, put it in Redis (L2) and in your local cache (L1).

You can read more on that here.

That said, there are more usages for Redis other than mere cache - Pub/Sub functionality, SETs, SORTED SETs and functionalities on them (like intersections, unions etc.) and even smart functionalities on STRING types, such as bitwise operations.

Ofer Zelig
  • 17,068
  • 9
  • 59
  • 93
6

As you already know, using Redis for in-server caching would be an overkill.

But using Redis with C# in a distributed application definitely offers some benefits (all of the below operations are thread-safe):

  1. You can build a custom ORM wrapper for your project in C# such that your .NET objects from one server can be seamlessly accessed from another. You can cache your Dictionary objects in Redis hashes, your List objects in Redis lists, your HashSet objects in Redis set etc. If your values are strings/ints/floats there is no need to even serialize!
  2. Publish/Subscribe to/from multiple servers/threads.
  3. C# background services / schedulers can benefit from maintaining tasks or objects in Redis lists and get event triggers using Redis's blPop API.
  4. Maintain 'top 10', 'bottom 10' items etc using Redis's sorted sets.

But at the end of the day, it all depends on your use-case.

Manu Manjunath
  • 6,201
  • 3
  • 32
  • 31