How to set the default timeout for a Redis connection?

In Redis, you can set the default timeout for connections by configuring the “timeout” parameter. By default, the timeout for Redis connections is unlimited, meaning there is no time limit. You can set the default timeout for connections by following the steps below:

  1. In the Redis configuration file (redis.conf), locate and adjust the value of the timeout parameter. For example:
  2. 300 seconds timeout
  3. This will set the timeout for the connection to 300 seconds.
  4. In the code for establishing a Redis connection, use the methods provided by the corresponding Redis client library to set the timeout. The specific method of setting the timeout depends on the Redis client library being used. Here are some examples of commonly used Redis client libraries.
  5. Python: Use the socket_timeout parameter to set a timeout duration. For example:
    import redis

    r = redis.Redis(host=’localhost’, port=6379, socket_timeout=5)

  6. Java (Jedis): Set a timeout value using the timeout parameter. For example:
    import redis.clients.jedis.Jedis;

    Jedis jedis = new Jedis(“localhost”, 6379, 5000);

  7. Node.js (ioredis): Use the connectTimeout parameter to set the timeout duration. For example:
    const Redis = require(‘ioredis’);

    const redis = new Redis({
    host: ‘localhost’,
    port: 6379,
    connectTimeout: 5000,
    });

By the above method, the default timeout for Redis connections can be set based on specific needs.

bannerAds