skip to content

Why did the Java library designers make most collections fail-fast rather than fail-safe, and when does that decision break down?

level: principalimportance: nice to knowfreq 30%

answer

  1. unsynchronized collections, fail-fast iterators
  2. CME = cheap early bug detector, not thread-safety
  3. a guarantee would need locking on every access
  4. breaks down under real concurrency / false confidence
  5. fix = right structure by access pattern, not catch CME

basics

~20 s

The docs state no motive; the usual reading is that single-threaded use is the common case, so iteration stays cheap and misuse usually fails loudly at the next check. It breaks down when collections are shared across threads, where concurrent collections fit instead.

solid answer

~50 s

The docs state the design, not the motive: the general-purpose collections are unsynchronized, and their fail-fast iterators 'fail quickly and cleanly (rather than behaving erratically)'. The usual reading is that they target single-threaded use: iteration stays cheap (no copying, no synchronization, live data) and the common 'modify while iterating' mistake usually becomes a localized ConcurrentModificationException at the loop's next step, instead of silent, data-dependent corruption. Detection costs a single int comparison per next() and is best-effort and unsynchronized; guaranteeing it under concurrency would require locking that single-threaded users would pay for. The decision breaks down when the collection is shared and mutated concurrently: fail-fast then gives false security (it might catch a race or might not), so it's documented as a bug-detection aid only. The proper response is to choose data structures by access pattern — concurrent collections (weakly-consistent iterators) for shared mutation, copy-on-write for read-mostly shared lists — rather than relying on or fighting CME.

go deeper

for a junior

Understands that fail-fast exists to catch the common mistake of changing a collection while looping over it, and that concurrent collections behave differently.

for a middle

Can explain that fail-fast is cheap and best-effort, and that it's not a real thread-safety mechanism.

for a senior

Articulates the cost/benefit (free detection vs no concurrency guarantee), why catching CME for concurrency is an anti-pattern, and the view-aliasing surprise.

for a principal

Frames fail-fast as an error-detection default rather than a concurrency control, reasons about the synchronization tax of guaranteeing it, and drives data-structure selection (or de-sharing) by access pattern across a system.

