springspring-bootredisspring-data-redis

How to resolve CROSSSLOT key error on Redis Cluster using Spring Boot and RedisHash with Multiple Indexes


Im trying to solve the CROSSSLOT Keys in request don't hash to the same slot when using @RedisHash on an entity class having more then one @Indexed on varaiables.

Is there any way to store the hashing in same redis slot?

Here my Entity Class for Refrence.

@RedisHash(value = "corp:TransactionDetails", timeToLive = Constants.TIME_TO_LIVE_REDIS)
public class TransactionDetailsEntity {
    private static final long serialVersionUID = 1L;
    @Id
    @Indexed
    private UUID transactionId;
    @Indexed
    private String sessionId;
    @Indexed
    private String referenceNumber;
    private String fromAccountNumber;
    private String remainingBalance;
    private String fromAccountType;
    private String beneficiaryName;
    private String toAccountNumber;
    private String toAccountBalance;
}

@Repository
public interface TransactionDetailsRepository extends CrudRepository<TransactionDetailsEntity, UUID> {
    Optional<TransactionDetailsEntity> findBySessionIdAndTransactionId(String sessionId, UUID uuid);

    Optional<TransactionDetailsEntity> findByReferenceNumber(String referenceNumber);

}

I was facing below error.

Caused by: io.lettuce.core.RedisCommandExecutionException: CROSSSLOT Keys in request don't hash to the same slot (context='', command='sinter', original-slot='11760', wrong-slot='9232', first-key='corp:TransactionDetails:sessionId:aeb30c5a-a8d4-433e-9bd6-a2078b7499f7', violating-key='corp:TransactionDetails:transactionId:299b21fc-1538-43a3-8a0f-b515dec4241d')
    at io.lettuce.core.internal.ExceptionFactory.createExecutionException(ExceptionFactory.java:147)

Solution

  • Perhaps one could use Hash tags:

    Hash tags There is an exception for the computation of the hash slot that is used in order to implement hash tags. Hash tags are a way to ensure that multiple keys are allocated in the same hash slot. This is used in order to implement multi-key operations in Redis Cluster.

    To implement hash tags, the hash slot for a key is computed in a slightly different way in certain conditions. If the key contains a "{...}" pattern only the substring between { and } is hashed in order to obtain the hash slot. However since it is possible that there are multiple occurrences of { or } the algorithm is well specified by the following rules:

    On the other side the spring-data-redis allows you to use Keyspaces or the existing RedisHash to include the hash tag.

    @RedisHash(value = "{corp:TransactionDetails}", timeToLive = Constants.TIME_TO_LIVE_REDIS)
    public class TransactionDetailsEntity {
        private static final long serialVersionUID = 1L;
        @Id
        @Indexed
        private UUID transactionId;
        @Indexed
        private String sessionId;
        @Indexed
        private String referenceNumber;
        private String fromAccountNumber;
        private String remainingBalance;
        private String fromAccountType;
        private String beneficiaryName;
        private String toAccountNumber;
        private String toAccountBalance;
    }