Fix Reliability Bottlenecks Outside Your APIs
In this article
APIs are often the first place engineers look when systems slow down or fail, but many reliability issues live outside the API layer entirely — in databases, background jobs, third-party services, network configuration, and even client-side code. This guide shows you how to systematically find and fix those non-API bottlenecks, so your service stays fast and available even when the API itself is healthy.
What You'll Need
- Access to your application's logs (application, database, web server, and infrastructure logs)
- Monitoring and alerting tools (e.g., Prometheus, Grafana, Datadog, New Relic, or cloud-native tools)
- Profiling tools for your language/runtime (e.g., pprof for Go, py-spy for Python, JProfiler for Java)
- A way to trace requests end-to-end (e.g., OpenTelemetry, Jaeger, Zipkin)
- Understanding of your architecture: which services, databases, queues, and caches are involved
- Permission to run load tests or queries against staging environments
Step-by-Step
- Map your request lifecycle. Draw a diagram of what happens after a user action: from the client, through the API, to databases, caches, queues, and third-party calls. Mark every point where data is read, written, or transformed. This map is your starting point for identifying where time is spent.
- Look for slow database queries. Enable slow-query logging in your database (e.g.,
long_query_timein MySQL,log_min_duration_statementin PostgreSQL). Identify queries that take more than a few hundred milliseconds. UseEXPLAINto check if they are using indexes. Add missing indexes, but avoid over-indexing — every index slows writes. - Check for N+1 query patterns. If your application fetches a list of records and then queries a related table for each one, that's an N+1 bottleneck. Use an ORM profiler or query logging to spot repeated queries with similar patterns. Replace them with joins or eager loading.
- Inspect background job queues. If you use job queues (e.g., Sidekiq, Celery, SQS), check queue length and processing time. A backlog of jobs indicates a bottleneck in the worker — maybe it's CPU-bound, or it's calling a slow external service. Scale workers horizontally or optimize the job code.
- Monitor cache hit rates. If you use a cache (Redis, Memcached), measure hit rates. A low hit rate means you're not caching effectively. Check if cache keys are being evicted too early, or if you're caching the wrong data. Add caching for expensive queries or computations, but be careful about stale data.
- Review third-party service calls. External APIs (payment gateways, email services, etc.) are a common bottleneck. Add timeouts and retries with exponential backoff. Consider circuit breakers to fail fast when a dependency is down. Measure the latency of each call to see which ones are slow.
- Analyze network and infrastructure. Check for network latency between your services, especially if they are in different regions. Look at CPU, memory, disk I/O, and network bandwidth on your servers. A bottleneck can be as simple as a small instance type or a misconfigured load balancer.
- Profile application code. Use a profiler to find CPU hotspots or memory leaks in your application code. Often the bottleneck is not the database but the code that processes data — e.g., heavy JSON serialization, inefficient loops, or blocking calls in event loops.
- Check client-side performance. If your users are on slow networks, large payloads can be a bottleneck. Compress responses (gzip), enable HTTP/2, and consider pagination for large lists. Also, check if your frontend is making too many requests — combine them where possible.
- Load test your system. Use tools like k6, JMeter, or Gatling to simulate traffic and observe where latency spikes. Test the database, the API, and the background workers separately. This helps you find bottlenecks that only appear under load.
- Implement end-to-end tracing. Use distributed tracing to follow a single request through all services. This shows you exactly where time is spent — in the database, in a queue, or in a third-party call. Look for spans with high latency or errors.
- Prioritize and fix. Based on your findings, fix the most impactful bottlenecks first. For database issues, optimize queries or add indexes. For code issues, refactor or add caching. For infrastructure, scale up or out. After each fix, measure again to confirm improvement.
Common Mistakes
- Ignoring background jobs. Even if your API responds fast, a backlog of background jobs can cause stale data and slow processing. Always monitor job queues.
- Over-caching. Caching everything can lead to stale data and memory pressure. Cache only what is expensive to compute and changes infrequently.
- Forgetting about connection pools. Database connection pools can become a bottleneck if the pool size is too small or connections are not released properly. Monitor pool usage.
- Treating symptoms, not causes. Adding more servers to fix a slow database query is a temporary fix. Fix the query first.
- Not setting timeouts on external calls. Without timeouts, a slow third-party service can tie up your workers and cause cascading failures.
Tips
- Start with the database — it's the most common bottleneck outside the API.
- Use real traffic data to prioritize. If 90% of your requests are reads, focus on read optimization.
- Set up alerts for queue length, cache hit rate, and slow query count. This helps you catch bottlenecks early.
- Document your architecture and known bottlenecks. This helps new team members and during incident reviews.
- Consider using a service mesh if you have many microservices — it gives you fine-grained metrics and traffic control.
Troubleshooting
If you can't find the bottleneck: Start with end-to-end tracing to see where time is spent. If you don't have tracing, add it. Also, check for resource saturation — high CPU or memory can slow everything down, even if the code is efficient.
If the bottleneck disappears after a restart: This often indicates a memory leak or a gradual accumulation of state (e.g., open connections, cached data). Check for leaks in your code or dependencies.
If the bottleneck is intermittent: Look for patterns — is it during peak hours, or after a specific event? Check if a third-party service has known latency spikes. Use load testing to reproduce it.
If the database is slow but queries are fast: Check for lock contention, I/O bottlenecks, or replication lag. Use database monitoring tools to see what's happening.
Frequently Asked Questions
Q: How do I know if the bottleneck is in my code or the database?
A: Use profiling and tracing. If the database query time is small but the total request time is large, the bottleneck is likely in code. If the query time is large, focus on the database.
Q: What's the best way to monitor background jobs?
A: Use a job queue dashboard (e.g., Sidekiq's web UI) or custom metrics for queue length, processing time, and failure rates. Set alerts for high queue length.
Q: Should I cache everything?
A: No. Cache only data that is read frequently and changes rarely. Otherwise, you risk stale data and memory overhead.
Q: How do I set timeouts for external calls?
A: In most HTTP clients, you can set a timeout parameter (e.g., timeout in Python requests, connectTimeout in Java). Use a reasonable value based on the service's SLA (e.g., 2-5 seconds).
Q: What is a circuit breaker and when should I use one?
A: A circuit breaker prevents repeated calls to a failing service, allowing it to recover. Use it when you have a critical dependency that can fail. Libraries like Hystrix (Java) or Polly (.NET) can implement it.
Q: Can client-side code cause reliability issues?
A: Yes, if it makes too many requests or processes large payloads. Optimize by reducing payload size, using pagination, and batching requests.