Prompt
I’m debugging a latency spike in a distributed backend how do I trace the exact service or database call causing it
Latest observation
To trace the exact call causing a latency spike in a distributed backend, use end-to-end tracing plus correlated metrics and logs. The goal is to answer: which request path got slow, which span inside it was slow, and why.
1) Start with a trace, not just metrics
Metrics tell you that latency increased. A distributed trace tells you where.
Use an APM/tracing system such as:
- OpenTelemetry + Jaeger / Tempo / Zipkin
- Datadog APM
- New Relic
- Honeycomb
- AWS X-Ray
Look for:
- The slowest traces during the spike window
- The service path with the highest duration
- The span with the largest self-time or child-time
2) Make sure every request has a trace ID
Propagate a trace/context ID across:
- API gateway / ingress
- service-to-service calls
- async jobs / queues
- DB client instrumentation
- cache / message bus calls
Then when a spike happens, you can:
- pick a slow request
- follow its trace through all services
- identify the exact downstream call that bloated latency
3) Inspect the trace waterfall
In the trace timeline:
- Find the longest span
- Compare exclusive time vs inclusive time
- Inclusive: total time including children
- Exclusive/self time: time spent in that span itself
Typical culprit patterns:
- One service span is long: CPU contention, lock contention, GC, thread pool saturation
- A DB span is long: slow query, missing index, lock waits, connection pool exhaustion
- Many small downstream spans add up: chatty N+1 calls
- One downstream dependency dominates tail latency
4) Correlate with database query logs / slow query log
If you suspect DB:
- enable or inspect the slow query log
- capture:
- query text or fingerprint
- duration
- rows scanned/returned
- lock wait time
- execution plan
Then match the trace span to:
- query fingerprint
- transaction ID / connection ID if available
- time window of the spike
Look for:
- missing index
- query plan regression
- lock contention / deadlocks
- connection pool waits
- replication lag affecting reads
5) Check service-level saturation and queueing
Sometimes the “slow call” isn’t slow execution; it’s waiting.
Inspect per service:
- CPU utilization
- GC pauses
- memory pressure
- thread pool saturation
- connection pool exhaustion
- queue depth / consumer lag
- event loop lag
- p95/p99 latency, not just average
A service may show a slow span because requests are queued before being processed.
6) Use logs with trace context
Add structured logs with:
- trace_id
- span_id
- request_id
- user/tenant ID
- dependency name
- query fingerprint
- latency_ms
Then you can search logs for the same trace ID and see:
- retries
- timeouts
- errors
- fallback paths
- circuit breaker trips
7) Narrow down by sampling and exemplars
If full tracing is expensive:
- use tail-based sampling to keep slow traces
- sample traces above a latency threshold
- attach metric exemplars to trace IDs for slow requests
This helps retain the exact traces that matter during spikes.
8) Common root causes to verify
The most common causes of distributed latency spikes:
- slow DB query / bad plan
- lock contention
- connection pool exhaustion
- downstream service slowdown
- retry storms
- thundering herd / cache stampede
- GC pauses
- CPU throttling
- noisy neighbor / node saturation
- network issues / packet loss
- cold starts or autoscaling lag
9) Practical workflow
A good incident workflow:
- Identify spike window from metrics
- Pull the top p99/p999 traces in that window
- Compare the slow trace to a normal trace
- Find the first span with abnormal duration
- Check that service’s logs, saturation metrics, and downstream dependency
- If DB-related, inspect slow queries and execution plans
- Confirm whether retries or queueing amplified the issue
10) If you don’t have tracing yet
Add it now:
- Instrument inbound HTTP/RPC handlers
- Instrument outbound HTTP/RPC clients
- Instrument DB calls
- Propagate context headers
- Export to a tracing backend
- Set up tail-based sampling for slow requests
If you want, I can also give you:
- a step-by-step incident runbook,
- an OpenTelemetry setup example, or
- a SQL/DB slow-query tracing checklist.