title: Spring Boot Redis: Distributed Caching of Objects from Backend Services for parallel consumer requests for the same Object
body: <p>i didn't know how to explain my challenge better in the title.</p>
  
  <p>The current setup is, that I'm using Spring Rest Service as a "middleware layer" in order to transform the backend's data, which is a huge response, in a more "friendly" way.</p>
  
  <p>This is the for example the structure of the backend:</p>
  
  <pre><code>public class Customer
  {
     private String id;
     private String bankAccount;
     private String customerName;
  }
  </code></pre>
  
  <p>The middleware is currently caching the response from the backend in redis.
  It also has following endpoints:</p>
  
  <pre><code>public class ServiceController
  {
     getBankAccountById(String id);
     getCustomerNameById(String id);
     getCustomerObjectById(String id);
  }
  </code></pre>
  
  <p>All of the requests are producing a backend call for the customer object and cache it in redis, if no object is already present.</p>
  
  <p>But in a multi-threaded environment and especially in an "cloud" / multi-instance environment if the consumer is doing n-requests (#1 getBankAccountById() #2 getCustomerNameById(), etc. in a parallel manner - exact same millisecond), that only one single request is fired against the "real" backend?</p>
  
  <p>My Goal is something like putting a marker into redis that there will be an object of the type customer for a given id is in the cache in the near future, which results in blocking all other threads / threads of other instances in order to reduce backend calls.</p>
  
  <p>My question, is there a simple or maybe an out of the box solution for that?</p>
  
  <p>The only thing I found was the docs of Spring Boot and @Cacheable, which is not feasible, since it synchronizes the calls for a given Id within the same application, not service wide in a clustered environment.</p>
  
  <p>And in addition to this, the backend is really slow (5-10 seconds per call), that the @Cacheable annotation is already passed by since it's starts writing to cache when the actual @Cachable Object is returned.</p>
  
  <p>Thanks in advance,
  Cheers Alex</p>
  
  <p>//Edit:
  I meant that @Cachable(sync=true) is only affecting the instance level and not a distributed level. Therefore it doesn't really make sense here from my understanding.</p>