208

I have not used Redis yet, but I have heard about it and plan to try using it for caching data.

I have heard that Redis uses memory as a cache store database. What's the point of Redis, since I can use an object or dictionary to store data? Like this:

var cache = {
    key: {
    
    },
    key: {
    
    }
    ...
}

What are the advantages of using Redis?

2 Answers 2

330

Redis is a remote data structure server. It is certainly slower than just storing the data in local memory (since it involves socket roundtrips to fetch/store the data). However, it also brings some interesting properties:

  • Redis can be accessed by all the processes of your applications, possibly running on several nodes (something local memory cannot achieve).

  • Redis memory storage is quite efficient, and done in a separate process. If the application runs on a platform whose memory is garbage collected (node.js, java, etc ...), it allows handling a much bigger memory cache/store. In practice, very large heaps do not perform well with garbage collected languages.

  • Redis can persist the data on disk if needed.

  • Redis is a bit more than a simple cache: it provides various data structures, various item eviction policies, blocking queues, pub/sub, atomicity, Lua scripting, etc ...

  • Redis can replicate its activity with a master/slave mechanism in order to implement high-availability.

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.

7
  • 6
    Your last point especially makes it seem like things like Rlite are a bit pointless - a dictionary store would be just as suitable in most use-cases where you have a single process. Is that right?
    – naught101
    Sep 7, 2015 at 5:05
  • 1
    Yes. IMO the interest of Rlite is quite limited. Sep 7, 2015 at 12:35
  • thanks for these hints, so Redis is great to scale but I assume in case of a simple chat'app with in average 300 - 500 object to retrieve in memor, in-memory data structure will do the job very well if not faster since thhese are little number ?
    – CodingWeb
    Feb 24, 2019 at 8:06
  • 4
    @DidierSpezia very large heaps do not perform well with garbage collected languages can you explain why? Jun 30, 2019 at 6:22
  • 7
    @roottraveller, I believe this is because the garbage collection process generally has to interrupt the execution of your application ("stop-the-world") to free heap memory, and the larger the heap, the longer this interruption generally lasts.
    – Regorsmitz
    Sep 27, 2019 at 1:28
14

My team likes using serverless architecture, where each request can go to a different container. In this case Redis can play a very important role.

We can't use a simple cache in serverless because we can't be sure our request will get served by the same container where our simple cache is stored. Redis is a good solution because it stores the cache at a remote location. We can access Redis from anywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.