SYSTEM DESIGN SKETCHBOOK / PRACTICE SHEET / NO. 41
Design a distributed tracing system
A checkout request touches dozens of services before it returns. When it is slow or fails, an engineer wants to open that one request and see every call it made, in order, with timings and errors, instead of guessing from averaged graphs.
Requirements
- Carry trace context across service calls over HTTP, RPC and message queues.
- Record spans: service, operation, start time, duration, status, parent span and tags.
- Fetch a full trace by ID and show it as a waterfall; search traces by service, operation, duration, error and tag over a time range.
- Keep every trace that contains an error or is unusually slow; sample the rest.
- Show a service dependency map with call rates between services.
Constraints and assumptions
- 2,000 services; 1 million incoming requests per second at peak; an average trace has 30 spans of about 500 bytes.
- Instrumentation may add at most 1% CPU and well under 1 ms of latency to a request.
- The storage budget is about 20 TB of spans per day; traces are kept for 7 days.
- Most spans of a trace arrive within 30 seconds of the request finishing; spans from asynchronous consumers can arrive minutes later.
- Searches over the last hour should return in under 5 seconds.
Out of scope for the first sketch
Log storage and log search · Continuous profiling · Automatic root-cause analysis
Draw your design
Rough math and notes
SYSTEM DESIGN SKETCHBOOK / FOLLOW-UPS
Design a distributed tracing system
Finish your first sketch before answering these. Cross out freely.
- How does trace context get from one service to the next, including through a message queue and a thread pool? What happens if one service in the middle drops it?
- Sampling 1% of requests at the entry point is cheap. Why does it fail the "keep every error" requirement, and what would you do instead?
- Your sampler must see every span of a trace before deciding. How do spans from 30 services end up in the same place, and what if that machine dies mid-trace?
- An engineer searches for checkout traces over 2 seconds with an error in the last hour. What indexes make that fast?
- In a trace, a child span appears to start before its parent. Why, and what do you show?
Compare afterwards: worked approaches and reading at sketchbook /questions/distributed-tracing/review