title: Understanding Spring Cloud Eureka Server self preservation and renew threshold
body: <p>I am new to developing microservices, although I have been researching about it for a while, reading both Spring's docs and Netflix's.</p>
  
  <p>I have started a simple project <a href="https://github.com/codependent/storyteller-microservices" rel="noreferrer">available on Github</a>. It is basically a Eureka server (Archimedes) and three Eureka client microservices (one public API and two private). Check github's readme for a detailed description.</p>
  
  <p>The point is that when everything is running I would like that if one of the private microservices is killed, the Eureka server realizes and removes it from the registry.</p>
  
  <p>I <a href="https://stackoverflow.com/questions/32616329/eureka-never-unregisters-a-service">found this question on Stackoverflow</a>, and the solution passes by using <code>enableSelfPreservation:false</code> in the Eureka Server config. Doing this after a while the killed service disappears as expected.</p>
  
  <p>However I can see the following message:</p>
  
  <blockquote>
    <p>THE SELF PRESERVATION MODE IS TURNED OFF.THIS MAY NOT PROTECT INSTANCE
    EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.</p>
  </blockquote>
  
  <p><strong>1. What is the purpose of the self preservation? The <a href="https://github.com/Netflix/eureka/wiki/Understanding-Eureka-Peer-to-Peer-Communication" rel="noreferrer">doc</a> states that with self preservation on <em>"clients can get the instances that do not exist anymore"</em>. So when is it advisable to have it on/off?</strong></p>
  
  <p>Furthermore, when self preservation is on, you may get an outstanding message in the Eureka Server console warning:</p>
  
  <blockquote>
    <p>EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN
    THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE
    INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.</p>
  </blockquote>
  
  <p>Now, going on with the Spring Eureka Console.</p>
  
  <pre><code>Lease expiration enabled    true/false
  Renews threshold    5
  Renews (last min)   4
  </code></pre>
  
  <p>I have come across a weird behaviour of the threshold count: when I start the Eureka Server alone, the threshold is 1.</p>
  
  <p><strong>2. I have a single Eureka server and is configured with <code>registerWithEureka: false</code> to prevent it from registering on another server. Then, why does it show up in the threshold count?</strong></p>
  
  <p><strong>3. For every client I start the threshold count increases by +2. I guess it is because they send 2 renew messages per min, am I right?</strong></p>
  
  <p><strong>4. The Eureka server never sends a renew so the last min renews is always below the threshold. Is this normal?</strong></p>
  
  <pre><code>renew threshold 5
  rewnews last min: (client1) +2 + (client2) +2 -&gt; 4
  </code></pre>
  
  <p><strong>Server cfg:</strong></p>
  
  <pre><code>server:
    port: ${PORT:8761}
  
  eureka:
    instance:
      hostname: localhost
    client:
      registerWithEureka: false
      fetchRegistry: false
      serviceUrl:
        defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    server:
      enableSelfPreservation: false
  #   waitTimeInMsWhenSyncEmpty: 0
  </code></pre>
  
  <p><strong>Client 1 cfg:</strong></p>
  
  <pre><code>spring:
    application:
      name: random-image-microservice
  
  server:
    port: 9999
  
  eureka:
    client:
      serviceUrl:
        defaultZone: http://localhost:8761/eureka/
      healthcheck:
        enabled: true
  </code></pre>