Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache Server like Redis Vs Static variable

I wanna cache an array of object that is not more than 300 items, It's read only array. First I implemented it in Redis with StackExchange.Redis client, and then implement it by static variable. Static variable has e better performance and get less CPU usage instead of Redis, but I do not know that is it good way or not?

like image 233
Behrooz Avatar asked Feb 23 '15 06:02

Behrooz


People also ask

Can Redis be used as a cache and session store?

It’s possible to use Redis as both a cache and a session store in a single setup as shown in the picture below. In this design, the application layer accesses and maintains the data in the cache. Therefore, all the sessions running inside the application access the same data stored in the cache.

Is Redis slower than local memory?

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).

What's the advantage of Redis?

} What's the advantage Redis have? 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:

Why we can't use simple cache in server less?

We can't use simple cache in server less as we can't be sure our request gets served at same container where our simple cache is stored. In this case, we have to use redis as it stores cache at remote location & we can access that even container changes in server less architecture.


2 Answers

If the data is truly static, then while yes you could store the data in redis, you would not usually go to redis every time - so it would probably end up having a local cache anyway (with redis as a second level cache). static can work fine for things like a read-only array, and this will be unbeatable in terms of performance. However, if you start mutating the contents of a static member: expect pain.

like image 200
Marc Gravell Avatar answered Sep 30 '22 02:09

Marc Gravell


In one of my project we used redis for caching and also we used local cache (Not static variable but you can also use static variable).

But it really depends on your requirements. We used redis because we had web farm and we wanted to share data between servers. We also used local caching (see MemoryCache) to increase performance.

like image 21
Vano Maisuradze Avatar answered Sep 30 '22 01:09

Vano Maisuradze