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

3

27

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?

Monitor answered 30/11, 2014 at 19:53 Comment(0)
S
20

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

Strasser answered 30/11, 2014 at 21:9 Comment(1)
Also, dumps are not managed the same way, better to have different dumps, you can't isolate rdb by db.Underpin
H
4

We solved this problem by namespacing the keys. Initially we tried using databases where each database ID would be used for a specific application. 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 in front of each key.

To reduce churn, we have built a framework (URP). If you are using StackExchange.Redis then you 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
Hallagan answered 2/10, 2021 at 5:20 Comment(2)
Does the URP work on other non-Azure Redis clusters?Ger
Yes it should. URP works with a Redis connection string, so any REDIS connection string will work.Hallagan
B
0

You can use different cache manager for each application will also work same way I am using. like :

@Bean(name = "myCacheManager")
    public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        return cacheManager;
    }

    @Bean(name ="customKeyGenerator")
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object o, Method method, Object... objects) {
                // This will generate a unique key of the class name, the method name,
                // and all method parameters appended.
                StringBuilder sb = new StringBuilder();
                sb.append(o.getClass().getName());
                sb.append(method.getName());
                for (Object obj : objects) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }
Berthoud answered 30/10, 2019 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.