about the author

Name: Ivan Loire

GitHub: iloire

Twitter: ivanloire

Location: Sydney (Australia)

from the author

Example of what Node.js is really good at: a single threaded HTTP monitor

by Ivan Loire on Monday, December 19, 2011

An HTTP monitor is a service that pings web sites or services in predefined intervals to check if they are up and running as they are supposed to.

How would you write a program of this kind?

If you are using single threaded programming model, you may need to do something like this:

    function ping (host){
        // do stuff, record status in database, etc...
    }

    while (true){ //let's create a endless loop
        foreach (var host in list_of_hosts)
        {
            ping (host)
        }
        sleep (5000) //sleep until next loop. Here were are pinging every 5 seconds.
    }

There are several problems with this approach:

The second approach could be by creating threads, one for each ping request:

    function ping (host){
        // do stuff, record status in database, etc...
    }

    while (true){ //let's create a endless loop
        foreach (var host in list_of_hosts)
        {
            var thread=new thread(); //use threadpool or something...
            thread.work (ping (host));
        }
        sleep (1000) //sleep until next loop
    }

With this approach, we are creating a separate thread to run each ping request, so each request doesn't depend on the previous one, so we can count on having the right ping intervals between each request.

The problems we face now are:

Node.js approach: No threads, async calls

Node.js is fantastic for this kind of job. Since everything is async and non-blocking, you can just do something like this:


    function ping (host, callback){
        //do http request here.
        //when finish, call "callback"
        callback(status);
    }

    function query_host (host){
        ping(host, function (status) {
            //..  do stuff like saving status in database, etc.
            setTimeout (query_host(host), 2000) //queue for next ping in the next predefined interval
        }));
    }

    //main
    foreach (var host in list_of_hosts)
    {
        setTimeout (query_host(host), 2000) //queue job. Every 2 seconds, query_host will be called.
    }

By the async nature of node.js, where all I/O is evented and asynchronous, every ping request is issued and then queued, waiting for response. Once the response is received, the proper callback is called. There is no blocking code anywhere, and memory footprint for an opened socket is low, so your server can handle hundreds or thousands of requests per second easily :-)

Try that with threads! :)

Real life example: Watchmen, HTTP monitor in Node.js

I have created an HTTP Monitor in Node.js, so have a look at GitHub.

Download HTTP monitor source code in GitHub

Http monitor in Node.js is available in Github

Note: this article is publicly available on GitHub. Feel free to pull request if you want to contribute.


View the discussion thread.blog comments powered byDisqus