1

I have a use case to identify expired tokens stored in cache, so I implemented a method in the following way -

private Boolean isTokenExpired(long programId,String token,Jwt jwt){ try { if(!jedis.isConnected() || generalConfigHelper.getInt(REDIS_DB_INDEX) != jedis.getDB()){ jedis=redisHelper.getConnection(); jedis.select(generalConfigHelper.getInt(REDIS_DB_INDEX)); } Claims claims = (Claims) jwt.getBody(); //redis keys String tokenCacheKey=programId+":Tokens:"+token; String expiryTimeKey=programId+":InvalidateTokensBefore:"+claims.get("user_id"); List<String> values=jedis.mget(tokenCacheKey,expiryTimeKey); if (values.get(0)!=null){ return true; }else if (values.get(1)!=null){ log.info("redis: exp value:"+values.get(1)); long exp=Long.parseLong(claims.get("exp_ms")+""); return exp<Long.parseLong(values.get(1)+""); } } catch (Exception ex){ log.info("Error while reading from redis:"+ex); } finally { log.debug("Returning jedis instance to the pool"); redisHelper.closeConnection(jedis); } return false; } 

The above code intially checks if client is connected to the resource pool and proceeds with select and mget commands, during the execution of these following commands I'm facing the following exceptions - SocketException: Broken pipe (Write failed) and SocketException: Socket is closed.

My Jedis Pool Configuration - maxTotal - 512 maxIdle - 32 minIdle - 8 timeout - 10000 ms

Upon researching about these exceptions I found these GitHub issues which were highlighting these exceptions - https://github.com/mozilla/gcp-ingestion/issues/451 https://github.com/redis/jedis/issues/185

The issues highlight about how the Redis Client timesout and breaks the connection therefore resulting in the following issues and made some recommendations for the timeout, I have followed the recommended config and still seem to face issues.

1
  • Which recommended config did you follow? Commented Apr 18, 2024 at 5:46

1 Answer 1

0

May I suggest to have a redisHelper.getConnectionPool() and not use a global jedis variable?

private Boolean isTokenExpired(long programId, String token, Jwt jwt) { try (Jedis jedis = redisHelper.getConnectionPool()) { jedis.select(generalConfigHelper.getInt(REDIS_DB_INDEX)); ... ... } catch (Exception ex) { log.info("Error while reading from redis:", ex); // not sure which logger // you're using but you should use this option if possible } return false; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.