Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Redis as a cache storage for for multiple application on the same server

Tags:

I want to use Redis as a cache storage for multiple applications on the same physical machine.

I know at least two ways of doing it:

  1. by running several Redis instances on different ports;
  2. by using different Redis databases for different applications.

But I don't know which one is better for me.

What are advantages and disadvantages of these methods?

Is there any better way of doing it?

like image 350
Behrooz Avatar asked Nov 30 '14 19:11

Behrooz


People also ask

Can I use Redis as storage?

Redis can be used with streaming solutions such as Apache Kafka and Amazon Kinesis as an in-memory data store to ingest, process, and analyze real-time data with sub-millisecond latency. Redis is an ideal choice for real-time analytics use cases such as social media analytics, ad targeting, personalization, and IoT.

Is Redis a shared cache?

Redis is an in-memory data store that is most often used as a distributed cache.

Is Redis good for long term storage?

Redis isn't the best fit for persistent storage as it's mainly performance focused. Redis is really more suitable for reliable in-memory storage/cacheing of current state data, particularly for allowing scalability by providing a central source for data used across multiple clients/servers.

Should Redis be shared?

In general it's not a good idea to share redis. If you only have a limited number of application instances, you are better off creating a separate Redis process for each. Redis is lightweight, so multiple processes running on different parts on the same server works well in practice.


2 Answers

Generally, you should prefer the 1st approach, i.e. dedicated Redis servers. Shared databases are managed by the same Redis process and can therefore block each other. Additionally, shared databases share the same configuration (although in your case this may not be an issue since all databases are intended for caching). Lastly, shared databases are not supported by Redis Cluster.

For more information refer to this blog post: https://redislabs.com/blog/benchmark-shared-vs-dedicated-redis-instances

like image 188
Itamar Haber Avatar answered Sep 22 '22 05:09

Itamar Haber


We solved this problem by namespacing the keys. Intially we tried using databases where each database ID would be used a specific applications. However, that idea was not scalable since there is a limited number of databases, plus in Premium offerings (like Azure Cache for Redis Premium instances with Sharding enabled), the concept of database is not used.

The solution we used is attaching a unique prefix for all keys. Each application would be annotated with a unique moniker which would be prefixed infront of each key.

To reduce churn, we have built a framework (URP). If you are using StackExchange.Redis then yuo will be able to use the URP SDK directly. If it helps, I have added some of the references.

  1. Source Code and Documentation - https://github.com/microsoft/UnifiedRedisPlatform.Core/wiki/Management-Console
  2. Blog Post (idea) - https://www.devcompost.com/post/__urp
like image 34
Pratik Bhattacharya Avatar answered Sep 21 '22 05:09

Pratik Bhattacharya