Reputation: 391
I’m facing an issue with Redis when checking if a key exists using CrudRepository.existsById(). Under normal conditions, everything works fine, and the key exists in Redis, but the problem arises under high request load or concurrent access.
Redis Service:
@Service
public class RedisService {
private final SessionRedisDataRepository sessionRedisDataRepository;
public RedisService(SessionRedisDataRepository sessionRedisDataRepository) {
this.sessionRedisDataRepository = sessionRedisDataRepository;
}
public boolean isKeyExists(String key) {
return sessionRedisDataRepository.existsById(key);
}
}
Redis Repository:
public interface SessionRedisDataRepository extends CrudRepository<SessionRedisData, String> {
}
Session Data Class:
@RedisHash(value = "SessionData", timeToLive = 3600L)
public class SessionRedisData implements Serializable {
@Id
private String sessionId;
}
When multiple requests come in at the same time or under high load, the isKeyExists method sometimes returns false even though the key exists in Redis.
What works: Under normal load, the key exists, and existsById returns true.
What fails: When multiple concurrent requests happen, the same key might be checked, and existsById returns false even though the key is in Redis.
What could be the underlying cause of this issue with existsById in this case and what are the fixes?
Upvotes: 0
Views: 30