Follow one request across services
A slow request touches ten services; which one is slow?
Say it like this
When a request crosses ten services I need to see where the time went, so every hop records a span under one trace id and I sample enough traces to keep the slow and failed ones.
Basic solution
Propagate a trace id through every hop and record spans; view the request as a waterfall.
One trace id across the feed read
The gateway starts the trace; each service adds child spans and forwards the context; spans export asynchronously to a collector that applies tail sampling and stores the whole trace for the waterfall view.
How it works
- The first service to see a request (the gateway) creates a trace id, which names this one request across every service, and a root span, which records that the gateway handled it. It writes both into the outgoing request headers in the W3C traceparent format so the next service can find them.
- Each downstream service reads traceparent from the incoming request, starts a child span with its own span id and with the caller's span id recorded as its parent, does its work, and copies the context into the headers of every call it makes in turn. The parent links are what later make the tree.
- A span holds a name, a start and end time, the service that produced it, and attributes such as the HTTP route, the status, the database statement template and the address of the peer it called.
- Spans are exported asynchronously, in batches, to a collector. The collector groups spans by trace id and stores them; the UI reads one trace and renders it as a waterfall: a timeline showing which spans ran one after another, which ran in parallel, and which one took most of the time.
- Sampling decides which traces are kept, because keeping every span at scale costs too much. Head sampling makes the decision at the root (keep 1 in 100) and propagates it. Tail sampling buffers the whole trace in the collector for a few seconds and keeps it if it turned out slow or failed.
- Queue hops propagate the context by writing the trace id and parent span id into the message headers, so when a consumer picks the message up its span links back to the producer's span instead of starting a new, disconnected trace.
What a trace shows that logs do not
- Timing and causality across services in one picture: the feed call waited 180 ms on the ranking service, which spent 150 ms inside one database query. Logs would show each of those facts on separate lines in separate stores.
Worked example
News feed home timeline, p99 of 800 ms
- A feed read touches the gateway, feed service, timeline cache, celebrity cache, ranking service, post cache and user cache: about 7 services and 40 spans including the per-shard MGETs.
- Traffic: 20K feed reads/s. Keeping every trace: 20K × 40 spans × 300 B ≈ 240 MB/s, 20 TB per day. Not an option.
- Head sampling at 1%: 2.4 MB/s, 200 GB per day, fine for a week of retention. But a p99 problem is in 1% of requests, and 1% of 1% leaves 2 slow traces per second, enough to look at but easy to miss if the slow path is rarer.
- Tail sampling: keep 100% of traces with any error or with total duration above 500 ms, plus 0.5% of the rest. Slow traces are 1% of 20K = 200/s, all kept; baseline adds 100/s. Total 300 traces/s ≈ 3.6 MB/s. Every slow request is now visible.
- Finding: the waterfall for slow traces shows the ranking span at 600 ms when the feature store span inside it waits on one Redis shard with a 20 ms GC pause 30 times in sequence, because the ranking service fetched features one key at a time. Fix: MGET, one round trip; p99 drops to 180 ms.
- Overhead on the service: creating a span costs a few microseconds; the exporter batches in the background. Under 1% CPU at this volume.
Variants and when each wins
Head-based sampling
- The root service decides to keep or drop the trace before any work happens, and every downstream service follows that decision. It wins on cost and consistency; it loses because it cannot know whether the trace will turn out interesting.
Tail-based sampling
- The collector buffers each complete trace for a few seconds and keeps it based on the outcome, such as any error or a total duration above a threshold. It wins because every slow or failed trace is kept; it costs a collector tier with memory for the buffer and routing that sends all spans of one trace to the same collector.
Always-on with low retention
- Every trace is kept, but only for an hour before being dropped. It wins for debugging right after a deploy; storage still scales with traffic, so it is not a long-term store.
Logs with trace ids, no span store
- The trace id goes into every log line and timing is reconstructed from log timestamps. It wins when you cannot run a tracing backend; it loses the waterfall view and makes parallelism hard to see.
Recommendation: OpenTelemetry instrumentation everywhere, tail sampling for errors and slow traces plus a small baseline, and trace ids in logs so the two stores join.
Failure modes
Context lost at a queue or a thread pool
- Traces end at the producer, and the consumer's work shows up as a separate trace with its own root. It happens because the context lives in a thread-local or request scope that does not cross the hop. Write traceparent into the message headers and restore it in the consumer, and use the instrumentation's context propagation for thread pools and executors.
Sampling hides the problem
- Users report slow requests, but no slow trace exists in the store. It happens because 1% head sampling rarely catches a slow path that is itself rare. Switch to tail sampling keyed on latency and errors, or add a debug flag that forces sampling for one user or request.
Span explosion
- Trace storage costs more than the services being traced. It happens because the code opens a span per cache key or per loop iteration. Keep one span per logical operation and put the counts in attributes; a span per shard is for debugging one specific issue, not the default.
Clock skew between hosts
- A child span appears to start before its parent. It happens because the two hosts' clocks disagree by a few milliseconds. Run NTP on every host and let the UI tolerate small negative offsets; durations measured within one host remain correct.
Missing attributes
- A slow database span shows no statement, so nobody knows which query was slow. It happens because instrumentation is off or attributes were stripped for privacy. Record the statement template with parameters removed, the route template rather than the full URL, and the peer service name.
Interviewer follow-ups
Answer out loud before you open each one.
How does trace context get from one service to the next?
In a header, traceparent, which carries the trace id, the parent span id and a sampled flag. Every outbound HTTP or gRPC call adds it, every inbound call reads it, and for queues the same fields go into the message headers so the consumer can continue the trace.
Why tail sampling rather than head sampling?
Head sampling decides before knowing whether the request will be slow or fail, so it keeps mostly boring traces. Tail sampling waits for the whole trace and keeps the interesting ones, at the cost of buffering spans in the collector for a few seconds.
What would a trace of the ride-matching offer flow show?
Root span at the ride API, child spans for the geo query, the ETA batch call, the lock acquisition in Redis and the offer push through the driver gateway, with the trip-store write at the end. The waterfall would show whether the ETA call or the lock is the dominant wait.
How do traces, metrics and logs link together?
Put the trace id in every log line so a trace opens its logs; attach exemplar trace ids to latency histogram buckets so a slow bucket opens a real trace; and derive request metrics from spans when a service has no separate instrumentation.
Connections
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon API Gateway | Gateway | API Gateway, Apigee | API Management | Kong, Tyk |
| Amazon EC2 Auto Scaling | Feed service | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |
| Amazon SageMaker | Ranking service | Vertex AI | Azure Machine Learning | KServe, Ray Serve, NVIDIA Triton |
| Amazon ElastiCache | Post cache | Memorystore | Azure Managed Redis | Redis, Valkey, Memcached |
| AWS X-Ray | Trace collector, Waterfall view | Cloud Trace | Application Insights | OpenTelemetry with Jaeger or Grafana Tempo |