What are traceId and spanId in Spring Boot microservices, and what are they used for?

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

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.

# One request, three services: same traceId, different spanIds
INFO [order-service,   65f2a1c9e0b74d21,1a2b3c4d5e6f7081] Creating order 501
INFO [payment-service, 65f2a1c9e0b74d21,9f8e7d6c5b4a3921] Charging 1299.00
INFO [inventory-service,65f2a1c9e0b74d21,0a1b2c3d4e5f6071] Reserving 2 items

// Create your own span for an important piece of work (Observation API)
@Service
class InventoryChecker {
    private final ObservationRegistry registry;
    InventoryChecker(ObservationRegistry registry) { this.registry = registry; }

    boolean check(String sku) {
        return Observation.createNotStarted("inventory.check", registry)
                .lowCardinalityKeyValue("warehouse", "pune")
                .observe(() -> warehouseClient.hasStock(sku));   // new span + timer metric
    }
}

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.

Last updated: 11 November 2026

Previous question: How do you enable and disable auto-configuration in Spring Boot?

Next question: What are WebFlux and Mono in Spring Boot?

More Spring Boot - Level V (Expert) interview questions

All Spring Boot - Level V (Expert) questions