Arecibo is a Fastify plugin that adds two routes for the Kubernetes probes: /arecibo/readiness and /arecibo/liveness. By default, each route replies with status 200. To tell Kubernetes that the application is not ready, add readiness checks or a custom callback.
This section also gives AI coding agents the data that they need to add Arecibo to a Fastify application.
Requirements: Node.js 22 or later, and Fastify 4 or 5. Only the latest major version of Arecibo is maintained. For Fastify 2 or 3, refer to the older releases.
npm install arecibo # or: pnpm add arecibo, yarn add areciboconst fastify = require('fastify')({ logger: true })
const arecibo = require('arecibo') // ES modules: import arecibo from 'arecibo'
fastify.register(arecibo, { logLevel: 'warn' })
// Listen on 0.0.0.0, because the kubelet connects to the Pod IP address.
fastify.listen({ port: 3000, host: '0.0.0.0' })The plugin adds GET /arecibo/readiness and GET /arecibo/liveness. By default, each route replies with status 200 and the Arecibo message. For the probe settings, refer to Kubernetes probes.
All options are optional.
| Option | Type | Default | Description |
|---|---|---|---|
message |
string |
the Arecibo message | The response body (text/html) of a route that has no callback. |
readinessURL |
string |
'/arecibo/readiness' |
The path of the readiness route. |
livenessURL |
string |
'/arecibo/liveness' |
The path of the liveness route. |
readinessCallback |
RouteHandler |
replies 200 with message |
The handler of the readiness route. |
livenessCallback |
RouteHandler |
replies 200 with message |
The handler of the liveness route. |
readinessChecks |
Record<string, () => unknown> |
none | The checks of the readiness route. See Readiness checks. |
checkTimeout |
number |
500 |
The time limit of each check, in milliseconds. Use a number from 1 to 2147483647. |
checkInterval |
number |
none | If set, the checks run in the background at this interval, in milliseconds. See Checks in the background. |
shutdownDelay |
number |
none | If set, the plugin starts a graceful shutdown with this delay, in milliseconds, on SIGTERM. See Graceful shutdown. |
logLevel |
'fatal', 'error', 'warn', 'info', 'debug', 'trace', or 'silent' |
'info' |
The log level of the two routes. |
A callback is a Fastify route handler. It can send a string or an object. Fastify serializes an object as JSON. Reply with status 503 to tell Kubernetes that the application is not ready.
const fastify = require('fastify')()
const arecibo = require('arecibo')
let ready = false // Set this to true when your application can accept traffic.
fastify.register(arecibo, {
readinessCallback: (request, reply) => {
if (!ready) {
return reply.code(503).send({ status: 'not ready' })
}
return reply.send({ status: 'ready' })
},
})
fastify.listen({ port: 3000, host: '0.0.0.0' })Do not check external dependencies in livenessCallback. For example, do not check a database, a cache, or a different service. If a dependency fails, Kubernetes restarts your containers, and a restart does not repair the dependency. Put these checks in readinessCallback.
Use readinessChecks to let the plugin check your dependencies on each readiness request.
fastify.register(arecibo, {
readinessChecks: {
database: () => db.query('SELECT 1'),
queue: () => queue.isConnected(), // false means that the check failed
},
checkTimeout: 500, // optional, in milliseconds, defaults to 500
})- The checks run in parallel.
- A check fails if it throws, rejects, returns or resolves to
false, or does not settle incheckTimeoutmilliseconds. - The timeout does not stop the work of a check. A check that blocks the event loop also blocks the timeout.
- If all checks pass, the route replies with status 200 and
message. - If a check fails, the route replies with status 503 and the body
Service Unavailable. The plugin logs the name and the error withrequest.log.error. ThelogLeveloption'silent'hides this log. - Do not use
readinessCheckstogether withreadinessCallback. The registration fails. readinessChecksmust be a plain object with at least one check, and each check must be a function. If not, the registration fails.- Set
timeoutSecondsof the Kubernetes readiness probe to a value larger thancheckTimeout. - The checks do not change the liveness route.
Without checkInterval, the checks run on each readiness request. Each Pod then sends a query to your dependencies on each probe. Use checkInterval to run the checks at a fixed interval:
fastify.register(arecibo, {
readinessChecks: { database: () => db.query('SELECT 1') },
checkInterval: 5000, // in milliseconds
})- The first run starts when Fastify is ready.
fastify.ready()waits for the first run. - The readiness route replies with the result of the last run. It does not run the checks.
- The next run starts
checkIntervalmilliseconds after the previous run ends. Thus two runs never overlap. - A result can be old by up to
checkIntervalpluscheckTimeoutmilliseconds. - The plugin logs a failed check with
fastify.log.erroronly when the check starts to fail. It logs withfastify.log.infowhen the check passes again. These logs use thelogLeveloption. - The runs stop when Fastify closes.
- If you use
checkIntervalwithoutreadinessChecks, or if the value is not a number from 1 to 2147483647, the registration fails.
The plugin adds fastify.arecibo. Use it to tell Kubernetes when your application can accept traffic, for example after a cache warm-up.
await fastify.register(arecibo)
fastify.arecibo.setReady(false)
await warmUpCache()
fastify.arecibo.setReady(true)setReady(false)makes the readiness route reply with status 503 and the bodyService Unavailable. Onlytruemakes the application ready again.- The state applies before the readiness handler. Thus the plugin does not run
readinessCallback, or the checks of a request, while the application is not ready. WithcheckInterval, the checks continue to run in the background. isReady()returns the current state. The initial state is ready.- The state does not change the liveness route.
- If you register the plugin more than one time in the same context, or in a child context, all registrations share one state. Registrations in two sibling contexts have two different states.
When Kubernetes stops a Pod, it sends SIGTERM. The cluster can still send new traffic to the Pod for some seconds. Use shutdownDelay to close Fastify only after this time.
fastify.register(arecibo, {
shutdownDelay: 5000, // in milliseconds
})On SIGTERM, the plugin does these steps:
- It sets the readiness state to not ready. The readiness route replies with status 503.
- It waits
shutdownDelaymilliseconds. All other routes, and the liveness route, continue to reply as usual. - It calls
fastify.close().
- Without
shutdownDelay, the plugin does not listen toSIGTERM. - Do not add a
SIGTERMhandler that callsfastify.close()directly. If Fastify closes during the delay, the delay stops. - The process exits after
fastify.close()only if no other timer, connection, or handler keeps it open. - If you handle the signals yourself, call
fastify.arecibo.shutdown(delay). It does the same steps and returns a promise. The default delay is 5000 ms. A second call returns the same promise.
- The plugin ignores the
prefixoption ofregister, because it uses fastify-plugin. To change the paths, usereadinessURLandlivenessURL. - Global hooks also apply to the probe routes. For example, an authentication
onRequesthook can reject the probes. Make such a hook skip the probe URLs. - The plugin ignores
messagefor a route that has a callback. - The routes also answer
HEADrequests. - With the default
logLevel('info'), an enabled Fastify logger logs each probe request. Use'warn'to stop these request logs. - If a callback throws an error, the default Fastify error handler replies with status 500. The response body contains the error message. Do not put secrets in error messages.
import arecibo from 'arecibo'This import needs esModuleInterop, allowSyntheticDefaultImports, or "module": "nodenext". For CommonJS output without these settings, use import arecibo = require('arecibo'). Do not use import * as arecibo from 'arecibo'. With esModuleInterop or nodenext, register does not accept that value.
Add these fields to the container in your Kubernetes manifest:
ports:
- name: http
containerPort: 3000
startupProbe:
httpGet:
path: /arecibo/liveness # the startup probe and the liveness probe use the same route
port: http
periodSeconds: 5
failureThreshold: 30
livenessProbe:
httpGet:
path: /arecibo/liveness # do not check external dependencies here
port: http
periodSeconds: 15
readinessProbe:
httpGet:
path: /arecibo/readiness
port: http
periodSeconds: 5- In this example, the application has 150 seconds (30 x 5) to start. Kubernetes does not run the other probes until the startup probe succeeds.
- To give the cluster time to stop new traffic to the Pod, use the
shutdownDelayoption. Then you do not need apreStophook. - Set
terminationGracePeriodSecondsof the Pod to a value longer thanshutdownDelayplus the shutdown time of your application. The default value is 30 seconds.
This repo uses Semantic Release with Conventional Commits. The type of the commit message sets the type of the release:
feat: new feature ---> 1.x.0
fix: fix a bug ---> 1.0.x
To mark a breaking change, add a ! after the type (for example, feat!:) or add a BREAKING CHANGE: footer to the commit message. Each one starts a major release.
feat: remove an option
BREAKING CHANGE: the option no longer exists.
- Fastify
- Configure Liveness and Readiness Probes
- Kubernetes Health Checks with Readiness and Liveness Probes (Kubernetes Best Practices)
The name is inspired by the Arecibo message, a 1974 interstellar radio message carrying basic information about humanity and Earth sent to globular star cluster M13 in the hope that extraterrestrial intelligence might receive and decipher it. The message was broadcast into space a single time via frequency modulated radio waves at a ceremony to mark the remodelling of the Arecibo radio telescope in Puerto Rico on 16 November 1974.

