Urgent.News

What's breaking now, across thousands of outlets.

Tech

How to Implement an API Readiness Endpoint

An API readiness endpoint is a dedicated HTTP endpoint used by the surrounding infrastructure to determine whether an application can currently receive traffic. Missing or incorrect readiness handling can cause: New instances to receive traffic before startup is complete. Unavailable instances to continue receiving traffic. Healthy instances to be removed from traffic because of optional…

An API readiness endpoint is an HTTP endpoint used by infrastructure to determine if an application can receive traffic. Misconfigured readiness handling can lead to new instances receiving traffic prematurely, unavailable instances continuing to receive traffic, healthy instances being removed from traffic, and repeated checks causing overload of failing dependencies.

To implement an API readiness endpoint, track the application's state (starting, ready, or shutting down), expose a dedicated endpoint (e.g., GET /ready) that returns HTTP 200 OK when ready and HTTP 503 Service Unavailable otherwise. Configure the infrastructure to periodically call the endpoint and route traffic only to applications reporting as ready.

Create a singleton class to keep track of the application's state, with methods to get the current state and update it. Mount the readiness endpoint after lightweight observability middleware, but before authentication, validation, business logic, or middleware that may fail for unrelated reasons.

Verify the health of each dependency that, if failing, would prevent the application from serving traffic. For example, if PostgreSQL is required for request processing, return HTTP 503 Service Unavailable if PostgreSQL is unavailable. Perform lightweight dependency checks that do not cause side effects, such as executing a SELECT 1 query.

Run independent dependency checks concurrently to prevent response time accumulation and ensure the endpoint responds within the probe timeout. Apply a short timeout to each check to prevent slow or unresponsive dependencies from blocking the endpoint's response.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Thursday 15 October →