News for hackers. Stuff that matters.
Re:Node.js and the new web front-end by jsnk on Monday October 07, 2013 @12:55PM (#6509520)
Here's a stupid question. If Nodejs is sitting between the client and PHP backend, isn't latency higher than just using PHP backend?
Re:Node.js and the new web front-end by k3n on Monday October 07, 2013 @01:10PM (#6509635)
Though there's no absolute answer here, and it could very well go either way, as he explains it would most likely be irrelevant due to the fact that PHP can only process serially.

For instance, if you need to run 3 SQL queries in PHP, then the total execution time will be t1 + t2 + t3. On the other hand, if you make those queries in NodeJS instead, it will be MAX(t1, t2, t3).

So, it depends on the complexity of your app and your ability to decouple and separate concerns in the NodeJS server.

Re:Node.js and the new web front-end by anonymoushn on Monday October 07, 2013 @02:22PM (#6510152)
Is it actually impossible to query the DB asynchronously in PHP?
Re:Node.js and the new web front-end by k3n on Monday October 07, 2013 @04:06PM (#6510840)
Not impossible, but not easy either considering PHP is single-threaded and synchronous.
Re:Node.js and the new web front-end by anonymoushn on Monday October 07, 2013 @05:00PM (#6511218)
I've never heard of languages being synchronous or asynchronous before. It looks like PHP ships with asynchronous DB APIs.
Re:Node.js and the new web front-end by k3n on Monday October 07, 2013 @09:34PM (#6512462)
Ok, poor example; just consider that it's single-threaded. Pretty much the only threading support is by way of a young PECL extension called pthreads[1], which is still experimental and dangerous. Otherwise, you can use the PCTL extension[2], if you don't mind compiling PHP yourself (and if you're not on Windows, where it's not supported).

1. http://www.php.net/manual/en/intro.pthreads.php

2. http://www.php.net/manual/en/pcntl.installation.php

Re:Node.js and the new web front-end by sisk on Monday October 07, 2013 @01:14PM (#6509662)
I think what EGreg is talking about (benefits of using node over, e.g., PHP) and what this article is talking about (benefits of throwing node between the client and the back-end) are kind of two different things. But I think the similarities address what you're asking.

If node is being used simply as a proxy then—no question—throwing it in to the mix will simply introduce latency (shortest distance blah blah blah). However the suggesting made by both the article and EGreg seems to be, "use the right tool for the job." In the case of the article, Nicholas seems to be suggesting that aspects of your data model might be better suited in the hands of your front-end engineers as they're the ones presenting that data. EGreg is suggesting leveraging the asynchronous nature of node (and evented programming in general) to handle certain tasks more efficiently.

But to answer your question, not necessarily. Even if you introduce additional latency, you might still benefit from putting more of the data model into the hands of your front-end guys (particularly if you're at a massive organization where coordination is the biggest hurdle). Plus, there's nothing stating you can't push the mutations performed at the node layer down farther into your stack and, potentially, remove it entirely. And, there's also the case that node might do the work you've delegated to it more quickly than if it were performed in PHP, negating the loss due to additional latency (but if that additional latency is the concern, I'd recommend relying benchmarking to help you make that decision).

Re:Node.js and the new web front-end by EGreg on Monday October 07, 2013 @01:09PM (#6509628)
Yes and no.

If Node.js can do things like

* cache responses

* not send duplicate responses but instead maintain a waiting queue

* throttle requests based on # of preforked threads etc.

then it would be faster most of the time.

And equally importantly you can take a page from Yahoo's book and pre-render stuff on the presumably faster server, for mobile devices and robots.

Re:Node.js and the new web front-end by rubiquity on Monday October 07, 2013 @01:18PM (#6509693)
Why can't you do these things in PHP (or whatever other language you're already using)?
Re:Node.js and the new web front-end by EGreg on Monday October 07, 2013 @01:28PM (#6509758)
You can, but Node.js is often a better choice than PHP.

Caching: you can have ONE node.js process listening to all requests, and it can keep the cached info in memory, to be looked up much more quickly. In PHP, you'd have to load the entire script environment, connect to an external source like Memcached or APC, etc.

Waitlist: Similar to caching, except Node.js can hold off on responding to the request until the response came back. It's MUCH harder to make something like PHP iterate through the waitlist because a bunch of scripts might get called at once, and the way it works, all of them will have to sleep. With Node.js, only one process has to sleep -- or handles something else while you're waiting.

etc.

Re:Node.js and the new web front-end by Touche on Monday October 07, 2013 @02:06PM (#6510023)
Because that shouldn't be the responsibility of the business layer. Perhaps they are serving many apps other than just the web client, some of which don't want cached data.
Re:Node.js and the new web front-end by ChikkaChiChi on Monday October 07, 2013 @01:14PM (#6509660)
Node can issue multiple non-blocking commands which can let PHP create children processes to handle each request.

It can work, though you lose a bit of transparency.