0
0
Spring Bootframework~8 mins

Custom actuator endpoints in Spring Boot - Performance & Optimization

Choose your learning style9 modes available
Performance: Custom actuator endpoints
MEDIUM IMPACT
This affects the server response time and the size of monitoring data sent to clients, impacting backend performance and network load.
Creating a custom actuator endpoint to expose application metrics
Spring Boot
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import java.util.concurrent.atomic.AtomicReference;
import java.util.Map;

@Endpoint(id = "cachedMetrics")
public class CachedMetricsEndpoint {
    private final AtomicReference<Map<String, Object>> cache = new AtomicReference<>();

    public CachedMetricsEndpoint() {
        // Precompute or update cache asynchronously
        cache.set(Map.of("data", "precomputed result"));
    }

    @ReadOperation
    public Map<String, Object> metrics() {
        return cache.get();
    }
}
Precomputes data or caches results to avoid expensive operations during requests, ensuring fast response.
📈 Performance GainResponds instantly without blocking, improving server throughput and reducing latency
Creating a custom actuator endpoint to expose application metrics
Spring Boot
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import java.util.Map;

@Endpoint(id = "heavyMetrics")
public class HeavyMetricsEndpoint {
    @ReadOperation
    public Map<String, Object> metrics() throws InterruptedException {
        // Simulate expensive operation
        Thread.sleep(5000);
        return Map.of("data", "heavy computation result");
    }
}
The endpoint performs a blocking 5-second operation on each request, delaying response and increasing server load.
📉 Performance CostBlocks response for 5 seconds per request, increasing server latency and reducing throughput
Performance Comparison
PatternServer ProcessingResponse DelayNetwork PayloadVerdict
Blocking heavy computation in endpointHigh CPU and blocking5 seconds delaySmall JSON[X] Bad
Cached or precomputed endpoint dataMinimal CPU, non-blockingInstant responseSmall JSON[OK] Good
Rendering Pipeline
Custom actuator endpoints run on the server and affect backend processing time before the browser receives data. Slow endpoints delay the server response, increasing time to first byte and overall load time.
Server Processing
Network Transfer
⚠️ BottleneckServer Processing when endpoint logic is expensive or blocking
Optimization Tips
1Avoid blocking or long-running operations inside custom actuator endpoints.
2Use caching or asynchronous precomputation to serve data quickly.
3Keep JSON payloads small to reduce network transfer time.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of a custom actuator endpoint that performs heavy computation on each request?
AIt causes layout shifts on the page
BIt increases browser rendering time
CIt increases server response time and delays client data reception
DIt reduces network bandwidth usage
DevTools: Network
How to check: Open browser DevTools, go to Network tab, trigger the actuator endpoint request, and observe the response time and size.
What to look for: Look for long waiting times (TTFB) indicating slow server processing and check payload size for network impact.