

Quick start
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 arecibo
const 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 deployment manifest.
Options
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. |
logLevel |
'fatal', 'error', 'warn', 'info', 'debug', 'trace', or 'silent' |
'info' |
The log level of the two routes. |
Custom callbacks
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.
Readiness checks
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.
Behaviour notes
- 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.
TypeScript
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.
Kubernetes deployment manifest
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
terminationGracePeriodSeconds: 30
containers:
- name: my-app
image: my-app:1.0.0
ports:
- name: http
containerPort: 3000
startupProbe:
httpGet:
path: /arecibo/liveness
port: http
periodSeconds: 5
failureThreshold: 30
livenessProbe:
httpGet:
path: /arecibo/liveness
port: http
periodSeconds: 15
readinessProbe:
httpGet:
path: /arecibo/readiness
port: http
periodSeconds: 5
lifecycle:
preStop:
sleep:
seconds: 5
- The
startupProbeuses the liveness URL. 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. - The
preStophook delays theSIGTERMsignal. This gives the cluster time to stop new traffic to the Pod. Callfastify.close()when your process receivesSIGTERM. terminationGracePeriodSecondsmust be longer than thepreStopdelay plus the shutdown time of your application.- Kubernetes enables the
sleepaction by default from version 1.30. The action is stable from version 1.34. For an older cluster, use anexechook that runssleep.
How to commit
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.
Reference
- Fastify
- Configure Liveness and Readiness Probes
- Kubernetes Health Checks with Readiness and Liveness Probes (Kubernetes Best Practices)
Fun fact: where does the name come from?
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.