sam
sam

Reputation: 1243

Spring Data Redis with multi tenancy

I am using Spring Data Redis, with spring data abstraction not using directly RedisTemplate.

My data model like below:

@RedisHash(value = “products")
public class Product {

    @Id
    @Indexed
    private String id;
    private String description;
    private BigDecimal price;
    private String imageUrl;

   //Getter and Setter

}

My repositories with spring data abstraction:

@Repository
public interface ProductRepository extends CrudRepository<Product,String> {
}

This is my config :

@Configuration
@EnableRedisRepositories(enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP)
public class RedisConfig {

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory jedisConFactory = new JedisConnectionFactory();
        jedisConFactory.setHostName("localhost");
        jedisConFactory.setPort(6379);
        return jedisConFactory;
    }
}

I am happy with these for a single tenant application.

And now I want to implement a Multi-tenant structure.

I thought that create a Redis instance for each tenant is a good solution.

I have a map that contains tenant id and Redis endpoint which is dedicated to this tenant.

Map data seems like below:

(Key : tenantId1, value: host1:port1) 
(Key : tenantId2, value: host2:port2)
(Key : tenantId3, value: host3:port3)

The scenario in my mind:
a tenant comes to the application with its tenant id and pass the request to redisRepository with tenant id. Example for save new product : productRepository.save(product,tenantId).

But can't imagine how to implement this routing.

I thought that create a RedisConnectionFactory for each tenant.

But I don’t know how to select related connectionFactory on Spring data abstraction.

Can somebody help me?

Upvotes: 1

Views: 2243

Answers (1)

CodeIsLife
CodeIsLife

Reputation: 1255

in order to implement a multitenant structure you can proceed as below :

  @Bean(name = "JedisConnectionFactor" )
  public Map<String, JedisConnectionFactory> JedisConnectionFactor(){
    Map<String, DataSource> factories = new HashMap<>();
    //for (JedisProperties properties :  // better to import jedis config from for each single tenant
      //multiTenantJedisProperties.getListOfProperties()) { 

      JedisConnectionFactory jedisConFactory = new JedisConnectionFactory();
      jedisConFactory.setHostName("localhost");
      jedisConFactory.setPort(6379);
      result.put(jeditProperties.getTenantId(), jedisConFactory)
     // iterate ....
      JedisConnectionFactory jedisConFactory = new JedisConnectionFactory();
      jedisConFactory.setHostName("localhost");
      jedisConFactory.setPort(6379);
      result.put(jeditProperties.getTenantId(), jedisConFactory);
     // n times

    }
    return factories;
  }

and you can use autowire the factories map

@Autowired
   private Map<String, JedisConnectionFactory> JedisConnectionFactories;

   @Autowire
   protected DataSource selectJedisConnectionFactory(String tenantId) {
     return this.JedisConnectionFactories.get(tenantId);
   }

i hope this will help !

Upvotes: 0

Related Questions