title: Spring data redis concurrency issue
body: <p>I have a big issue when using multiple threads with spring-redis-data and it's so easy to reproduce that I think I've been missing something trivial.</p>
  
  <h1>Straight to the point</h1>
  
  <p>If I query a CrudRepository while doing save operations, it sometimes (up to 60%) doesn't find the record on Redis.</p>
  
  <h1>The environment</h1>
  
  <ul>
  <li><a href="https://github.com/hector6371/spring-data-redis" rel="noreferrer">A simple spring boot project (2.3.0.RELEASE)</a></li>
  <li><a href="https://registry.hub.docker.com/_/redis" rel="noreferrer">A docker running redis</a> </li>
  </ul>
  
  <h1>The code</h1>
  
  <p>Despite the full code could be found in the link above, this are the main components:</p>
  
  <h2>CrudRepository</h2>
  
  <pre><code>@Repository
  public interface MyEntityRepository extends CrudRepository&lt;MyEntity, Integer&gt; {
  
  }
  </code></pre>
  
  <h2>Entity</h2>
  
  <pre><code>@RedisHash("my-entity")
  public class MyEntity implements Serializable {
  
      @Id
      private int id1;
  
      private double attribute1;
      private String attribute2;
      private String attribute3;
  </code></pre>
  
  <h2>Controller</h2>
  
  <pre><code>    @GetMapping( "/my-endpoint")
      public ResponseEntity&lt;?&gt; myEndpoint () {
  
          MyEntity myEntity = new MyEntity();
          myEntity.setAttribute1(0.7);
          myEntity.setAttribute2("attr2");
          myEntity.setAttribute3("attr3");
          myEntity.setId1(1);
  
          myEntityRepository.save(myEntity);//create it in redis
  
          logger.info("STARTED");
  
          for (int i = 0; i &lt; 100; i++){
              new Thread(){
                  @Override
                  public void run() {
                      super.run();
  
                      myEntity.setAttribute1(Math.random());
  
                      myEntityRepository.save(myEntity); //updating the entity
  
                      Optional&lt;MyEntity&gt; optionalMyEntity = myEntityRepository.findById(1);
                      if (optionalMyEntity.isPresent()) {
                          logger.info("found");
                      }else{
                          logger.warning("NOT FOUND");
                      }
                  }
              }.start();
  
          }
  
          return ResponseEntity.noContent().build();
      }
  
  </code></pre>
  
  <h1>The result</h1>
  
  <pre><code>2020-05-26 07:52:53.769  INFO 30655 --- [nio-8080-exec-2] my-controller-logger                     : STARTED
  2020-05-26 07:52:53.795  INFO 30655 --- [     Thread-168] my-controller-logger                     : found
  2020-05-26 07:52:53.798  WARN 30655 --- [     Thread-174] my-controller-logger                     : NOT FOUND
  2020-05-26 07:52:53.798  WARN 30655 --- [     Thread-173] my-controller-logger                     : NOT FOUND
  2020-05-26 07:52:53.806  INFO 30655 --- [     Thread-170] my-controller-logger                     : found
  2020-05-26 07:52:53.806  WARN 30655 --- [     Thread-172] my-controller-logger                     : NOT FOUND
  2020-05-26 07:52:53.812  WARN 30655 --- [     Thread-175] my-controller-logger                     : NOT FOUND
  2020-05-26 07:52:53.814  WARN 30655 --- [     Thread-176] my-controller-logger                     : NOT FOUND
  2020-05-26 07:52:53.819  WARN 30655 --- [     Thread-169] my-controller-logger                     : NOT FOUND
  2020-05-26 07:52:53.826  INFO 30655 --- [     Thread-171] my-controller-logger                     : found
  2020-05-26 07:52:53.829  INFO 30655 --- [     Thread-177] my-controller-logger                     : found
  </code></pre>
  
  <p>So simply with 10 threads, 6 of them are not finding the result in db.</p>
  
  <h1>Replacing with spring data redis</h1>
  
  <p>As mentioned <a href="https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#redis.repositories.anatomy.replace" rel="noreferrer">here</a> replacing in redis with spring data redis contains at least, 9 operations.</p>
  
  <h1>First conclusion</h1>
  
  <p>So, as to replace a value in redis, it has to remove the hash, the indexes and then adding the new hash and the new indexes again, maybe a thread is in the middle of doing this operations while other thread tries to find the value by index and this index has not beeing added yet.</p>
  
  <h1>Second conclusion</h1>
  
  <p>I think it's nearly impossible that spring data with data-redis has such a bug, so I'm wondering what I'm not understanding of data-redis or redis. As redis has concurrency I think something different may be happening, but with the provided example it seems like that...</p>
  
  <p>Thank you in advance to all of you</p>