Correlation ID Pattern
patterns observability logging tracing
A correlation ID is a unique identifier set at the entry point of any processing flow and passed through every step, node, and external call. It makes a single execution traceable across services, logs, and external systems without a distributed tracing infrastructure.
The Pattern
- Set at entry — generate a UUID (or accept one from the caller) at the first node/handler before any other work.
- Never replace the message object — carry the ID as a field on the existing message, not as a wrapper or replacement.
- Include in every log line — every log statement emits
correlationIdalongside the message and context. - Pass to external systems — use the
X-Correlation-IdHTTP header on all outbound calls so downstream services can log it too. - Return to callers — include the ID in error responses so the caller can reference it in support requests.
Why It Matters
Without a correlation ID, a failure in a multi-step pipeline produces logs scattered across nodes with no way to reconstruct which records belong to the same execution. With one, a single ID surfaces the complete execution trace.
Implementation Notes
- If the incoming request already contains a correlation ID header, use it rather than generating a new one. This preserves traceability across system boundaries.
- Store the ID in a consistent field name (e.g.
msg.correlationIdorcontext.traceId) and document that field name in the service’s readme. - Short-lived IDs are fine — they only need to survive one execution, not forever.
Anti-Patterns
- Generating a new ID mid-flow (breaks the trace).
- Logging without including the ID (defeats the purpose).
- Replacing the full message object in a node (loses the ID).
Source: Derived from distributed systems tracing practices. Validated in multi-step integration flows 2026-04.
Related
- error-handling — error logs must include correlationId
- idempotency-guard — idempotency keys and correlation IDs are related but distinct
- pre-release-checklist — verify correlation ID is wired through before release