title: Spring Redis template with Jedis connection factory , Redis standalone configuration and multi-threading
body: <p>I am using Spring Redis template in a multi-threaded environment. One thread saves the data into Redis and other one (the scheduler) fetches the data from it. JedisConnectionFactory is used in redis template. Following is the code snippet for obtaining redis connection:</p>
  
  <pre><code>JedisConnectionFactory jedisConnectionFactory() {
  
      JedisConnectionFactory jedisConnectionFactory = null;
  
      try {
          RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(hostName,
                  port);
          jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration);
      } catch (RedisConnectionFailureException e) {
          LOGGER.error("Connection break with redis " + e.getMessage());
      }
  
      return jedisConnectionFactory;
  }
  
  /**
   * Redis template.
   *
   * @return the redis template
   */
  @Bean
  public RedisTemplate&lt;String, Object&gt; redisTemplate() {
      final RedisTemplate&lt;String, Object&gt; template = new RedisTemplate&lt;String, Object&gt;();
      template.setConnectionFactory(jedisConnectionFactory());
      template.setValueSerializer(new GenericToStringSerializer&lt;Object&gt;(Object.class));
      template.setEnableTransactionSupport(true);
      return template;
  }
  </code></pre>
  
  <p>The instance of redis template is obtained using constructor auto-wiring as follows:</p>
  
  <pre><code>@Autowired
  public A(RedisTemplate&lt;String, Object&gt; redisTemplate) {
      this.redisTemplate = redisTemplate;
  }
  </code></pre>
  
  <p>I am getting an exception while fetching data from Redis using "findAll()" method of redis template: </p>
  
  <blockquote>
    <p>org.springframework.data.redis.RedisConnectionFailureException: java.net.SocketException: Connection reset by peer: socket write error; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Connection reset by peer: socket write error</p>
  </blockquote>
  
  <p>Below are my findings:</p>
  
  <ol>
  <li>connection reset by peer exception occurs when TCP socket is to be "closing" and your code to not have yet been notified. (the thread for findAll has not been notified for the closed connection).</li>
  <li>Redis template is thread safe (only if connection pool is used) and handles connection management on its own. It may happen that when thread saved data into redis and closed the connection and during that only, the fetch operation occured and demanded data. In that case, the server might have issued RST command but fetch operation might have not obtained it. </li>
  <li>Jedis pool config can be used; but there are depreciated methods in this that can later cause upgradation issues.</li>
  </ol>
  
  <p>Please suggest best method to handle multi-threading with "JedisConnectionFactory", "RedisStandAloneConfiguration" and "RedisTemplate".</p>