title: Eureka Ribbon LoadBalancer Cache update delay
body: <p>I am setting up a micro service based application, where Aggregation layer / API gateway makes calls to micro services. Eureka is used for service discovery and Ribbon for providing a load balancing RestTemplate.</p>
  
  <p>Postman calls Aggregation--> Aggregation calls Micro service using Eureka/Ribbon/RestTemplate.</p>
  
  <p>I have 4 instances of one micro services type running on my machine on 4 different ports. Hitting the same REST endpoint repeatedly Postman causes the requests to get load balanced properly in a round robin fashion.</p>
  
  <p>When I stop one of the micro service instances the service is deregistered from Eureka, but the LoadBalancer still sends requests to the dead service and the call fails.</p>
  
  <p>Below is my code:</p>
  
  <p><strong>Aggregation:</strong></p>
  
  <pre><code> @Configuration
      @ComponentScan(basePackages = {"com.mycompany.aggregator"})
      @EnableAutoConfiguration
      @EnableEurekaClient
      public class AggregatorApplication {
  
          public static void main(String[] args) {
              SpringApplication.run(AggregatorApplication.class, args);
          }
      }
  
  **Configuration:**
  @Configuration
  public class AggregatorConfig {
  
      @Bean
      @LoadBalanced
      public RestTemplate restTemplate() {
          return new RestTemplate();
      }
  }
  
  @Configuration
  @RibbonClient(name="microservice", configuration = FooConfig.class)
  public class TestConfig {
  }
  </code></pre>
  
  <p>//FooConfig is excluded from component-scan</p>
  
  <pre><code>@Configuration
  public class FooConfig {
  
      @Bean
      public IPing ribbonPing(IClientConfig config) {
          return new NIWSDiscoveryPing();
      }
  
      @Bean
      public IRule ribbonRule(IClientConfig config) {
          return new AvailabilityFilteringRule();
      }
  }
  </code></pre>
  
  <p>restTemplate call:</p>
  
  <pre><code>ResponseEntity&lt;Object&gt; responseEntity = restTemplate.getForEntity(myUrl, Object.class);
  </code></pre>
  
  <p>Properties:</p>
  
  <pre><code>spring.application.name=aggregator
  server.contextPath=/ott
  server.port = 8090
  my.url=http://microservice
  eureka.instance.leaseRenewalIntervalInSeconds=1
  eureka.instance.leaseExpirationDurationInSeconds=2
  </code></pre>
  
  <p><strong>MicroService Code:</strong></p>
  
  <pre><code>@SpringBootApplication
  @EnableEurekaClient
  public class MicroServiceApplication
  </code></pre>
  
  <p>Properties:</p>
  
  <pre><code>spring.application.name=microservice
  server.contextPath=/ott
  server.port = 9000
  eureka.instance.leaseRenewalIntervalInSeconds=1
  eureka.instance.leaseExpirationDurationInSeconds=2
  </code></pre>
  
  <p><strong>Eureka Server:</strong></p>
  
  <pre><code>@SpringBootApplication
  @EnableEurekaServer
  public class EurekaserverApplication {
  </code></pre>
  
  <p>Properties:</p>
  
  <pre><code>server.port=8761
  
  eureka.server.enableSelfPreservation=false
  eureka.client.registerWithEureka=false
  eureka.client.fetchRegistry=false
  
  logging.level.com.netflix.eureka=OFF
  logging.level.com.netflix.discovery=OFF
  </code></pre>