My application is built on spring boot and is backed by Redis datastore. Currently I have a connection only to a single redis server and the properties of that server ( host / port ) is defined in bootstrap.yml.
I want to support multi-tenancy by using a separate Redis server for each customer. In order to do this I need to dynamically connect with various different redis server at runtime. Is it possible with RedisTemplate and JedisConnecitonFactory ?
In short
No need for multiple
RedisTemplateinstances; implement a routingRedisConnectionFactory.Explanation
RedisTemplateuses an underlyingRedisConnectionFactoryto obtain connections. To route Redis operations to different connections, just implement a routing variant ofRedisConnectionFactory.RedisTemplateasks the associatedRedisConnectionFactoryfor each operation to provide aRedisConnection.You need to pay attention when using transactions. Transactional use binds the Redis connection to the calling Thread and
RedisTemplateworks effectively on the same connection until the transaction is finished.Take a look at AbstractRoutingDataSource to get an idea how data source routing is supported for JDBC connections. A routing
RedisConnectionFactorycould follow the same pattern.