FailSafe Vs FailFast

Define: Fail-fast

Fail Fast of a property of a system/application/module by Virtue of which it reports immediately upon failure.

In Java, the mechanism of reporting failure is throwing ConcurrentModificationException

How does iterators provide fail-fast functionality?

Iterators (for example Iterator implemented in HashMap) keeps a variable “modification count” maintained by the parent collection. Iterator also maintains a “iterator modification count” in its implementation. Both these modification counts should always be some.Thus, in every (overridden) method of iterator, a check is made comparing these modification counts. Whenever there is mismatch in these counts, fail-fast happens via ConcurrentModificationException.

These variables will not be same in case, for example, one thread is modifying a collection/map but not using the current iterator.

Examples of Fail-Fast implementations?

1. Hashtable

2. HashMap

3. ArrayList

4. LinkedList

5. Vector

6. Collections.synchronizedMap

Did you say Vector/Hashtable/SynchronizedMap? They are Thread-Safe, are not they?

Vector/Hashtable and SynchronizedMap are all Thread-safe in the sense that they all the methods are Synchronized. But they do not (cannot) protect against thread safety outside their method operations. Refer to Thread Safety in Collection for more details.

Hashtable is not fail fast, does it mean it is fail safe?

Not fail fast does not mean fail safe. They both are different properties. Hashtable (its enumeration to be precise) is neither fail safe, not fail fast.

What is Fail-Safe?

Fail safe in iterator mean that iterator would not fail at all.How this is done is by copying the list when an iterator is requested (meaning iterator object is created). Thus, no matter what happens to the original list, iterator would always result in same (old) list.

Examples of Fail-Safe?

1. CopyOnWriteArrayList (See next question)

How CopyOnWriteArrayList works?

CopyOnWriteArrayList is a perfect example of failsafe iterator. Java doc on this data structure: The "snapshot" style iterator method uses a reference to the state of the array at the point that the iterator was created. This array never changes during the lifetime of the iterator, so interference is impossible and the iterator is guaranteed not to throw ConcurrentModificationException. The iterator will not reflect additions, removals, or changes to the list since the iterator was created. Element-changing operations on iterators themselves (remove, set, and add) are not supported. These methods throw UnsupportedOperationException.

Another other Important Point?

Interesting to note that Java says fail-fast is not a guaranteed behavior. Reason: it may happen that in multiple threads scenario, concurrent modification is happening but the comparison of modification counts is still correct (little chances but possible).

Fail-fast is not limited to multiple threads modifying the collection. Even when same thread modifies the collection (while iterating, removing something without using iterator.remove) will cause fail-fast (ConcurrentModificationException). Thus ArrayList iterator throwing ConcurrentModificationException does not mean it is thread safe.

Note from above: ConcurrentModificationException has nothing to do with the concurrency in the sense of multithreading or thread safety. It means modification while iterating.

References

  1. http://www.ibm.com/developerworks/library/j-jtp07233/index.html