title: Spring Data Redis JedisConnectionException: Unexpected end of stream
body: <p>Redis 3.0.5<br>
  Spring Data Redis 1.3.6<br>
  jedis 2.6.3<br>
  - Our is web application which receives data from redis over pub/sub.<br>
  - Also performs read/write of data on redis as key/value pairs.<br>
  - read/write happens on listener thread, independent monitoring thread and http request threads.<br>
  - We have used a same connection factory for Listener and redis template<br>
  - Our redis server has "timeout=30" configured</p>
  
  <pre><code>&lt;bean id="jedisConnectionFactory"
      class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"&gt;
      &lt;property name="hostName" value="nnnn"&gt;&lt;/property&gt;
      &lt;property name="port" value="nnnn"&gt;&lt;/property&gt;
      &lt;property name="password" value="****"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"&gt;
      &lt;property name="connectionFactory" ref="jedisConnectionFactory" /&gt;
  &lt;/bean&gt;
  &lt;bean id="redisContainer"
      class="org.springframework.data.redis.listener.RedisMessageListenerContainer"&gt;
      &lt;property name="connectionFactory" ref="jedisConnectionFactory" /&gt;
      &lt;property name="messageListeners"&gt;
          &lt;map&gt;
              &lt;entry key-ref="messageListener"&gt;
                  &lt;bean class="org.springframework.data.redis.listener.ChannelTopic"&gt;
                      &lt;constructor-arg value="topic_name" /&gt;
                  &lt;/bean&gt;
              &lt;/entry&gt;
          &lt;/map&gt;
      &lt;/property&gt;
      &lt;property name="taskExecutor" ref="redisTaskExecutor" /&gt;
      &lt;property name="subscriptionExecutor" ref="redisSubTaskExecutor" /&gt;
  &lt;/bean&gt;
  &lt;bean id="redisTaskExecutor"
      class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"&gt;
      &lt;property name="threadNamePrefix" value="RedisListenerThread"&gt;&lt;/property&gt;
      &lt;property name="corePoolSize" value="1" /&gt;
      &lt;property name="maxPoolSize" value="1" /&gt;
  &lt;/bean&gt;
  &lt;bean id="redisSubTaskExecutor"
      class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"&gt;
      &lt;property name="threadNamePrefix" value="RedisSubscribeThread"&gt;&lt;/property&gt;
      &lt;property name="corePoolSize" value="1" /&gt;
      &lt;property name="maxPoolSize" value="1" /&gt;
  &lt;/bean&gt;
  &lt;bean id="messageListener"
      class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter"&gt;
      &lt;constructor-arg index="0"&gt;
          &lt;bean class="my.data.Receiver" /&gt;
      &lt;/constructor-arg&gt;
      &lt;constructor-arg index="1"&gt;&lt;value&gt;receive&lt;/value&gt;&lt;/constructor-arg&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>occasionally we face below problem on production during data read.</p>
  
  <blockquote>
    <p>org.springframework.data.redis.RedisConnectionFailureException: Unexpected end of stream.; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream.
        at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:47)
        at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:36)
        at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:37)
        at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:37)
        at org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:182)
        at org.springframework.data.redis.connection.jedis.JedisConnection.get(JedisConnection.java:1087)
        at org.springframework.data.redis.connection.DefaultStringRedisConnection.get(DefaultStringRedisConnection.java:276)
        at org.springframework.data.redis.core.DefaultValueOperations$1.inRedis(DefaultValueOperations.java:46)
        at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:50)
        at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:190)
        at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:152)
        at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:84)
        at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:43)</p>
  </blockquote>
  
  <p>I have read other threads talking of using single thread for read/write. But in our case it is difficult to use single thread. Also as per RedisTemplate documentation, it is thread safe.
  The problem is occasional and we are unable to reproduce in any of dev/testing/uat environment. Thus unable to find exact cause of the same.
  What have we done wrong?</p>