Fail Fast and Fail Safe Design Principles — With Java Code Examples

Shruti Kulkarni
3 min readMay 8, 2024

--

Photo by Markus Spiske on Unsplash

Lets quickly understand the definition, advantages and disadvantages of both Fail fast & Fail safe. This is a common question asked in interviews.

Fail Fast -

Fail-fast is a design principle that emphasizes detecting and reporting errors as soon as they occur. The idea is to halt the system’s operation or throw an exception immediately upon detecting an error, preventing the error from propagating further.

  • Purpose: Fail-fast aims to minimize the impact of errors by stopping execution as soon as an issue is detected. This helps in identifying and fixing problems early in the development process, reducing the likelihood of hidden bugs causing more significant issues later on.
  • Example: Checking input parameters for validity at the beginning of a method and throwing an exception if they are incorrect is a common implementation of fail-fast.

Advantages:

  • Early error detection: Fail-fast systems identify errors as soon as they occur, making it easier to diagnose and fix issues.
  • Prevents cascading failures: By stopping execution upon detecting an error, fail-fast systems prevent further damage or incorrect behavior.

Disadvantages:

  • Potential for disruption: Fail-fast systems can interrupt the normal flow of execution, leading to a less seamless user experience.
  • May require additional error handling: When errors are detected early, the system needs mechanisms to handle and report them effectively.
public class FailFastExample {

public static void main(String[] args) {
// Creating a map with some initial data
Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);

// Iterating over the map and trying to modify it concurrently
for (String key : map.keySet()) {
if (key.equals("Two")) {
// Modifying the map while iterating (fail-fast behavior)
map.remove(key); // This will throw ConcurrentModificationException
}
}
}
}

Fail Safe —

Fail-safe is a foundational design principle in engineering and software architecture, focusing on the creation of systems that can continue to operate safely even in the presence of component failures or unforeseen errors. Fail-safe mechanisms prioritize system stability and resilience, ensuring that critical functions remain operational under adverse conditions.

  • Purpose: Fail-safe strategies aim to mitigate the impact of failures by implementing redundancy, backup systems, or graceful degradation mechanisms. These measures safeguard against catastrophic system failures, maintaining essential functionalities and preventing disruptions to services or operations.
  • Example: Employing redundant hardware components, fault-tolerant algorithms, or failover systems to seamlessly transition operations to backup resources in the event of primary system failures demonstrates the fail-safe principle’s application.

Advantages:

  • Safety and reliability: Fail-safe systems prioritize safety by ensuring that failures do not lead to dangerous situations.
  • Graceful degradation: The system can continue to operate at a reduced capacity, minimizing disruptions.

Disadvantages:

  • Complex design: Implementing fail-safe mechanisms can add complexity to the system, increasing development and maintenance costs.
  • Performance impact: Fail-safe systems may sacrifice performance in favor of safety and reliability.
public class FailSafeExample {
public static void main(String[] args) {
// Creating a blocking queue with a capacity of 2
BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(2);

// Adding elements to the queue
queue.offer(1);
queue.offer(2);

// Trying to add a third element, which would normally block in a full queue
queue.offer(3); // In a fail-safe approach, this operation might return false or silently ignore the addition

// Printing the contents of the queue
System.out.println("Elements in the queue: " + queue);
}
}
public class FailSafeExample {
public static void main(String[] args) {
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
map.put("One", 1);
map.put("Two", 2);

// Iterating over the map using keySet
for (String key : map.keySet()) {
if (key.equals("Two")) {
// Removing an element from the map while iterating (fail-safe behavior)
map.remove(key);
}
}
System.out.println("Map after removal: " + map);
}
}

--

--

Shruti Kulkarni

This is a personal blog which I write on Java topics. They help me brush up my knowledge for interviews and just in general.