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.
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.
ReservationNode Lock Trap vs. Future Coalescing
- Thread T1 locks Bucket 7
- Recurses computeIfAbsent(parent)
- IllegalStateException / I/O stall
- Thread T1 inserts Future in Bucket 7
- Bucket 7 lock released in nanoseconds
- Recurses safely outside bin lock
Implementing Non-Blocking Future Coalescing.
Use putIfAbsent(key, created) to atomically claim computation ownership without holding the bucket lock during execution.
Non-Blocking Future Coalescing States
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();
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.
ThreadLocal Cycle Guard + Unlocked Future Coalescing
- 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.
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?
| Choice | What it buys | What it costs |
|---|---|---|
| ConcurrentHashMap.computeIfAbsent() | Concise single-line API for pure in-memory defaults Zero extra Future object allocation on cache miss | Same-bucket recursion throws IllegalStateException: Recursive update Slow I/O inside lambda blocks all unrelated keys hashing to the same bin |
| putIfAbsent(key, CompletableFuture) Coalescing | Safe 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: Make the trade-off yourself.
In a thread dump of a stalled service, threads are BLOCKED waiting on ConcurrentHashMap$ReservationNode. What is the root cause?