Prompt
I'm troubleshooting a database that falls over under traffic spikes
Latest observation
Here’s a practical way to troubleshoot a database that collapses during traffic spikes.
1) First, identify the failure mode
When it “falls over,” figure out what exactly breaks:
- CPU saturation: CPU pinned at 100%, queries slow, threads queued
- Memory pressure: swapping, OOM kills, buffer cache thrashing
- Disk I/O bottleneck: high latency, slow commits, checkpoint stalls
- Connection exhaustion: too many open connections, pool exhaustion
- Lock contention / deadlocks: lots of waiting, timeouts, stalled queries
- Replication lag: readers stale, failover issues, write pressure
- Network bottleneck: packet loss, latency, bandwidth saturation
Check during the spike:
- DB CPU / load average
- RAM / swap
- Disk IOPS and latency
- Active connections
- Lock waits / deadlocks
- Query latency distribution
- Slow query log / query plan changes
- Replication lag
2) Capture evidence during the spike
You want data from the exact bad period:
- Top queries by total time
- Queries with highest p95/p99 latency
- Slow query log
- Execution plans for the worst offenders
- Connection counts over time
- Lock wait graphs
- DB metrics correlated with app traffic
If you don’t already have observability, add:
- DB monitoring dashboard
- Slow query logging
- APM tracing from request → query
- Periodic metrics snapshots
3) Common root causes during spikes
A. N+1 queries or chatty application code
Traffic spike means many more small queries, overwhelming the DB.
Fixes
- Batch queries
- Add caching
- Reduce round trips
- Precompute where possible
B. Missing or ineffective indexes
A query that is “fine” at low traffic can become catastrophic under load.
Fixes
- Identify scans on large tables
- Add/selective indexes on filter/join columns
- Verify the query planner uses them
- Remove low-value indexes that slow writes
C. Connection pool misconfiguration
If the app opens too many DB connections, spikes can cause thrashing.
Fixes
- Limit pool size
- Set sensible queue timeouts
- Use a pooler/proxy if applicable
- Ensure app concurrency matches DB capacity
D. Locking and transaction issues
Long transactions or hot rows can serialize traffic.
Fixes
- Shorten transaction duration
- Avoid touching the same rows repeatedly
- Split hot counters / use sharding or atomic services
- Check isolation level
- Reduce retry storms
E. Write amplification / too much work per request
Each request may trigger multiple writes, index updates, triggers, or cascades.
Fixes
- Simplify write path
- Remove unnecessary triggers
- Defer non-critical writes to async jobs
- Consider append-only/event-driven designs
F. Poor schema or hot spots
Single rows or single partitions become bottlenecks.
Fixes
- Repartition/shard hot data
- Hash partition by key
- Spread counters or use per-entity buckets
- Avoid sequential hotspots if they cause contention
4) Check for query regressions
Sometimes spikes expose bad plans that were rare before.
Look for:
- parameter sniffing
- stale statistics
- plan cache instability
- data skew
- table bloat / fragmentation
Actions:
- Update statistics
- Rebuild/reorganize indexes if needed
- Test the query with production-like data volume
- Compare explain plans before/after spike
5) Look at the app side too
Often the DB is only the symptom.
Check:
- Are retries multiplying load?
- Did a deploy change query patterns?
- Is a background job hitting the same tables?
- Are timeouts too short, causing retry storms?
- Is traffic spike actually a thundering herd after cache expiry?
Mitigations:
- Add circuit breakers
- Rate limit expensive endpoints
- Add request coalescing / cache stampede protection
- Stagger scheduled jobs
6) Immediate mitigation options
If it’s live and hurting:
- Temporarily reduce traffic or shed load
- Increase cache hit rate
- Disable nonessential features/jobs
- Scale read replicas if the workload allows
- Increase pool queue timeouts carefully
- Restart only if you know it clears a known issue — not as a real fix
- Throttle expensive endpoints
- Add temporary indexes if the problem is a clear missing index
7) Long-term hardening
- Capacity test with production-like load
- Run load tests that simulate spike patterns, not just steady load
- Set SLOs and alert on p95/p99 latency, not just CPU
- Add backpressure and rate limiting
- Use caching strategically
- Partition hot data
- Keep transactions short
- Ensure indexing strategy matches query patterns
- Review slow queries regularly
8) A simple triage checklist
During the next spike, answer these:
- What metric hits first: CPU, memory, disk, connections, or locks?
- Which queries dominate time?
- Are there retries or timeouts amplifying load?
- Are there recent code/schema/deploy changes?
- Is the workload read-heavy, write-heavy, or mixed?
- Is one table/row/index the hotspot?
- Are connection pools and DB max connections aligned?
- Is the DB waiting on I/O or locks?
If you want, I can help you build a step-by-step incident checklist or help interpret specific metrics/logs/explain plans if you paste them here.