WebSphere Tutorial · WebSphere Liberty · Lesson 11 of 23

MicroProfile on Liberty

Cloud-native APIs on Liberty: Config, Health, Metrics, Telemetry, Fault Tolerance, REST Client and OpenAPI.

Members can save this page, take notes and track progress. See membership · Sign in

What MicroProfile adds

Feature Use it for
mpConfig Read settings from environment variables, files and system properties with @ConfigProperty
mpHealth /health/live and /health/ready endpoints for Kubernetes probes
mpMetrics Metrics in Prometheus format at /metrics
mpTelemetry OpenTelemetry traces, metrics and logs
mpFaultTolerance @Timeout, @Retry, @CircuitBreaker, @Bulkhead, @Fallback
mpRestClient Type-safe HTTP clients from an interface
mpOpenAPI API documentation generated at /openapi
@RegisterRestClient(configKey = "inventory")
public interface InventoryClient {
    @GET @Path("/stock/{sku}")
    @Timeout(2000) @Retry(maxRetries = 2) @CircuitBreaker(requestVolumeThreshold = 10, failureRatio = 0.5)
    Stock stock(@PathParam("sku") String sku);
}

Timeouts and circuit breakers prevent the classic WebSphere outage where every thread waits on a slow downstream service.

A circuit breaker opening after repeated failuresTry it yourself
class CircuitBreaker:
    def __init__(self, window=10, failure_ratio=0.5):
        self.window, self.ratio, self.results, self.open = window, failure_ratio, [], False

    def call(self, ok):
        if self.open:
            return "rejected fast (circuit open)"
        self.results = (self.results + [ok])[-self.window:]
        if len(self.results) == self.window and self.results.count(False) / self.window >= self.ratio:
            self.open = True
        return "success" if ok else "failure (after timeout)"

cb = CircuitBreaker()
outcomes = [True] * 4 + [False] * 8 + [True] * 2
for i, ok in enumerate(outcomes, 1):
    print(f"call {i:2}: {cb.call(ok)}")

Output

call  1: success
call  2: success
call  3: success
call  4: success
call  5: failure (after timeout)
call  6: failure (after timeout)
call  7: failure (after timeout)
call  8: failure (after timeout)
call  9: failure (after timeout)
call 10: failure (after timeout)
call 11: rejected fast (circuit open)
call 12: rejected fast (circuit open)
call 13: rejected fast (circuit open)
call 14: rejected fast (circuit open)

Test yourself

Which MicroProfile feature stops threads waiting forever on a slow service?

  1. Config
  2. OpenAPI
  3. Fault Tolerance (@Timeout)
  4. JWT
Show answer

Answer: Fault Tolerance (@Timeout)

@Timeout, @CircuitBreaker and @Bulkhead protect threads from slow dependencies.

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake