Skip to content
Aabha AI Academy

Module 8 of 9 · Lesson 23 of 26

Distinguish liveness, readiness and metrics

Work through distinguish liveness, readiness and metrics using a runnable reference, a focused regression check and a local extension.

Read in any order. All lessons stay open, including after an unanswered or incorrect check.

In this lesson you will distinguish liveness, readiness and metrics. Work with the Equipment Rental API in the downloadable lab. The reference is a complete solution with separate lesson checks, so you can inspect the answer, make a deliberate local change and verify its behavior.
Liveness asks whether the process can respond. Readiness asks whether it can serve work that depends on its current dependencies. /health/live returns without a database query; /health/ready performs SELECT 1 and reports 503 if the database cannot be reached. Restarting a healthy process repeatedly because a database is unavailable can make recovery harder.

Readiness does not prove every business function works or that migrations are compatible. The delivery rehearsal separately checks schema revision, contract requests and rollback planning. Metrics count request method, route pattern and status group. They omit user ids, rental ids and query parameters, because those values create unbounded series and may expose private information. The local /metrics route is a teaching JSON view, not a production monitoring service; a deployment should choose its exporter and access boundary explicitly. Read a symptom in context: readiness failure with passing liveness points to a dependency path, while rising 409 capacity conflicts may be expected business behavior rather than server outages.
Worked source: equipment/api.py, create_app.

Run lesson23. Both health routes pass with fresh PostgreSQL. A separate app points to an unused local port: liveness still passes while readiness returns 503. Ten different missing URLs contribute to one unmatched metrics row, demonstrating bounded labeling.
pythonCopyable
def test_lesson23(db):
    app = create_app(
        Settings(database_url=os.environ["LAB_DATABASE_URL"], jwt_secret=KEY)
    )
    with TestClient(app) as client:
        assert client.get("/health/live").status_code == 200
        assert client.get("/health/ready").status_code == 200
        for identifier in range(10):
            client.get(f"/missing-{identifier}")
        rows = client.get("/metrics").json()
        unmatched = [row for row in rows if row["route"] == "unmatched"]
        assert len(unmatched) == 1 and unmatched[0]["count"] == 10
    failed = create_app(
        Settings(
            database_url="postgresql+psycopg://unused:unused@127.0.0.1:1/aabha_lab_missing?connect_timeout=1",
            jwt_secret=KEY,
        )
    )
    with TestClient(failed) as client:
        assert client.get("/health/live").status_code == 200
        assert client.get("/health/ready").status_code == 503
TerminalPython 3.13 virtual environment; Docker running; extracted lab directory
python run_checks.py -k lesson23

Expected result The selected lesson test passes against a new temporary PostgreSQL database; the container is removed afterward.

Keep for reference

Equipment Rental lab and lesson checks

ZIP containing Python source, real Alembic migrations, 26 lesson checks, a dependency lock and text instructions. Extract it before following the local exercise.

Download Equipment Rental lab and lesson checks

Practise locally

Write a diagnostic note for an unavailable database: include the two health statuses, the failing dependency and the next safe check. Add a test that readiness never returns the database URL or password in its error. Then inspect the metrics after a deliberate 404 and a successful equipment read and explain their different status groups.

The lesson check verifies the reference behavior. Add your own assertions for your change. Local practice is not uploaded or scored by this learning release.

Pause and reflect

What failure does this lesson prevent, and which assertion in lesson23 would expose it?

Use a concrete input, expected result and limitation from your local work. Saving a reflection does not certify the project.

Optional knowledge check

If the database is unavailable but the process can respond, what fits?

Liveness must fail so the process restarts forever.

Try another answer. A dependency outage alone does not prove the application process is dead.

Readiness passing proves every migration and business rule is correct.

Try another answer. A dependency probe is only one signal; schema and contract verification remain separate.

Liveness passes while database-dependent readiness fails.

Correct. The signals distinguish the responding process from an unavailable dependency.

This practice does not assess your project or award a certificate.

Your reading progress

Progress is saved in this browser when storage is available.

Sign in to save across devices · Create an optional account