Prompt
How do I tell if an app slowdown is code or database?
Latest observation
A good way to tell is to measure where the time is spent and then correlate app traces with database metrics. In practice, you’re looking for three patterns:
1) If the app is slow, is it waiting on the DB?
Check request traces/APM if you have them.
Likely database problem if:
- Most of the request time is inside SQL calls
- The same endpoint gets slower when DB load increases
- Queries show high latency, lock waits, or lots of rows scanned
- The app thread is mostly waiting, not CPU-bound
Likely code problem if:
- Time is spent before/after DB calls
- There are many small DB queries in a loop
- CPU is high in the app but DB is idle
- Slowdowns happen even when DB metrics look normal
2) Compare app metrics vs DB metrics
Look at both at the same time.
Signs it’s the DB:
- High query latency
- High lock contention / deadlocks
- Connection pool exhaustion
- High I/O on the database
- Slow queries in the DB slow query log
- CPU, memory, or disk saturation on DB server
Signs it’s the code:
- High app CPU
- High GC / memory pressure
- Excessive serialization/deserialization
- Slow external API calls
- N+1 query patterns
- Inefficient loops or sorting in app code
3) Isolate with simple tests
A few quick experiments help a lot:
Disable or stub DB access
If the endpoint still runs slowly without DB calls, it’s likely code or another dependency.
Run the same query directly in the DB
If the query is slow in the database console, it’s a DB/query issue.
Measure query time vs total request time
If one request takes 2s and the DB query takes 1.8s, the DB is the main culprit. If request is 2s and DB time is only 50ms, it’s likely app code or another service.
4) Look for classic symptoms
Database issue clues
- Slow during peak traffic only
- Gets better when fewer users are active
- Missing indexes
- Table scans on large tables
- Lock waits from concurrent writes
Code issue clues
- Slow on specific inputs
- Slower after a deployment
- Only certain endpoints are affected
- Performance worsens with larger payloads or bigger loops
5) Practical workflow
- Pick one slow endpoint.
- Capture a trace/profiler sample.
- Break down time into:
- app code
- DB queries
- external calls
- Check DB slow query log and execution plans.
- Check app CPU, memory, GC, and thread/connection pools.
- Fix the biggest time sink first.
Rule of thumb
- If time is in SQL, it’s DB/query-related.
- If time is outside SQL, it’s app code or another dependency.
If you want, I can give you a step-by-step troubleshooting checklist or show how to do this in tools like Datadog, New Relic, AppDynamics, or PostgreSQL/MySQL logs.