Prompt
How do I track down the root cause of slow API responses in production?
Latest observation
To track down slow API responses in production, use a structured approach that separates where time is spent from why it’s happening.
1) First confirm the symptom
Before debugging, answer:
- Is the slowness across all endpoints or just one?
- Is it global or limited to certain regions/clients?
- Is it constant or spiky?
- Does it affect latency, throughput, or error rate?
Look at:
- p50 / p95 / p99 latency
- request rate
- error rate
- timeout rate
- saturation metrics: CPU, memory, GC, thread pool, DB connections, queue depth
If only p99 is bad, that often points to tail-latency causes like DB contention, lock contention, cold caches, or retries.
2) Break down the request path
Instrument the full path so you can see time spent in:
- load balancer / gateway
- application code
- downstream services
- database queries
- cache calls
- external APIs
- serialization/deserialization
- queueing / async workers
If you don’t already have it, add:
- distributed tracing
- structured logs with request IDs
- span timing around major operations
A trace will often show the culprit immediately.
3) Compare fast vs slow requests
Take a sample of:
- one fast request
- one slow request
- one timeout
Compare:
- endpoint
- payload size
- user/account/tenant
- feature flags
- cache hit/miss
- DB query plan
- downstream dependency latency
- retry count
- instance/pod/node
- time of day
Patterns usually emerge quickly: one tenant, one code path, one specific query, or one noisy downstream dependency.
4) Check the usual bottlenecks
Database
Common signs:
- slow queries
- missing indexes
- full table scans
- lock contention
- connection pool exhaustion
- N+1 query patterns
- too many round trips
What to do:
- inspect slow query logs
- review query plans
- check connection pool metrics
- look for deadlocks/lock waits
- verify indexes match query filters/sorts
Caches
Common signs:
- cache miss spikes
- thundering herd on expiration
- hot key contention
- large objects causing serialization overhead
What to do:
- measure hit rate
- check TTLs and stampede protection
- verify cache key cardinality
- look for cache evictions
Application/runtime
Common signs:
- high CPU
- GC pauses
- memory pressure
- thread starvation
- event loop blocking
- lock contention
- heavy JSON serialization
What to do:
- profile CPU
- inspect GC logs / heap usage
- check thread dumps
- look for blocking I/O in async code
- measure serialization time
Downstream dependencies
Common signs:
- one service causes cascading latency
- retries amplify load
- circuit breakers absent or misconfigured
What to do:
- check dependency latency histograms
- examine retry behavior
- verify timeouts are shorter than user-facing deadlines
- add circuit breakers/bulkheads if needed
Infrastructure / network
Common signs:
- noisy neighbors
- packet loss
- DNS slowness
- TLS handshake overhead
- pod/node throttling
- autoscaling lag
What to do:
- check node/pod CPU throttling
- inspect network metrics
- review DNS and load balancer timings
- correlate latency with deploys or scaling events
5) Use tracing to localize the slowdown
If you have distributed tracing, ask:
- Is most time in one span?
- Are spans waiting on downstream calls?
- Is there a gap before the first span starts? That can indicate queueing.
- Do slow requests have extra spans that fast ones don’t?
If you don’t have tracing, add minimal timing logs like:
- auth
- validation
- DB fetch
- enrichment
- external call
- response serialization
6) Correlate with changes
Look at:
- recent deploys
- config changes
- schema changes
- traffic patterns
- feature flags
- dependency releases
A latency regression often starts immediately after a change, especially:
- new query
- added API call
- changed timeout/retry settings
- schema migration
- logging volume increase
7) Reproduce under controlled conditions
In staging or locally:
- replay production-like traffic
- use production-like data volume
- test the specific slow request shape
- compare with and without recent changes
Be careful: staging often hides issues because data size, concurrency, and dependency behavior differ from production.
8) Add guardrails once found
After identifying the root cause, prevent recurrence with:
- latency SLOs and alerts
- tracing on critical paths
- slow query alerts
- dependency timeout budgets
- connection pool monitoring
- p99 dashboards
- load tests in CI/CD
- canary deploys
- rollback criteria
A practical debugging order
If you need a quick playbook, do this:
- Check p95/p99, error rate, saturation.
- Pull a trace for a slow request.
- Identify the slow span.
- Determine whether it’s DB, cache, dependency, or app code.
- Compare against a fast request.
- Review recent changes.
- Confirm with profiling or query analysis.
- Fix and add monitoring.
If you want the fastest path
The three highest-value tools are:
- distributed tracing
- slow query logs
- app/runtime profiling
If you tell me your stack (language, framework, DB, infra, whether you already have tracing), I can give you a more specific step-by-step checklist.