跪拜 Guibai
← All articles
Backend · Interview · Java

Order API Spikes from 80ms to 4s: Three Thread Pool and Timeout Traps That Looked Normal

By Cosolar ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A single slow dependency can take down an entire order service without a single red metric, and the fixes that feel intuitive often make things worse. Any team running JVM-based microservices will eventually hit these same failure modes, so the concrete patterns here are directly applicable.

Summary

A Java backend order service saw response times jump from 80ms to 4 seconds while CPU, memory, and GC all looked healthy. The root cause chain began with a single shared thread pool used for all remote calls. When one downstream (coupon) slowed down, its slow requests occupied every thread, and because `newFixedThreadPool` uses an unbounded queue, the entire JVM died. Isolating thread pools per dependency seemed like a fix, but throughput dropped 15%: the team had bumped threads to 50 while the HikariCP database pool still had 20 connections, adding only context switching and lock contention.

The second fix—aligning thread and connection pools—uncovered the nastiest trap. During a load test, the gateway's 2-second timeout on the order endpoint forced retries while downstream timeouts were set at 5 and 8 seconds. Each retry spiraled into more coupon calls, creating a retry storm that took the coupon service down entirely. The article walks through how a positive feedback loop forms when every layer retries independently, then prescribes three principles: isolate shared resources with bulkheads and bounded queues, treat timeouts as a decreasing budget across layers, and put circuit breakers on failure propagation.

Takeaways
— A shared thread pool for all remote calls lets one slow dependency occupy all threads and block unrelated requests.
— `Executors.newFixedThreadPool` uses an unbounded `LinkedBlockingQueue` with a capacity of `Integer.MAX_VALUE`, which can exhaust memory under load.
— Raising business thread count above the database connection pool size adds context switching and lock contention without increasing useful throughput.
— HikariCP's pool sizing formula is `connections = ((core_count * 2) + effective_spindle_count)`, recommending small pools with threads queued outside.
— An Oracle experiment cut a connection pool from 2048 to 96 and dropped response time from ~100ms to ~2ms, a 50x improvement.
— Timeout budgets must decrease downstream: each layer's timeout should be smaller than the one above it to avoid orphaned requests.
— Google SRE's per-client retry budget of 10% can suppress retry storms from ~3x traffic growth to ~1.1x.
— An "overloaded, don't retry" error code and circuit breakers can stop positive feedback loops at the source.
Conclusions

The three pitfalls are not independent bugs but progressive symptoms of one design flaw: unbounded resource sharing and failure propagation. Fixing one without addressing the underlying boundary principle just moves the damage downstream.

Green dashboards give a false sense of safety when the real risk sits in unbounded queues, absent retry budgets, and silent thread contention that never trips conventional alerts.

The most counterintuitive correction is that more threads and bigger connection pools can degrade throughput, contradicting the common instinct to scale up horizontally when latency rises.

Concepts & terms
Bulkhead pattern
An isolation strategy that divides resources (like thread pools) into separate compartments per dependency, so a failure in one service doesn't consume resources for all others, analogous to ship hull compartments.
Combinatorial retry explosion
A retry storm where each layer of a call stack independently retries failed requests, causing retry counts to multiply rather than add, potentially overwhelming downstream services.
Timeout budget
The practice of allocating a decreasing time allowance as a request travels downstream, ensuring each lower layer has a shorter timeout than its upstream caller to prevent orphaned work and cascading failures.
Per-client retry budget
A cap on the proportion of a client's requests that can be retries (typically 10%), reset over time, to prevent aggregate retry volume from amplifying overload.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