Hi,
It would be nice if we could use many redis database indexes with Redis auto configure. Imagine we have the following configuration :
redis:
...
databases: { # Database indexes used by the connection factory.
'refDb' : 2,
'ttuDb' : 3
}
which instantiate two redisTemplates instances prefixed by the name of the database key (e.g refDbRedisTemplate and so one)
Comment From: snicoll
We got the exact same requests for the JPA support several times and we chose not to implement it. IMO, this goes a bit too far with regards to the convention (especially the prefix part).
Comment From: RaphC
Right. If I understand, AutoConfig... aims to be the simplest approach for the use of a technology. So as soon as we consider a more advanced use, we should forget AutoConfig feature and define our own beans configuration.
Moreover we could imagine 2 kinds of RedisAutoConfiguration - the RedisAutoConfiguration (the existing one) for simple use - the MultipleDatabaseRedisAutoConfiguration for more advanced use like multi database management.
What do you think?
Regards,
Comment From: snicoll
What do you think?
I think you have a very narrowed view of what the auto-configuration can and should provide. It's not a matter of "advanced" use case but a matter of how we could have an opinion about it that makes sense. If you need to create several connection factory, do you mark one as @Primary? Which one? If you don't then you can't autowire by type so you need to document that. How do you manage that from a configuration standpoint? What prefix do you use? Do you stop at 2 instances? 3? Unlimited.
My own take at this problem is that the auto-configuration should be designed in such a way that you could reuse pieces of it to do your own thing. DatasourceBuilder is perfect example of that for the JPA World. Maybe we need something of the kind of Redis.
The bottom line is that if you can't find a relatively straightforward answer for all theses questions, maybe it shouldn't happen automatically (via auto-configuration that is).
Comment From: RaphC
My narrowed view is certainly due to my young experience on spring-boot projects :). The idea was to create several connection factories with a name like this xxxRedisConnectionFactory and inject them into their own redisTemplate (e.g xxxRedisTemplate) where xxx is the key linked to the index database (if we use a Map structure for database property configuration under RedisProperties). Is @Primary mandatory if I inject myself the correct redisTemplate instance in my code ?
You're right, this way prevents the autowiring by type and then makes more difficult the understanding or use of auto-configuration.
Concerning the limits , in theory 42 which is by default the number of databases on redis but it can be overriden so unlimited ? (42 instances of connection factories and so one ... I don't know if the application can supports this).
Perhaps this case is not a good practice. Someone prefers to use differents instances of redis instead...
Comment From: snicoll
I didn't meant narrowed in a bad way (though I agree I could have phrased it differently, sorry about that). I think I have a good understanding of what you want to do and all I can say is that there is a reason why we haven't added support for that in the past (be it for Redis or something else). I've added the team discussion label so we're going to revisit this issue later this week.
Comment From: RaphC
Thanks.
Regards
Comment From: snicoll
After a careful consideration we've decided not to implement this for the reason I mentioned above.
Comment From: RaphC
Ok,
Thanks for the time you spent about this.
Regards
Comment From: madhead
the auto-configuration should be designed in such a way that you could reuse pieces of it to do your own thing.
DatasourceBuilderis perfect example of that for the JPA World. Maybe we need something of the kind of Redis.
How can one reuse the existing autoconfiguration to connect to the different database on the same server? I have a configuration like this:
spring:
data:
redis:
host: ${REDIS_HOSTNAME}
port: ${REDIS_PORT}
password: ${REDIS_PASSWORD}
ssl:
enabled: on
lettuce:
pool:
enabled: true
max-active: 8
min-idle: 1
This configuration connects me to the database #0 on my server. Now I need to create another connection (RedisConnectionFactory / ReactiveRedisConnectionFactory) to the same server, with the same configs, but different database, #1.
I find it impossible. Classes like PropertiesRedisConnectionDetails, LettuceConnectionConfiguration, etc, that might be useful to reuse, are not public, so I cannot access them in my code. I basically need to reimplement the whole org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration#redisConnectionFactory on my own to achieve the goal. That's frustrating.