Skip to main content
← CONCEPTS / CONCURRENCY-SYSTEMS
An end-to-end domain learning journey

Decoupling Computation from
ConcurrentHashMap Bin Locks.

Java's ConcurrentHashMap.computeIfAbsent() holds a synchronized bucket lock for the entire mapping lambda, stalling unrelated keys during I/O and throwing IllegalStateException on same-bucket recursion.
Start the journey3 sections· Last modified Nov 28, 2026
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When memoizing I/O or recursive dependencies in Java, pair putIfAbsent(key, future) with atomic remove(key, created) on failure.

TL;DR30-second simplified summary
▾

Two-phase Future coalescing preserves singleflight deduplication while eliminating same-bucket I/O stalls and recursive update exceptions.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
Java ConcurrentHashMap bucket-level locking (Java 8+ synchronized bin nodes)
Core prerequisite: Java ConcurrentHashMap bucket-level locking (Java 8+ synchronized bin nodes).
CompletableFuture and singleflight request coalescing
Core prerequisite: CompletableFuture and singleflight request coalescing.
Full walkthrough · 3 stages
01
STAGE 01L4 · FUNDAMENTALS

Why computeIfAbsent() Holds a Bin Monitor.

To guarantee at-most-once execution per key, ConcurrentHashMap inserts a ReservationNode and synchronizes on it while executing the mapping lambda.

Bucket Lock Hold Time

Decoupling Computation from Bin Monitor

Before · computeIfAbsent holds bucket lock for 50ms I/O -> unrelated keys in bin stall
  • Lock hold: Full T_compute (50ms I/O)
  • Unrelated keys in same bin: Blocked
  • Same-bucket recursion: IllegalStateException
After · putIfAbsent(key, future) holds bucket lock for ~15ns -> I/O runs outside lock
  • Lock hold: ~15ns pointer insertion
  • Unrelated keys in same bin: Unblocked during I/O
  • Same-bucket recursion: Safe via Future.join()
02
STAGE 02L4 · FUNDAMENTALS

Two-Phase CompletableFuture Coalescing.

Use putIfAbsent() to elect one computation owner while concurrent callers await the same Future without holding any bucket lock.

Singleflight Coalescing

Two-Phase CompletableFuture Coalescing

Thread 1 (Owner)ConcurrentHashMapThread 2 (Waiter)1. putIfAbsent(K, fut1) returnsnull (lock released)2. putIfAbsent(K, fut2) returnsfut1 (lock released)3. Computes V outside lock, calls…4. fut1.join() returns V
CompletableFuture<V> fut = map.get(key);
if (fut == null) {
    CompletableFuture<V> created = new CompletableFuture<>();
    CompletableFuture<V> prev = map.putIfAbsent(key, created);
    if (prev == null) {
        try {
            created.complete(computeOutsideLock(key));
        } catch (Throwable t) {
            map.remove(key, created);
            created.completeExceptionally(t);
        }
        fut = created;
    } else { fut = prev; }
}
return fut.join();
03
STAGE 03L5 · APPLIED DESIGN

Failure Eviction & Amortized Complexity.

Always pair exception handling with map.remove(key, created) (2-arg atomic remove) so a transient failure does not poison the cache forever. [INSERT YOUR BENCHMARK / PROD NOTE] for tail latency under high miss concurrency.

Future Entry Lifecycle

CompletableFuture Coalescing & Failure Eviction State Machine

putIfAbsent(key, created)…computeOutsideLock(key)…catch (Throwable t) ->…next caller retries cleanlyCACHE_MISSmap.get(key) returns nulCOMPUTING_OUTSIDE…putIfAbsent(key, createdCOMPLETED_CACHEDcreated.complete(V) unblATOMICALLY_EVICTEDmap.remove(key, created)
Engineering Decision Framework
Problem
How to memoize blocking I/O, external RPCs, or recursive map lookups in ConcurrentHashMap without stalling unrelated keys in the same bin or throwing IllegalStateException.
Naive Solution
Call ConcurrentHashMap.computeIfAbsent(key, fn) directly for every memoized computation.
Why It Fails
computeIfAbsent() holds a synchronized ReservationNode lock for the entire mapping lambda, blocking unrelated keys hashing to the same bucket and throwing IllegalStateException: Recursive update on same-bucket recursion.
Alternatives
Compare computeIfAbsent() (singleflight but holds bin lock across T_compute), get() then putIfAbsent() (nanosecond lock hold but duplicates work on concurrent misses), and two-phase putIfAbsent(key, new CompletableFuture<>()) with atomic remove(key, created).
Engineering Decision
Use computeIfAbsent() only for pure CPU-bound, non-recursive micro-computations (<1 microsecond); when population involves blocking I/O, external RPCs, or recursive lookups into the same map, store a CompletableFuture via putIfAbsent() and compute outside any map lock.
Trade-Offs
Eliminates bin-lock starvation and recursive update exceptions while preserving singleflight deduplication, in exchange for one CompletableFuture allocation per miss and explicit 2-arg remove(key, created) failure cleanup.
Decision Exercise · Make the Call

Why must exception cleanup call map.remove(key, created) instead of map.remove(key)?

ChoiceWhat it buysWhat it costs
computeIfAbsent(k, fn)Singleflight: YesLock Hold Time: Full T_compute Recursive Safe?: No (IllegalStateException)
get() then putIfAbsent()Lock Hold Time: Nanoseconds Recursive Safe?: YesSingleflight?: No (duplicate work)
putIfAbsent(k, Future)Lock Hold Time: Nanoseconds Recursive Safe?: Yes Singleflight?: YesAllocates one CompletableFuture per cache miss Requires 2-arg atomic remove(key, created) on failure
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 4
Score: 0/4

What happens in Java 17 when ConcurrentHashMap.computeIfAbsent() recursively calls computeIfAbsent() on a different key that hashes to the same bucket?

Recommended Learning Flow