Part 11 / Topic 5 of 7
Correlation IDs
One dispatcher clicks once; the request touches your API, the sync service, and Acme's API. When it fails, the evidence is scattered across three log systems. A correlation ID stitches it back together: generate an ID at the edge (or accept the caller's), pass it in a header (X-Request-ID) on every downstream call, and put it in every log line's fields:
log.info("acme fetch", extra={"fields": {"request_id": rid, "page": 2}})
Now "one ID finds the whole story" is literally a log query. In FastAPI this is ten lines of middleware; the discipline — no log line without the ID, no outbound call without the header — is the part that pays.
🧪 Quick quiz
- What is a correlation ID for?
Show the answer
Following one request across logs from several services. Pass it through every call and log line, so one ID finds the whole story.
Done here? The practice set for Part 11 — 4 exercises with solutions — is on the part page.