Spring in production · senior
Connection pools, memory growth and async work
Pool exhaustion, unbounded caches and unbounded async queues are the usual Spring production failures. Each leaves a clear sign in metrics.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- "Connection is not available, request timed out after 30000ms": every pool connection was busy for 30 seconds. A mostly idle database means connections are held while no query runs.
- Metrics:
hikaricp_connections_active,hikaricp_connections_pending,hikaricp_connections_usage_seconds.spring.datasource.hikari.leak-detection-thresholdlogs a stack trace for a long-held connection. - Cause: open-in-view (on by default). The request's EntityManager holds a connection while the controller calls a slow remote service. Fix: turn it off, return DTOs from short transactions, call remote services outside database work, set timeouts.
- A bigger pool moves the queue into the database. HikariCP's sizing guide favours small pools. Many pods times a large pool can pass the database's connection limit.
- Memory climbing for days: if old-generation usage after GC rises, it is a heap leak. If the heap is flat but process memory rises, look off-heap.
- Cause seen:
@Cacheablekeyed by search text and user id with no cache library on the classpath, so Boot uses an unbounded map. Use Caffeine (maximumSize,expireAfterWrite) or Redis with a TTL. - Other causes: unbounded @Async queues, long transactions, metric tags with unbounded values, ThreadLocals not cleared.
- @Async default executor in Boot: 8 core threads and an unbounded queue, so
max-sizenever takes effect. Emails arrive hours late and are lost on restart. Make sending durable with an outbox row and a worker. Exceptions in an@Async voidmethod only go to theAsyncUncaughtExceptionHandler. Return aCompletableFutureor handle errors. Watchexecutor_queued_tasksandexecutor_active_threads. Virtual threads (spring.threads.virtual.enabled=true) remove the Tomcat thread cap, so thousands of requests reach the pool and the database. Add a concurrency limit (@ConcurrencyLimitor a semaphore) sized to the pool.
The red pen
Common traps: mistake → fix
Mistake: A bigger Hikari pool fixes timeouts
Fix: it moves the queue to the database.
Mistake: A non-@Transactional endpoint cannot hold a connection
Fix: with open-in-view it can.
Mistake: @Cacheable is always bounded
Fix: without a configured cache it is an unbounded map.
Easy to hard
Questions interviewers ask, easy to hard
- The API fails with Connection is not available but the database is idle. What is going on?
- Memory climbs for days after a release. How do you find the cause?
- Order emails arrive hours late. Why?
- Virtual threads were switched on and the database overloaded. Why?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.