What is Fail-fast

We are all here to learn something new everyday. Today, I was reading about cloud native again. (on this link) I heard so often about “fail-fast systems”. I did my own research about it and wanted to share some ideas on it.

There are a few principles about fail-fast systems and code. We will talk about all.

Fail-fast Systems:

Fail-fast modules and components are often used in situations where failure in one component might not be visible until it leads to failure in another module.

Finding the cause of a failure is easier in a fail-fast system, because the system reports the failure with as much information as possible as close to the time of failure as possible. In a fault-tolerant system, the failure might go undetected, whereas in a system that is neither fault-tolerant nor fail-fast the failure might be temporarily hidden until it causes some seemingly unrelated problem later.

Developers also refer to fail-fast code to a code that tries to fail as soon as possible at variable or object initialization. In OOP, a fail-fast designed object initializes the internal state of the object in the constructor, launching an exception if something is wrong (vs allowing non-initialized or partially initialized objects that will fail later due to a wrong “setter”). The object can then be made immutable if no more changes to the internal state are expected. In functions, fail-fast code will check input parameters in the precondition. In client-server architectures, fail-fast will check the client request just upon arrival, before processing or redirecting it to other internal components, returning an error if the request fails (incorrect parameters, …). Fail-fast designed code decreases the internal software entropy, and reduces debugging effort.

Fail-fast Development & Code

There is an another terminology about it. Its called “test driven development”. Lets go on and explain it on some coding example.

Lets assume that you are reading keyword from a property file. And you are sending requests via TwitterAPI.

public String getKeyword() {
String keyword = getProperty("twitter.api.search.query.keyword");
if(keyword == null)
return String.EMPTY;
return keyword;

What if we apply fail-fast principle In our method?

public String getKeyword() {
String keyword = getProperty("twitter.api.search.query.keyword");
throws new NullReferenceException("keyword can't be null or empty");
return keyword;

To be continued with examples, pros and cons.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.