Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Jedis timeout

Tags:

java

redis

jedis

I'm having problems completing an .hgetall(), here's what I've tried:

Jedis jedis = new Jedis(REDIS_MASTER_NODE);
jedis.connect();
jedis.configSet("timeout", "30");

Map<String, String> alreadyStored = jedis.hgetAll(redisTargetHash);

and here's what I get:

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
    at redis.clients.jedis.Protocol.process(Protocol.java:79)
    at redis.clients.jedis.Protocol.read(Protocol.java:131)
    at redis.clients.jedis.Connection.getBinaryMultiBulkReply(Connection.java:199)
    at redis.clients.jedis.Jedis.hgetAll(Jedis.java:851)

This solved the issue:

Jedis jedis = new Jedis(REDIS_MASTER_NODE, 6379, 1800);
like image 216
dranxo Avatar asked Feb 21 '13 02:02

dranxo


People also ask

What is config timeout 60 in Jedis?

Doing CONFIG SET timeout 60, means that Redis will close idle client connections after 60 seconds. That's why you get the exception in Jedis. Whats the unit of timeout? As in, is it in seconds or milliseconds? @xetorthio can you tell me how I can specify query timeout in jedis ?

How to configure the timeout in Redis?

Configure the timeout based on what the service can tolerate. For example, if you need to request Redis twice in an HTTP request and the maximum timeout of an HTTP request is 10s, it is recommended that you set the timeout in Redis to 5s. This prevents service interruption if faults occur due to a long timeout duration or no timeout duration.

What happens if the timeout parameter is not configured?

If the timeout parameter is not configured and there is a faulty node, client connections will be blocked. Configure the timeout based on what the service can tolerate. For example, if you need to request Redis twice in an HTTP request and the maximum timeout of an HTTP request is 10s, it is recommended that you set the timeout in Redis to 5s.

What are the configuration parameters of jedispool?

The configuration parameters of JedisPool are mostly assigned by their counterparts in JedisPoolConfig. MaxActive: controls how many instances of jedis can be assigned to a pool, and gets them by pool.getresource ().


1 Answers

If what you want to do is set Jedis connection timeout, you should do it using the special constructor made for that:

public Jedis(final String host, final int port, final int timeout)

What you are doing is setting the timeout on Redis settings from Jedis. Doing CONFIG SET timeout 60, means that Redis will close idle client connections after 60 seconds. That's why you get the exception in Jedis.

like image 130
xetorthio Avatar answered Oct 25 '22 02:10

xetorthio