Modern systems integrate with a lot of external dependencies. Most of these dependencies are outside our control. While requesting content from one of them, it could happen that it does not reply our requests. Even worse would be if it decides to answer our requests at a rate of one byte per second!

What could we do in this case? That is where Timeouts comes to rescue!

Although this technique has been around for decades, developers still don’t provide sane Timeout values most of the time. It then becomes the burden of the Operating System, Virtual Machine or framework.

One major problem that might arise from this is:

  1. An external dependency faces difficulties answering requests.
  2. Our system makes tons of requests to this external dependency.
  3. Our system accumulates tons of threads waiting for a response, that might never come.
  4. As it accumulates more and more waiting threads it becomes unable to handle new requests.
  5. Eventually our server crashes due to insufficient memory.

It does not seem too much asking developers to define a Timeout value for external requests. In case you are developing using Node.js, as I am right now, it might be as easy as:

const request = require('request');
options = {
  uri: 'http://www.google.com',
  timeout: 5000
};
const requestCallback = (err, data) => {
  console.log(data);
};
request(options, requestCallback);

In case you are interested in learning more about Stability Patterns, read Michael T. Nygard’s book Release it!. It inspired me writing this short blog post.