Container health checks in Docker - Time & Space Complexity
Checking container health helps keep apps running smoothly. We want to see how the time to check health changes as we add more containers.
How does the number of health checks grow when we have more containers?
Analyze the time complexity of the following Docker health check setup.
HEALTHCHECK --interval=30s --timeout=5s \
CMD curl -f http://localhost/health || exit 1
This code sets a health check that runs every 30 seconds, trying to reach a local URL to confirm the container is healthy.
Look for repeated actions that affect time.
- Primary operation: The health check command runs repeatedly on each container.
- How many times: It runs every 30 seconds per container, so the number of checks grows with the number of containers.
As you add more containers, the total health checks increase proportionally.
| Input Size (n) | Approx. Operations (checks per interval) |
|---|---|
| 10 containers | 10 health checks |
| 100 containers | 100 health checks |
| 1000 containers | 1000 health checks |
Pattern observation: The total checks grow directly with the number of containers.
Time Complexity: O(n)
This means the time spent on health checks grows linearly as you add more containers.
[X] Wrong: "Health checks run once for all containers together, so time stays the same no matter how many containers there are."
[OK] Correct: Each container runs its own health check independently, so total checks add up with more containers.
Understanding how repeated tasks scale helps you design systems that stay reliable as they grow. This skill shows you can think about real-world system behavior clearly.
"What if we change the health check interval to run twice as often? How would the time complexity change?"