0Pricing
FastAPI Backend Development Bootcamp · Lesson

Service Discovery and Health Checks

Learn how microservices find each other dynamically and how health checks keep traffic flowing only to healthy instances.

Service Discovery and Health Checks is a free FastAPI Backend Development Bootcamp lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the FastAPI Backend Development Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Discovery Problem

In a microservices system, instances start, stop, and move across hosts constantly. Hardcoding IP addresses breaks immediately.

Service discovery lets a service find healthy instances of another service by name at runtime.

Client-Side vs Server-Side

Two patterns exist:

  • Client-side: the caller queries a registry and picks an instance
  • Server-side: a load balancer or gateway resolves the name and forwards the request

Service Registries

A registry stores live instances. Popular options include Consul, etcd, and Kubernetes DNS.

Services register on startup and deregister on shutdown.

Registering a Service

On boot, a service announces its address to the registry.

import httpx

async def register():
    await httpx.AsyncClient().put(
        "http://consul:8500/v1/agent/service/register",
        json={"Name": "orders", "Address": "10.0.0.5", "Port": 8000})

Why Health Checks Matter

A registered instance might still be broken. Health checks ensure the registry only routes to instances that are actually able to serve requests.

A FastAPI Health Endpoint

Expose a lightweight endpoint that returns quickly when the service is healthy.

from fastapi import FastAPI

app = FastAPI()

@app.get("/health")
def health():
    return {"status": "ok"}

Liveness vs Readiness

Two distinct checks:

  • Liveness: is the process alive? If not, restart it.
  • Readiness: can it serve traffic now with DB connected and caches warm? If not, hold traffic.

A Readiness Check

Readiness verifies downstream dependencies before accepting traffic.

from fastapi import Response, status

@app.get("/ready")
async def ready(response: Response):
    if not await db_is_up():
        response.status_code = status.HTTP_503_SERVICE_UNAVAILABLE
        return {"ready": False}
    return {"ready": True}

Kubernetes Probes

Kubernetes uses these endpoints as probes to manage pods automatically.

livenessProbe:
  httpGet:
    path: /health
    port: 8000
readinessProbe:
  httpGet:
    path: /ready
    port: 8000

Graceful Deregistration

On shutdown, deregister so callers stop routing to a dying instance, and finish in-flight requests first.

@app.on_event("shutdown")
async def shutdown():
    await deregister_from_registry()

TTL and Stale Entries

Registry entries usually carry a TTL. If a service stops sending heartbeats, its entry expires automatically so callers stop routing to a dead instance.

Quick Check

Test your discovery knowledge.

Recap

You learned how services find and trust each other:

  • Service discovery resolves services by name via a registry
  • Health checks ensure only healthy instances get traffic
  • Liveness restarts, readiness gates traffic

Together they keep a dynamic microservices system resilient.

Frequently asked questions

Is the “Service Discovery and Health Checks” lesson free?

Yes — the full text of “Service Discovery and Health Checks” is free to read here on the web, and the FastAPI Backend Development Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the FastAPI Backend Development Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Service Discovery and Health Checks”?

Learn how microservices find each other dynamically and how health checks keep traffic flowing only to healthy instances. You practise FastAPI Backend Development Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start FastAPI Backend Development Bootcamp?

No prior experience is required. FastAPI Backend Development Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Service Discovery and Health Checks” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this FastAPI Backend Development Bootcamp lesson?

Yes. Every FastAPI Backend Development Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Designing Microservices Architecture
  2. Inter-service Communication
  3. Implementing an API Gateway with FastAPI
  4. Service Discovery and Health Checks
← Back to FastAPI Backend Development Bootcamp