Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forever or domain - which of them is better for node.js continuous work [closed]

I want my node app run continuously. I'm sure that some crashes can appear during the app's work. Nowadays i can see 3 ways of making an app work continuously:

  1. Start my app with forever, so when the app crashes forever will restart it automatically
  2. Use domain module to catch errors and start the app again when "error" event is emitted
  3. deprecated process.on('uncaughtException')

The question is: which of these 3 ways is better to use. It seems to me like there's no exact answer to this question, so any comments are welcome.

In my opinion the first one is more appropriate because node process memory consumption doesn't grow with time - each time the node process crashes forever starts another one.

like image 349
Dmitrii Sorin Avatar asked Jan 30 '13 19:01

Dmitrii Sorin


1 Answers

Ah, that's a tricky question. First of all, domains approach differs from forever in that it doesn't force you to restart the whole Node process. Say, for example, your Node application processes requests coming from several clients simultaneously. By carefully configuring your domains you (at least, in theory) will be able to prevent other requests from failing when one of the requests throws an error.

However, in practice to get domains work some components of your application has to be domain-aware. That applies to third-party components, too. For example, a database connection module that uses a pool of connections internally, should not wrap them into it's own domain, but rather check if the callback has a domain attached to it already. Otherwise, an exception thrown in a database code would be caught in a module's own domain and your domain wouldn't know about it. So, in order to use domains with third-party code you have to heck first if that code was written with domains in mind.

forever simply restarts your application whenever it crashes. It sounds like a worse idea than domains, but it also doesn't impose any specific requirements over a third-party code. Thus, you can use whatever library or module you want. You also don't have to put any complicated error recovery logic into your code. Sometimes having a simple codebase is more important than having non-failing yet complex one.

As for process.on('uncaughtException') I wouldn't use it. It's deprecated now, so it will probably be removed at some point.


Here's my breakdown:

  1. forever

    Pros:

    • lets you keep your codebase simpler and smaller
    • allows you to write your application logic first and think about error handling later

    Cons:

    • Single uncaught exception causes all other requests to fail

    Use when:

    • Your Node process and your requests are cheap
    • Clients can retry on error
  2. domains

    Pros:

    • Broken request doesn't break other clients
    • Node process stays up longer reducing your overall downtime
    • See also domains+cluster combo (Thanks, Isaac!)

    Cons:

    • Can be used with domain-aware 3rd-party libraries
    • Requires additional logic in your program

    Use when:

    • Your requests or your Node process are expensive (e.g. file uploads, streaming data)
    • Single-node uptime is important
  3. process.on('uncaughtException') don't use it.


Notes

  1. Isaac Z. Schlueter and Felix Geisendörfer talked about domains in 13th episode of NodeUp

  2. There's a recent article explaining the difference between forever and Unix's systemd. You may find it useful.

like image 174