Volume 6 · Chapter 5 — Observability and Operations
Distributed Tracing with X-Ray
5.1 Introduction
In a microservices system, a single customer request may pass through a dozen services, and when it’s slow or fails, metrics and logs alone struggle to tell youwhere. Distributed tracing solves this by following a request end-to-end across services, and AWS X-Ray is the service that provides it. Tracing is the third pillar of observability and is especially important in distributed systems, so understanding what it does and how it complements metrics and logs is a common interview and design topic for microservices.
5.2 The Problem Tracing Solves
When one request flows through many services - gateway, authentication, payments, a downstream partner, the database - and it’s slow or fails, the hard question is: which service caused it? Metrics show that latency or errors rose somewhere, and each service’s logs show its own piece, but stitching together the full path of a single request across all those services manually is painful and slow. Distributed tracing answers this directly by tracking the request’s entire journey as one connected trace, so you can see exactly which service (or which downstream call) was slow or failed - turning “somewhere in this chain” into “this specific call in this specific service.”
5.3 How X-Ray Works
X-Ray traces requests as they travel through your application. Each request is assigned a trace ID that propagates across the services it touches, and each service records a segment describing the work it did and how long it took, including calls to downstream services and AWS resources. X-Ray assembles these segments into a single trace showing the complete path and timing - a breakdown of where the request spent its time and where any error occurred. It also produces a service map, a visual graph of your services and their dependencies annotated with latency and error information, so you can see the overall topology and spot unhealthy or slow services at a glance. This makes latency and failures in a distributed system diagnosable.
sequenceDiagram autonumber participant GW as Gateway participant Auth as Auth participant Pay as Payments participant DB as Database Note over GW,DB: One trace ID propagates across all services GW->>Auth: authenticate (segment: 12ms) GW->>Pay: process payment (segment: 240ms) Pay->>DB: write transaction (segment: 210ms - the slow part) DB-->>Pay: ok Pay-->>GW: done Note over Pay,DB: Trace pinpoints the DB call as the latency source
5.4 Tracing with Metrics and Logs
Tracing is most powerful combined with the other pillars. The trace ID that X-Ray propagates should also appear in your logs (as the correlation ID), so you can move seamlessly between a trace and the detailed logs for the same request. The typical investigation flow becomes: a metric alarm indicates elevated latency or errors; you look at the service map and traces to identify which service or downstream call is responsible; then you drill into that service’s logs for the affected traces to see exactly what went wrong. Metrics detect and quantify, traces localize to a service, and logs explain the detail - and the shared trace/correlation ID is the thread connecting all three, which is why consistent ID propagation across telemetry is a key practice.
5.5 Banking Production Scenario
A bank instruments its microservices with X-Ray. Each customer request carries a trace ID propagated across the gateway, authentication, payments, downstream partner calls, and the database, and the same ID appears in every log entry as the correlation ID. When payment latency rises, an alarm fires on the latency metric; the on-call engineer opens the X-Ray service map, sees the payments service showing high latency, and drills into a trace that reveals the database write as the slow segment; they then query the logs for those traces to find the specific cause. What could have been an hours-long hunt across many services becomes a few minutes of following the trace to the exact slow call. The service map also continuously highlights any unhealthy dependency across the whole system.
5.6 Troubleshooting: Tracing Gaps
Tracing problems usually mean incomplete instrumentation. If a trace stops partway, some service isn’t instrumented or isn’t propagating the trace ID - ensure every service participates and passes the ID onward. If you can’t connect a trace to logs, the trace/correlation ID isn’t in the log entries - include it consistently. If you can’t tell which service caused a slowdown, tracing may not be enabled at all - add it, since metrics and logs alone can’t localize across services easily. If the service map shows gaps, downstream calls aren’t being captured. The consistent fix is complete, consistent instrumentation: every service traces its work, propagates the trace ID, and includes that ID in logs - so traces are end-to-end and tie into the rest of the telemetry. Without that, the chain breaks exactly where you most need visibility.
What is distributed tracing, and why is it essential in a microservices architecture?
Distributed tracing is the practice of following a single request end-to-end as it flows across all the services it touches, recording the path it took and the time spent in each service, and AWS X-Ray is the service that provides it. It’s essential in a microservices architecture because of a problem that metrics and logs alone handle poorly: when one customer request passes through many services - a gateway, authentication, the payments service, a downstream partner, the database - and that request is slow or fails, the critical question is which service or which specific call caused it, and answering that by hand is painful. Metrics can show that latency or errors rose somewhere, but not precisely where across the chain; each service’s logs show only its own piece of the story; and manually stitching together a single request’s journey across a dozen services’ separate logs is slow and error-prone, especially under incident pressure. Tracing solves this directly. Each request is assigned a trace ID that propagates across every service it touches, and each service records a segment describing the work it did and how long it took, including its calls to downstream services and AWS resources. X-Ray assembles these segments into one connected trace that shows the complete path and timing - a breakdown of exactly where the request spent its time and where any error occurred - so “somewhere in this chain” becomes “this specific database call in the payments service took 210 milliseconds and caused the slowness.” It also builds a service map, a visual graph of your services and their dependencies annotated with latency and error data, so you can see the whole topology and immediately spot an unhealthy or slow service. Tracing is even more powerful combined with the other observability pillars: the trace ID should also appear in your logs as the correlation ID, so the investigation flows naturally - a metric alarm signals elevated latency, the trace and service map localize it to a specific service or downstream call, and the logs for those traces explain the detailed cause. Metrics detect and quantify, traces localize across services, logs explain, and the shared trace/correlation ID threads them together. So distributed tracing is essential in microservices because it’s the only pillar that efficiently answers “where in this distributed request did the problem occur,” turning what would be an hours-long cross-service hunt into minutes of following a trace to the exact slow or failing call - which is exactly why it’s a core part of observability for any system where a request spans many services.
Chapter Revision Notes
- Distributed tracing follows one request end-to-end across services, showing the path and time spent in each - X-Ray provides it.
- A trace ID propagates across services; each records a segment; X-Ray assembles them into one trace and a service map of dependencies.
- It answers “where across services did the request slow or fail” - which metrics and logs alone can’t do easily in a distributed system.
- Put the trace ID in logs (as the correlation ID) so you move seamlessly between metrics, traces, and logs for one request.
- Gaps come from incomplete instrumentation - every service must trace its work and propagate the ID.