0

Here's the scenario I anticipate: I have an app written in PHP which has a domain layer (complete with validators, etc). I want to use node.js for my web services for performance in high concurrency situations. If I create a command line interface for my php domain layer (see below), will this give me better performance than just using Apache? Is this even a good way to do this? I'm new to node.js and am trying to get my bearings. Node: The command line interface for the domain layer will return json encoded objects.

//Super simple example:
var http = require("http");
var exec = require('child_process').exec;

function onRequest(request, response) {
  exec("php domain-cli.php -model Users -method getById -id 32", function(error, stdout, stderr){
      response.writeHead(200, {"Content-Type": "application/json"});
      response.write(stdout);
      response.end();
  });

}

http.createServer(onRequest).listen(80);
3
  • 1
    So you use nodejs as a webserver. Why not nginx then? PS: spawning a new process will take a lot of time (in comparison to apache, which already has children run) Commented Jun 25, 2012 at 5:19
  • if I need to make a database call from node.js, would it be better to use a native db lib instead of asynchronously calling something like above? I've never used nginx; I'm just trying to learn about node.js and the best way to so things with it Commented Jun 25, 2012 at 5:22
  • Yes, a native db lib would be better. Commented Jun 25, 2012 at 5:45

2 Answers 2

5

will this give me better performance than just using Apache?

You would have to measure it to be sure, but I highly doubt it.

Node's performance benefits come because it is a select()-based server, so it eschews threading and blocking (with expensive context switches and CPU pipeline stalls) in favor of non-blocking IO (aka green threading). If you offload all the work to PHP, then you're basically just using Node as a front-end server -- at which point you should just use Apache, since mod_php will be doing almost exactly what you're doing. Only mod_php can do it better, because it can keep the PHP interpreter hot in memory, instead of having to spin up a new interpreter on every request like you're doing.

Is this even a good way to do this?

Essentially what you've done is reimplement CGI using Node. So I would say no -- if CGI is what you want, there are plenty of existing implementations out there!

Sign up to request clarification or add additional context in comments.

5 Comments

what does it mean to be a "select()-based server"? Thanks for the reply.
@orourkedd: select() is a system call on POSIX systems that's used to poll a set of file or socket handles to see which ones are currently available for I/O. It's basically the main alternative to using threads for the purpose of interleaved I/O activity. You might want to ask a separate question about it, as I'm sure that other people can explain it much more thoroughly than I can.
it's actually epoll/kqueue/IOCP (linux/bsd/windows) based
@AndreySidorov: True. I just used the phrase "select()-based server" to mean a server with non-blocking IO and green threading, as opposed to a thread-based server -- but you're right that that doesn't mean that the server will necessarily use the select() function. Do you have a better idea for a generic term for this kind of approach?
@DanielPryden "server implementing reactor pattern" :) en.wikipedia.org/wiki/Reactor_pattern
1

Your code will be spawning a new process for php for every single client request. Not good! Web servers such as Apache generally have child processes that stay open to handle multiple client requests, so no new processes have to be spawned. PHP is also run as a module in these cases, so a process for it doesn't have to be created to execute a php script, it's already there in memory waiting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.