<p>Do you know (if it is possible) how to reserve threads/memory for a specific endpoint in a spring boot microservice?</p>
<p>I've one <strong>microservice</strong> that accepts <strong>HTTP Requests via Spring MVC</strong>, and those requests trigger http calls to <strong>3rd system</strong>, which sometimes is partially degraded, and it responds very slow. I can't reduce the timeout time because there are some calls that are very slow by nature.</p>
<p>I've the spring-boot-actuator <strong><code>/health</code></strong> endpoint enabled and I use it like a container <strong><code>livenessProbe</code></strong> in a <strong>kubernetes cluster</strong>. Sometimes, when the 3rd system is degraded, the microservice doesn't respond to <code>/health</code> endpoint and kubernetes restarts my service.</p>
<p>This is because I'm using a <strong>RestTemplate to make HTTP calls</strong>, so I'm continuously creating new threads, and JVM starts to have problems with the memory.</p>
<p>I have thought about some solutions:</p>
<ol>
<li><p>Implement a high availability “/health” endpoint, reserve threads, or something like that.</p>
</li>
<li><p>Use an async http client.</p>
</li>
<li><p>Implement a Circuit Breaker.</p>
</li>
<li><p>Configure custom timeouts per 3rd endpoint that I'm using.</p>
</li>
<li><p>Create other small service (golang) and deploy it in the same pod. This service is going to process the liveness probe.</p>
</li>
<li><p>Migrate/Refactor services to small services, and maybe with other framework/languages like Vert.x, go, etc.</p>
</li>
</ol>
<p>What do you think?</p>

Miguel Fernández
<p>The actuator health endpoint is very convenient with Spring boot - almost too convenient in this context as it does deeper health checks than you necessarily want in a liveness probe. For readiness you want to do deeper checks but not liveness. The idea is that if the Pod is overwhelmed for a bit and fails readiness then it will be withdrawn from the load balancing and get a breather. But if it fails liveness it will be restarted. So you want only minimal checks in liveness (<a href="https://stackoverflow.com/questions/53873153/should-health-checks-call-other-app-health-checks">Should Health Checks call other App Health Checks</a>). By using actuator health for both there is no way for your busy Pods to get a breather as they get killed first. And kubernetes is periodically calling the http endpoint in performing both probes, which contributes further to your thread usage problem (do consider the periodSeconds on the probes). </p>

<p>For your case you could define a liveness command and not an http probe - <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/#define-a-liveness-command" rel="noreferrer">https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/#define-a-liveness-command</a>. The command could just check that the Java process is running (so kinda similar to your go-based probe suggestion). </p>

<p>For many cases using the actuator for liveness would be fine (think apps that hit a different constraint before threads, which would be your case if you went async/non-blocking with the reactive stack). Yours is one where it can cause problems - the actuator's probing of availability for dependencies like message brokers can be another where you get excessive restarts (in that case on first deploy). </p>
