In a Spring Boot microservice architecture, traceId and spanId are correlation identifiers used for distributed tracing, letting a single request's journey be reconstructed across many services even though each service processes it independently.
Key Points: • traceId is a single identifier assigned to a request the moment it enters the system and stays the same across every service it touches. • spanId identifies one unit of work, typically one service call or operation, within that overall trace. • These IDs are usually propagated automatically via HTTP headers by libraries like Micrometer Tracing (or the older Spring Cloud Sleuth). • Tracing backends like Zipkin or Jaeger use traceId to stitch spans from different services into a single visual timeline. • Including traceId in log output lets engineers grep logs across multiple services for one specific request during debugging.
Example: A checkout request that touches the order, payment, and inventory services carries the same traceId through all three; each service logs its own spanId, and a tracing tool like Zipkin visualizes the full call chain and where time was spent.
Interview Tip: A concise interview answer is:
"traceId identifies an entire request as it flows across multiple microservices, while spanId identifies one specific unit of work within a single service. Together they let tools like Zipkin reconstruct the full path of a request, which is essential for debugging latency and failures in a distributed system."