X-Request-ID
General Summary
Section titled “General Summary”Unique identifier for tracking a specific request through distributed systems and logs.
Detailed Description
Section titled “Detailed Description”The X-Request-ID header (also known as Request-ID) contains a unique identifier assigned to each HTTP request. This identifier allows developers and operations teams to trace a single request as it flows through multiple services, load balancers, proxies, and application layers. It’s invaluable for debugging, monitoring, and correlating log entries across distributed systems. The header can be set by the client, a load balancer, or the first service that receives the request.
Use Cases (When, Why, and How)
Section titled “Use Cases (When, Why, and How)”When to Use It
Section titled “When to Use It”Include this header in every request and response in distributed systems, microservices architectures, or any application where request tracing is important. It should be generated at the edge of your system (load balancer or API gateway) if not provided by the client.
Why to Use It
Section titled “Why to Use It”When debugging production issues, you need to correlate logs across multiple services handling the same request. Without a request ID, finding related log entries is nearly impossible in high-traffic systems. This header enables end-to-end request tracing, performance monitoring, and error tracking across your entire stack.
How to Use It
Section titled “How to Use It”Generate a unique identifier (typically a UUID) for each incoming request and propagate it through all downstream services. Log this ID with every log entry related to the request.
Example code in Javascript (Express middleware):
import { v4 as uuidv4 } from 'uuid';
app.use((req, res, next) => { const requestId = req.headers['x-request-id'] || uuidv4(); req.id = requestId; res.setHeader('X-Request-ID', requestId);
console.log(`[${requestId}] ${req.method} ${req.path}`); next();});Example code in Javascript (Fetch client):
fetch('https://api.example.com/data', { headers: { 'X-Request-ID': crypto.randomUUID() }});Example
Section titled “Example”X-Request-ID: 550e8400-e29b-41d4-a716-446655440000