Chaithra Rai
Chaithra Rai

Reputation: 37

CROSSSLOT Keys Error with Redis Cluster-Mode Enabled in springboot using Lettuce Configuration

We just moved from redis standalone to cluster and we came across the following Exception: RedisSystemException: Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: CROSSSLOT Keys in request don't hash to the same slot. On Further digging into it I found this issue comes into picture when Keys in request don't hash to the same slot during a redisTemplate.opsForZSet().intersectAndStore / redisTemplate.opsForZSet().differenceAndStore and similar union intersection operations.

I came across this concept called hashtags but I am not figure out a way to resolve it through my code

     @LogMethodCall
  public String getValueByKey(String key) {
    if (BooleanUtils.isTrue(keyExists(key))) {
      return redisTemplate.opsForValue().get(key+"{someconstantstring}");
    }
    return null;
  }

  @LogMethodCall
  public void save(String key, String value) {
    if (StringUtils.isNotEmpty(key)) {
      redisTemplate.opsForValue().set(key+"{someconstantstring}", value);
    }
  }

  @LogMethodCall
  public void saveWithExpiryInMinutes(String key, String value, long expiryTime) {
    if (StringUtils.isNotEmpty(key)) {
      redisTemplate.opsForValue().set(key+"someconstantstring", value, expiryTime, TimeUnit.MINUTES);
    }
  }

  @LogMethodCall
  public void saveWithExpiryInSeconds(String key, String value, long expiryTime) {
    if (StringUtils.isNotEmpty(key)) {
      redisTemplate.opsForValue().set(key+"{someconstantstring}", value, expiryTime, TimeUnit.SECONDS);
    }
  }

Have I understood the concept wrong ?Any inputs with code snippet examples that might be working will probably help me get through this?

Upvotes: 0

Views: 3303

Answers (1)

Chaithra Rai
Chaithra Rai

Reputation: 37

I was trying to pass different keys for one redis key:value. The catch is to pass the same key each time rather than risk changing it. Something like For example

redisTemplate.opsForValue().set("{someconstantstring}", value;
    

This blog provides a good insight on the same: https://hackernoon.com/resolving-the-crossslot-keys-error-with-redis-cluster-mode-enabled

Upvotes: 0

Related Questions