0
votes

My work system consists of Spring web applications and it uses Redis as a transaction counter and it conditionally blocks transaction requests.

The transaction is as follows:

  1. Check whether or not data exists. (HGET)
  2. If it doesn't, saves new one with count 0 and set expiration time. (HSET, EXPIRE)
  3. Increases a count value. (INCRBY)
  4. If the increased count value reaches a specific configured limit, it sets the transaction to 'blocked' (HSET)

The limit value is my company's business policy.

Such read and write operations are requested one after another, immediately. Currently, I use one Redis instance at one machine. (only master, no replications.) I want to get Redis HA, so I need slave insntaces but at the same time, I want to have all reads and writes to Redis only to master insntaces because of slave data relication latency.

After some research, I found that it is a good idea to have a proxy server to use Redis HA. However, with proxy, it seems impossible to use only the master instances to receive requests and the slaves only for failover. Is it possible??

Thanks in advance.

2

2 Answers

1
votes

What you need is Redis Sentinel.

With Redis Sentinel, you can get the master address from sentinel, and read/write with master. If master is down, Redis Sentinel will do failover, and elect a new master. Then you can get the address of the new master from sentinel.

0
votes

As you're going to use Lettuce for Redis cluster driver, you should set read preference to Master and things should be working fine a sample code might look like this.

    LettuceClientConfiguration lettuceClientConfiguration =
        LettuceClientConfiguration.builder().readFrom(ReadFrom.MASTER).build();

    RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
    List<RedisNode> redisNodes = new ArrayList<>();
    redisNodes.add(new RedisNode("127.0.0.1", 9000));
    redisNodes.add(new RedisNode("127.0.0.1", 9001));
    redisNodes.add(new RedisNode("127.0.0.1", 9002));
    redisNodes.add(new RedisNode("127.0.0.1", 9003));
    redisNodes.add(new RedisNode("127.0.0.1", 9004));
    redisNodes.add(new RedisNode("127.0.0.1", 9005));
    redisClusterConfiguration.setClusterNodes(redisNodes);
    LettuceConnectionFactory lettuceConnectionFactory =
        new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);
    lettuceConnectionFactory.afterPropertiesSet();

See in action at Redis Cluster Configuration