How to Check Service Readiness Dependencies in Python

This code simulates a readiness check for external dependencies (database, cache, queue) with mock availability data and reports readiness status.

Easy Python 3.9+ Aug 9, 2026 Observability & SRE 11 views 0 copies

Python code

34 lines
Python 3.9+
import sys
from datetime import datetime


def check_dependencies(config):
    results = []
    for dep, required in config.items():
        available = mock_availability(dep)
        status = "READY" if available >= required else "NOT READY"
        results.append((dep, available, required, status))
    return results


def mock_availability(dep):
    slots = {"database": 5, "cache": 3, "queue": 2, "auth": 4}
    return slots.get(dep, 0)


if __name__ == "__main__":
    config = {
        "database": 3,
        "cache": 4,
        "queue": 1,
        "auth": 2,
        "unknown": 1,
    }
    print(f"Readiness check at {datetime.now():%Y-%m-%d %H:%M:%S}")
    for dep, available, required, status in check_dependencies(config):
        print(f"{dep:12s} available={available} required={required} -> {status}")
    if all(status == "READY" for _, _, _, status in check_dependencies(config)):
        print("Overall: ALL SYSTEMS READY")
    else:
        print("Overall: SOME DEPENDENCIES MISSING")
        sys.exit(1)

Output

stdout
Readiness check at 2025-01-15 10:30:00
database     available=5 required=3 -> READY
cache        available=3 required=4 -> NOT READY
queue        available=2 required=1 -> READY
auth         available=4 required=2 -> READY
unknown      available=0 required=1 -> NOT READY
Overall: SOME DEPENDENCIES MISSING

How it works

The check_dependencies function iterates over a config dictionary mapping dependency names to their required counts. For each dependency, it calls mock_availability to get the current available count. The status is determined by comparing available to the required count, marking it "READY" if sufficient. The main block prints each dependency's status and exits with a non-zero code if any dependency is not ready, which is useful for CI pipelines or container health checks. Hardcoded mock data replaces real service queries, making the logic testable without external systems.

Common mistakes

  • Forgetting to handle unknown dependencies gracefully (mock_availability returns 0)
  • Not using `sys.exit(1)` to signal failure for orchestration tools
  • Hardcoding time output makes tests flaky
  • Comparing availability numbers without considering units or thresholds

Variations

  1. Replace mock_availability with real HTTP checks (e.g., requests.get for health endpoints)
  2. Use asyncio to check dependencies concurrently for faster startups

Real-world use cases

  • Kubernetes liveness/readiness probes that call a Python script to verify database connectivity before serving traffic.
  • CI/CD pipeline steps that validate required services (cache, queues) are up before running integration tests.
  • Microservice startup scripts that fail fast with a clear message when dependent services are unavailable.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Observability & SRE

Related tutorials and quizzes for this topic.