Design a Distributed Tracing System
Understanding the Problem
🧭 What is distributed tracing? Distributed tracing records the path of a single request as it moves through a system of services. Each unit of work (an HTTP handler, a database query, a call to another service) becomes a span with a start time, a duration and a parent, and all the spans of one request share a trace ID. Put together, they form a tree that shows where the time went and where the error came from. Jaeger, Zipkin, Grafana Tempo, AWS X-Ray and the tracing products of Datadog and Honeycomb all work this way, and OpenTelemetry is the standard way to produce the data.
You'll hear this asked as "design Jaeger," "design Zipkin," "design an OpenTelemetry backend" or "design a system that captures HTTP requests and responses for debugging." The last one is a feature of the same system: a trace already records each HTTP call, and capturing headers and bodies attaches more detail to those spans. We'll build the tracing pipeline and treat payload capture as a requirement with its own deep dive.
Functional Requirements
Core Requirements
- Services should be able to emit spans with little or no code (an SDK and auto-instrumentation), and trace context should propagate across service calls and queues.
- Users should be able to open a trace by its ID and see the full tree of spans as a waterfall.
- Users should be able to search for traces by service, operation, duration, status and attributes, and see a map of which services call which.
- Users should be able to capture HTTP request and response details (headers and bodies) for selected endpoints.
Below the line (out of scope)
- Continuous profiling and real-user browser monitoring.
- Metrics and log storage, except where we derive metrics from spans.
- Automatic root-cause analysis.
Non-Functional Requirements
Ask for numbers. We'll assume a company with 2,000 services handling 500,000 requests per second at the edge. An average request touches enough services and databases to produce about 30 spans, so the fleet produces 15 million spans per second. A span with its attributes is about 500 bytes.
Core Requirements
- Tracing should add very little overhead to services (on the order of 1% CPU), add nothing to request latency, and never cause a request to fail.
- The pipeline should accept 15M spans per second before sampling.
- A trace should be viewable within about a minute of the request finishing.
- We should keep every trace with an error or unusually high latency, plus a representative sample of normal traffic, for 7 days.
- Opening a trace should take under 2 seconds; searches over the last hour under a few seconds.
Below the line (out of scope)
- Keeping 100% of traces.
- Perfect completeness. A trace missing one span from a crashed pod is acceptable.
The math decides the design. 15M spans per second at 500 bytes is 7.5 GB/s, about 650 TB per day. Storing all of it for a week is over 4 PB, mostly of successful requests that look identical to millions of others. Keeping about 1% plus the interesting ones brings it down to a few TB per day. So the central question isn't how to store everything; it's how to decide what to keep, and the right decision often can't be made until the request is over.
Tracing is the one observability signal where sampling is the default, not a compromise. Metrics are already aggregated and logs are often filtered at the source, but raw traces are large, highly repetitive, and mostly useful in their unusual cases.