Health checks in Compose in Docker - Time & Space Complexity
We want to understand how the time taken by Docker Compose health checks changes as the number of services grows.
Specifically, how does adding more containers with health checks affect the total checking time?
Analyze the time complexity of this Docker Compose health check setup.
version: '3.8'
services:
web:
image: nginx
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 30s
timeout: 10s
retries: 3
db:
image: postgres
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 30s
timeout: 10s
retries: 5
This Compose file defines two services, each with its own health check that runs periodically.
Look for repeated health check commands and their frequency.
- Primary operation: Each service runs its health check command repeatedly at set intervals.
- How many times: Each health check runs independently and periodically, repeating indefinitely.
As the number of services with health checks increases, the total number of health check commands run per interval grows.
| Number of Services (n) | Health Check Commands per Interval |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The total health check commands grow directly with the number of services.
Time Complexity: O(n)
This means the total health check work grows linearly as you add more services.
[X] Wrong: "Health checks run once and do not add to ongoing work as services increase."
[OK] Correct: Health checks run repeatedly and independently for each service, so more services mean more repeated checks.
Understanding how repeated tasks scale helps you design systems that stay responsive and manageable as they grow.
What if all health checks ran sequentially instead of independently? How would the time complexity change?