Quick revision
The short answer
The mapping function passed to computeIfAbsent calls fib, which calls computeIfAbsent on the same map while the outer call is still in the middle of updating it. The Javadoc forbids a mapping function from modifying the map; on Java 8 this could hang forever, and since Java 9 ConcurrentHashMap detects many of these cases and throws IllegalStateException: Recursive update. It fails only from some n upwards because it depends on whether an inner key lands in a bin the outer call has reserved, or a resize needs one; the fix is a plain loop, or a get first and a put after computing outside computeIfAbsent.
What to remember
In this answer, we saw that the memoized fib fails because its mapping function calls computeIfAbsent on the same map while the outer call holds a reserved bin, something the Javadoc forbids. Java 8 could spin forever in that state; Java 9 and later detect many cases and throw IllegalStateException: Recursive update. The failure appears only from some n upwards because it needs an inner key to hit a reserved bin or a resize to reach one. We fixed it with a plain loop, and with a get, compute outside the map, then putIfAbsent pattern that keeps a cache without breaking the rule.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The mapping function passed to computeIfAbsent calls fib, which calls computeIfAbsent on the same map while the outer call is still in the middle of updating it. The Javadoc forbids a mapping function from modifying the map; on Java 8 this could hang forever, and since Java 9 ConcurrentHashMap detects many of these cases and throws IllegalStateException: Recursive update. It fails only from some n upwards because it depends on whether an inner key lands in a bin the outer call has reserved, or a resize needs one; the fix is a plain loop, or a get first and a put after computing outside computeIfAbsent.
Why this question is asked
Memoization, remembering results so they are computed only once, is one of the first optimizations people reach for, and computeIfAbsent looks like it was made for it. That is exactly why this bug is so common: the code is short, elegant and correct for every value we try by hand, and then it throws in production for larger inputs. Interviewers ask it because it tests whether we know that some APIs have rules the compiler cannot check, and whether we can explain why a failure appears only for some inputs. In this answer, we will recap what computeIfAbsent does inside ConcurrentHashMap, follow the recursion until it collides with itself, see why only some n fail, compare Java 8 with newer versions, and fix the code two ways.
1A Quick Recap of computeIfAbsent
computeIfAbsent(key, function) looks up the key, and if there is no value, calls the function to compute one and stores it, all as one atomic step for that key. In ConcurrentHashMap, atomic means the map holds a lock on the key's bin, the slot of the internal array the key hashes to, while the function runs. If the bin is empty, it first installs a placeholder called a reservation node and locks that, so other threads that want the same bin wait instead of computing the same value twice.
Because the map is in the middle of an update while our function runs, the Javadoc sets a rule: the computation should be short and simple and must not attempt to update any other mappings of this map. The memoized fib breaks that rule, because computing fib(k) means calling computeIfAbsent for k - 1 and k - 2 on the same map.
2Following the Recursion Into the Same Bin
Let us run the code on a fresh map, where the table has 16 bins and an Integer key k lands in bin k & 15 for small values.
- computeIfAbsent(n)reserve bin of n, lock it, call the function
- Function calls fib(n - 1)
computeIfAbsenton the same map, same thread - Inner key's bin, or a resize, hits a reserved bin?YesSame thread re-enters its own lockNoRecursion continues safely
- Map finds its own reservationthe bin is mid-update
- Java 9+: throw IllegalStateExceptionJava 8: could loop forever
5 steps in order. Play them, or step through them one at a time.
For fib(10), the outer call reserves bin 10 and calls fib(9), which reserves bin 9, and so on down to bin 2. Every key lands in a different bin, so every inner call finds an empty bin and nothing collides. The rule is still broken, but nothing notices.
As n grows, two things eventually go wrong. An inner key can hash into a bin that an outer call on the same thread has reserved, for example key 2 and key 18 share bin 2 in a 16-bin table. Or, as completed results are stored, the map passes its resize threshold of 12 entries and the same thread starts moving bins to a bigger table, reaching a bin it has itself reserved. Java's monitor locks are reentrant, meaning a thread that holds a lock can enter it again, so the thread does not block on its own reservation. Instead it walks into a half-finished update. In our test on JDK 17 with a fresh map, n = 13 worked and every n from 14 up failed, which matches the resize at 12 entries; on another JDK or with a pre-sized map the first failing n can differ, so check your version.
3Java 8 Versus Java 9 and Later
On Java 8, the inner call found a reservation node that was not a normal entry and not a moved marker. The code did not know what to do with it, released nothing, and went round its retry loop, again and again. The thread spun at 100 percent CPU forever. That was a much nastier symptom than an exception, because the service simply hung.
From Java 9, ConcurrentHashMap checks for this situation: when the thread finds a reservation node in a bin it is about to update, or notices that a bin changed while its own function was running, it throws IllegalStateException with the message Recursive update. It is a detection of some cases, not a guarantee; a recursive update that happens to touch only other bins can still slip through silently. HashMap made a similar change in Java 9 and throws ConcurrentModificationException from computeIfAbsent when the function modified the map, which is what our test printed when we swapped the map type.
4Fix 1: A Plain Loop
Fibonacci does not need a map at all. Computing from the bottom up needs only the last two values, so it is faster, uses constant memory and has no recursion depth to worry about.
The general lesson is that when a recursive function only depends on smaller inputs, an iterative version is usually the cleanest fix for a memoization problem.
5Fix 2: Compute Outside the Map, Then Store
When the recursion is real, for example a dependency graph where each node's value depends on others, we keep the cache but move the computation out of the mapping function. We check with get, compute normally, and store the result with putIfAbsent, so the map is never modified from inside one of its own operations.
The price is that two threads may compute the same value at the same time and one result is thrown away. For a pure function like this one, that is harmless: both get the same answer. If the computation were expensive or had side effects, we would store a CompletableFuture per key instead, which is a different pattern for a different problem.
6Follow-up questions
Why does the bug not show up in unit tests?
Because tests usually call fib with small values like 5 or 10, where every key gets its own bin and no resize happens during the recursion. The failure depends on table size, hashing and the resize threshold, so it appears only once n is large enough, which is why it tends to show up first in production.
Is it safe to call computeIfAbsent on a different map inside the function?
It does not break the first map's own rule, but it holds the first map's bin lock while working on the second map. If another thread does the reverse, locking the second map's bin and then calling into the first, the two threads can deadlock. Keep mapping functions short and free of other locks.
7Conclusion
In this answer, we saw that the memoized fib fails because its mapping function calls computeIfAbsent on the same map while the outer call holds a reserved bin, something the Javadoc forbids. Java 8 could spin forever in that state; Java 9 and later detect many cases and throw IllegalStateException: Recursive update. The failure appears only from some n upwards because it needs an inner key to hit a reserved bin or a resize to reach one. We fixed it with a plain loop, and with a get, compute outside the map, then putIfAbsent pattern that keeps a cache without breaking the rule.
Open with
The mapping function I pass to computeIfAbsent calls fib, and fib calls computeIfAbsent on the same map. So while the outer call holds the lock on its key's bin, and has put a reservation node there because the bin was empty, the same thread starts modifying the map again.
Then, if they want more
The Javadoc says the mapping function must not update the map, and this is why. Java locks are reentrant, so the thread does not block on its own lock; it walks into its own half-finished update. On Java 8 that could loop forever at full CPU.
From Java 9, ConcurrentHashMap detects many of these cases and throws IllegalStateException with Recursive update. It only fails for larger n because small n puts every key into its own empty bin and no resize happens.
Once an inner key hashes into a bin an outer call has reserved, or the map passes its resize threshold mid-recursion, it hits its own reservation. In a quick test on JDK 17 with a fresh map, 13 worked and 14 failed.
For the fix, Fibonacci does not need a map at all: a simple loop with two variables is faster and uses constant memory. If the recursion is real and I want caching, I check with get first, compute the value outside the map, and store it with putIfAbsent.
Two threads might compute the same value once, which is fine for a pure function.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8, Java 9, Java 17
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "This memoized Fibonacci works for small n but fails for larger n on Java 9+ with...": https://scrivio-interview-library.netlify.app/java/concurrent-collections/7767d35f8a/