## The design question Why is `ArrayList`/`HashMap` iteration **fail-fast** (throws `ConcurrentModificationException`, CME, on modification during iteration) instead of **fail-safe** (tolerating modification)? And where does that choice stop serving you? ## Background terms - **Fail-fast iterator:** detects structural modification (an add, a remove or another change that perturbs iteration) via a `modCount`/`expectedModCount` integer comparison and throws CME at its next check. - **Fail-safe / weakly-consistent / snapshot iterator:** never throws CME; either tolerates concurrent change (weakly consistent, e.g. `ConcurrentHashMap`) or iterates a frozen copy (snapshot/copy-on-write, e.g. `CopyOnWriteArrayList`). - **Structural modification:** adds or removes elements or otherwise perturbs iteration, as `ArrayList.sort` does; replacing a value in place is not structural. ## The reasoning behind fail-fast as the default The Collections Framework Overview states what was built: unsynchronized general-purpose implementations, synchronization wrappers for those who want them, and fail-fast iterators that 'fail quickly and cleanly (rather than behaving erratically)'. The cost argument below is a reading of that design, not a quoted rationale. **1. The common case is single-threaded.** The original Collections Framework (Java 2) was built for general-purpose, mostly single-threaded data structures. The framework separated *unsynchronized, fast* collections from any thread-safety concern (the older synchronized `Vector`/`Hashtable` were de-emphasized; `Collections.synchronizedXxx` wrappers were offered for opt-in synchronization). **2. The most frequent bug is 'mutate while iterating' — by one thread.** Removing inside a for-each is an extremely common mistake. Without detection, the iterator would silently skip elements or read stale slots, producing **heisenbugs**: wrong results that depend on data and timing and surface far from the cause. Fail-fast usually converts that into a **loud, localized** exception at the loop's next step. This is the same philosophy as failing fast on `null` or array bounds: detect programmer error as early and as close to the source as possible. **3. Detection must be nearly free for the common case.** A single unsynchronized `int` compare per `next()` costs essentially nothing and needs no locking. Making it a *guarantee* under concurrency would require memory barriers/locks on every structural change and every `next()` — a tax on single-threaded users to serve those who should be using a different class anyway. The Javadoc states the limit: fail-fast behavior 'cannot be guaranteed' and should be used 'only to detect bugs'. **4. Freshness and no copying.** Fail-fast iteration reads the live data and allocates only the iterator — unlike copy-on-write collections, which pay for their snapshots with an array copy on every write, or weakly-consistent ones that accept staleness. ## Where the decision breaks down **A. Genuine concurrency.** When two threads share a plain `HashMap` and one mutates while the other iterates, fail-fast gives **false confidence**: the `modCount` read is unsynchronized, so CME may fire or may not. Worse, an unsynchronized `HashMap` under concurrent writes can corrupt internally, and CME does nothing to prevent that: entries get lost, and a thread can hang. On JDK 25 and 27 a writer can spin inside a corrupted tree bin, where `HashMap$TreeNode.root()` is an unbounded parent walk; before JDK 8 a resize relinked bucket chains in reverse, so racing resizes could link a chain into a cycle and leave a later traversal spinning. The correct fix is not to catch CME but to switch classes. **B. Over-catching CME.** Teams sometimes wrap iteration in `try/catch (ConcurrentModificationException)` and retry, treating it as a concurrency signal. This is an anti-pattern: it's unreliable and masks a real design problem (shared mutable state without proper concurrency control). **C. Hidden view aliasing.** `subList`, `keySet`, `entrySet` check the parent's `modCount`; modifying the parent while iterating a view trips CME even single-threaded — surprising if you don't know the views alias. ## The principled resolution: pick by access pattern - **Single-threaded / externally synchronized →** plain fail-fast collections; treat CME as a welcome bug detector. - **Shared, frequently mutated →** concurrent collections (`ConcurrentHashMap`, `ConcurrentLinkedQueue`) with weakly-consistent iterators (never throw, may see partial/late updates). - **Shared, read-mostly, write-rare →** copy-on-write (`CopyOnWriteArrayList/Set`): O(n) per write, lock-free reads, snapshot iteration. The deeper lesson for a principal-level engineer: fail-fast is not a thread-safety mechanism — it's an *error-detection default*, documented for detecting bugs. Recognizing that, you stop trying to make CME do concurrency's job and instead model shared mutable state with the right structure (or eliminate the sharing).

  • If fail-fast detection isn't reliable for concurrency, why include it at all?
    Because it catches the common single-threaded 'modify-while-iterating' bug cheaply and, best-effort, concurrent modification too: the Javadoc names both cases. It's a debugging aid, documented as best-effort and not a concurrency guarantee.
  • What can go wrong with a plain HashMap shared across threads, beyond CME?
    Far worse than CME: unsynchronized concurrent writes can corrupt the internal structure. Entries get lost, and a thread can hang: in a tree bin on JDK 25 and 27, in a bucket chain that racing resizes linked into a cycle before JDK 8. CME detection does nothing to prevent this; you must use ConcurrentHashMap or external synchronization.

  • Treating fail-fast / CME as a thread-safety mechanism rather than a bug detector.
  • Catching CME and retrying as a way to 'handle' concurrency.
  • Believing a plain HashMap is safe under concurrent reads/writes as long as no CME is thrown — it can corrupt.
  • Claiming CME is guaranteed to fire on every concurrent modification — it is best-effort.

Collections Framework iterators
Java 1.2

Assumes JDK 27, and JDK 25 LTS behaves the same. The form of a HashMap race is version-sensitive: before JDK 8 a resize reversed bucket chains, so racing resizes could form a cycle; resize has preserved chain order since, and a writer can still hang in a corrupted tree bin.

context