TL;DR
Jedis instances are not thread-safe, calls made to a Jedis instance across multiple threads will result in these types of errors. Try switching to use a JedisPool
to manage the creation of connections.
Explanation
I was seeing these exact same exceptions and while trying to figure out the problem I came across a similar issue on the Jedis project explaining Jedis is not thread-safe.
Since I found this question when searching for this same exception I will share some details of my particular case so it may help future readers.
In my particular case I calling the SCAN
command using RedisTemplate
from Spring Data Redis. I was making calls to the resulting Cursor
outside of the connections lifecycle.
My original code looked like this.
public Long size() {
Cursor<byte[]> scan = redisTemplate.execute((RedisConnection connection) -> connection.scan(ScanOptions.scanOptions().match(prefix + "*").build()));
AtomicLong count = new AtomicLong();
scan.forEachRemaining(bytes -> count.getAndIncrement());
return count.get();
}
After a minor change I found the correct way to do this is by simply moving the cursor interactions inside the execute lambda.
public Long size() {
return redisTemplate.execute((RedisConnection connection) -> {
Cursor<byte[]> scan = connection.scan(ScanOptions.scanOptions().match(prefix + "*").build());
AtomicLong count = new AtomicLong();
scan.forEachRemaining(bytes -> count.getAndIncrement());
return count.get();
});
}
[B
is abyte[]
. This isn't defined by OP. See this: docs.oracle.com/javase/7/docs/api/java/lang/…