Skip to main content
← PROBLEMS / LOW-LEVEL-DESIGN
An end-to-end domain learning journey

Preventing Recursive computeIfAbsent() Bin-Lock
Deadlocks & I/O Stalls.

Recursive or I/O-heavy lambdas inside ConcurrentHashMap.computeIfAbsent() trigger IllegalStateException: Recursive update on hash-bucket collisions and stall unrelated keys.
Start the journey3 stages· Last modified Nov 28, 2026
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When fixing ReservationNode contention, decouple map insertion from execution using CompletableFuture and 2-arg remove() on error.

TL;DR30-second simplified summary
▾

putIfAbsent(key, future) shrinks the bin lock to nanoseconds and prevents both recursive update errors and I/O convoying.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
Java ConcurrentHashMap bin-level synchronization
Core prerequisite: Java ConcurrentHashMap bin-level synchronization.
Thread dump analysis for blocked worker pools
Core prerequisite: Thread dump analysis for blocked worker pools.
Full walkthrough · 3 stages
01
STAGE 01L4 · FUNDAMENTALS

Spotting the ReservationNode Lock Trap.

Thread dumps show worker threads blocked on ConcurrentHashMap$ReservationNode while one thread waits on HTTP I/O or recurses into the same map.

Bucket Lock Contention

ReservationNode Lock Trap vs. Future Coalescing

Before · computeIfAbsent() Bin Lock
  • Thread T1 locks Bucket 7
  • Recurses computeIfAbsent(parent)
  • IllegalStateException / I/O stall
After · putIfAbsent(Future) Coalescing
  • Thread T1 inserts Future in Bucket 7
  • Bucket 7 lock released in nanoseconds
  • Recurses safely outside bin lock
02
STAGE 02L4 · FUNDAMENTALS

Implementing Non-Blocking Future Coalescing.

Use putIfAbsent(key, created) to atomically claim computation ownership without holding the bucket lock during execution.

Singleflight Lifecycle

Non-Blocking Future Coalescing States

allocate CompletableFutureexisting == null (owner)compute(key) finishesCache Misscache.get(key) returns nputIfAbsent(Futur…Brief bin lock claims owCompute OutsideLockOwner runs compute(key);Future Completedcomplete(value) or remov
CompletableFuture<Rule> created = new CompletableFuture<>();
CompletableFuture<Rule> existing = cache.putIfAbsent(key, created);
if (existing == null) {
    try { created.complete(compute(key)); }
    catch (Throwable t) { cache.remove(key, created); created.completeExceptionally(t); }
    return created.join();
}
return existing.join();
03
STAGE 03L5 · APPLIED DESIGN

Cycle Detection & Bounded Retries.

For recursive dependency graphs, pass a ThreadLocal visited set outside the map lock to catch true data cycles before they cause StackOverflowError. [INSERT YOUR BENCHMARK / PROD NOTE] for production pool sizing.

Cycle-Safe Resolution

ThreadLocal Cycle Guard + Unlocked Future Coalescing

Caller ThreadEntry PointThreadLocal<Set>Cycle GuardConcurrentHashMapputIfAbsent(Future)External I/O / DAGUnlocked Computecheck visitedacyclic: claim Futureowner computesunlocked
Engineering Decision Framework
Problem
How to deduplicate concurrent cache misses and resolve hierarchical keys in ConcurrentHashMap without ReservationNode bin-lock deadlocks or I/O convoying.
Naive Solution
Call map.computeIfAbsent(key, k -> resolveOrFetch(k)) directly inside the mapping lambda.
Why It Fails
ConcurrentHashMap holds a synchronized bin lock on ReservationNode for the entire lambda; same-bucket recursive lookups throw IllegalStateException and slow I/O stalls all unrelated keys in that bucket.
Alternatives
Synchronize externally on a global lock, use Caffeine AsyncLoadingCache, or use putIfAbsent(key, CompletableFuture) coalescing with a ThreadLocal visited set for DAG cycles.
Engineering Decision
Use putIfAbsent(key, created) with CompletableFuture coalescing whenever a loader performs I/O, acquires other locks, or recurses into the same map; reserve computeIfAbsent() for trivial pure in-memory transformations like map.computeIfAbsent(k, x -> new ArrayList<>()).
Trade-Offs
Shrinks bin lock hold time to nanoseconds and eliminates recursive update exceptions, at the cost of one CompletableFuture allocation per miss and explicit 2-arg remove(key, created) cleanup on failure.
Decision Exercise · Make the Call

If Thread 1 inserts Future_A via putIfAbsent() and Thread 2 calls Future_A.join() while Thread 1 is blocked on HTTP, are other keys in Bucket 7 blocked?

ChoiceWhat it buysWhat it costs
ConcurrentHashMap.computeIfAbsent()Concise single-line API for pure in-memory defaults Zero extra Future object allocation on cache missSame-bucket recursion throws IllegalStateException: Recursive update Slow I/O inside lambda blocks all unrelated keys hashing to the same bin
putIfAbsent(key, CompletableFuture) CoalescingSafe under same-bucket recursion (zero bin lock held during compute) Zero bin blocking for unrelated keys during slow I/O Transient exceptions cleanly evicted via 2-arg cache.remove(key, created)Allocates a CompletableFuture per cache miss Requires explicit ThreadLocal visited set for true DAG cycle detection
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 2
Score: 0/2

In a thread dump of a stalled service, threads are BLOCKED waiting on ConcurrentHashMap$ReservationNode. What is the root cause?

Recommended Learning Flow