We’ve been using microservices in my workplace for a few years now, using a combination of Spring as our microservice framework and Redis as our cache layer, with Redis Sentinel helping to manage our high availability (i.e., HA). It’s worked well for us with our current master-slave configuration for HA, but we’ve always been curious about creating some load distribution among our Redis nodes, by redirecting some reads to the slaves. However, from what I’ve read in the past, it’s a bit tricky with the code to have both HA (through Sentinel) and I/O redirection. It seemed enough of an annoyance that it wasn’t really worth it. At least, until now!
In the next (i.e, Lovelace) release train of the Lettuce project, there will now be an optional way to create Redis connections with reads preferably directed to the slaves. And, if you still want to keep Redis Sentinel functionality, you just have to do the following:
@Bean
@Qualifier(value = "redisConnectionFactoryForMe")
public LettuceConnectionFactory redisConnectionFactory1() {
LettuceConnectionFactory redisConnectionFactory = new LettuceConnectionFactory();
// Do some stuff here
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration().master(masterName);
// Do other stuff here
LettuceClientConfiguration config = LettuceClientConfiguration.builder().readFrom(ReadFrom.SLAVE).build();
redisConnectionFactory = new LettuceConnectionFactory(sentinelConfig, config);
return redisConnectionFactory;
}
And now you can have the best of both worlds!
Speaking of microservices, I still think that it would be a cool idea if Spring became more flexible with its configuration data. Like most Java applications, configuration data comes from the usual suspects: properties files in the Resources folder, environment variables, command-line options, etc. However, wouldn’t it be especially cool if configurations could come from dynamic sources, like database tables? And the services could change their configuration dynamically, by occasionally reading the configuration from the table? Sounds like an opportunity for MDD to make an entrance! But that’s a topic for another time.