Sub topic 5
Tuning interval, timeout, retries, and start_period
This chapter assumes you can read a compose.yaml, know what a Compose healthcheck is, and have seen pg_isready used as a probe. Here you will learn what each timing option controls and how to choose values.
The four timing options
A healthcheck runs its test command repeatedly inside the container. Four options decide when it runs and when Compose gives up.
| Option | Meaning | Default |
|---|---|---|
| `interval` | Time between checks | 30s |
| `timeout` | How long one check may run before it counts as failed | 30s |
| `retries` | Consecutive failures needed to mark the container unhealthy | 3 |
| `start_period` | Grace time after start during which failures are not counted | 0s |
Here is the PostgreSQL healthcheck from earlier, with all four set:
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 3s
retries: 5
start_period: 10sThis runs pg_isready every 5 seconds. A single check that takes longer than 3 seconds counts as one failure. Failures during the first 10 seconds are ignored for the retry count. After that, five failures in a row mark the container unhealthy.
How the options interact
The options combine in a few ways you should know.
- A success ends the starting phase early. If a check passes during
start_period, the container becomes healthy right away. The grace period does not delay a healthy result. - Failures reset on success. The retry count is consecutive. One passing check clears the count.
- Time to unhealthy is roughly
retries × (interval + timeout)in the worst case. With the values above, that is up to 40 seconds after the grace period ends. - Time to healthy is bounded by
interval. The first check runs oneintervalafter the container starts, so a long interval delays the first healthy report even when the service is ready.
Choosing values
Pick values from how the service behaves, not from habit.
- Set
start_periodto the normal boot time. Measure how long the service takes to accept connections on a typical machine, then add some margin. A database that runs crash recovery needs more than one that starts empty. - Set
intervalfor the cost of the probe. A cheap local command likepg_isreadycan run every 2 to 5 seconds. An expensive probe, such as an HTTP request that hits a database, should run less often. - Set
timeoutbelowinterval. If a check can run longer than the interval, runs pile up and the signal gets noisy. - Set
retriesto tolerate brief stalls. A value of 1 makes a single slow check mark the service unhealthy. A value of 3 to 5 absorbs short spikes.
Common errors
- Waiting forever for
healthy. If the check never passes andretriesis large, the dependent service waits a long time. Lowerretriesor fix thetestcommand. - Marked unhealthy on slow boot. The service needs 40 seconds to start, but
start_periodis 0s andretriesis 3. Raisestart_period. - A fast interval that loads the service. A 1-second interval on a heavy probe adds constant load. Raise
interval.
Exercise
A service takes about 30 seconds to boot. Its healthcheck uses interval: 5s, timeout: 3s, retries: 3, and no start_period. Compose marks it unhealthy before it finishes booting. Which option do you change, and to what?
Show answer
Set start_period to a value above the boot time, for example 40s. Failures during that period do not count toward retries, so the container is not marked unhealthy while it boots. A passing check during the period still makes it healthy immediately.