-2

I'm going to be using Nodejs to process some CPU intense loop operations with sending emails to registered users as PHP was using too much during the time it runs and freezes the site.

One thing is that Nodejs will be on different server and do a request using external connection in MySQL.

I've heard that external db connection is bad for performance.

Is this true? And are there any pros and cons of doing this?

Passionate Engineer
  • 10,034
  • 26
  • 96
  • 168

1 Answers1

0

Keep in mind, when running a CPU intensive operation in Node the whole application blocks as it runs in a single thread. If you're going to run a CPU intensive operation in Node, make sure you spawn it off into a child process who's only job is to run the calculation and then return to the primary application. This will ensure your Node app is able to continue responding to income requests as the data is being processed.

Now, onto your question. Having the database on a different server is extremely common and typically is a good practice to have. Where you can run into performance problems is if your database is in a different data center entirely. The further (physically) your database server is from your application server, the more latency there will be per request.

If these requests are seriously CPU intensive, you should consider looking into a queueing mechanism for a couple reasons. One, it ensures that even in the event of an application crash, you don't lose a request that is being processed. Two, you can monitor the queue, and scale the number of workers processing the queue in the event that the operations are piling to the point that a single application can't finish processing one before another comes in.

Timothy Strimple
  • 22,920
  • 6
  • 69
  • 76
  • Thanks for this. It's actually a combination of complex MySQL requests (they are well indexed) and PHP loop operations. I was under the impression that Node would be much faster especially during intense loop operations than PHP (http://blog.loadimpact.com/2013/02/01/node-js-vs-php-using-load-impact-to-visualize-node-js-efficency/) – Passionate Engineer Aug 13 '13 at 07:15
  • Javascript executed via V8 is definitely faster than PHP. However you'll likely be able to get the same sort of benefit if you just run your PHP calls on a separate server and not the primary web server. You can still have a queuing system with PHP worker instead of node which run your existing loops and queries, just on a different server. This may be easier to implement since your codebase is already PHP. – Timothy Strimple Aug 13 '13 at 07:19
  • Here is a pretty good rundown of the steps I would take to solve this sort of problem. As much as I love node, it's not a magic bullet and your problems can easily be solved in PHP alone. – Timothy Strimple Aug 13 '13 at 07:21
  • Great. Are there any queuing libraries that you would recommend? – Passionate Engineer Aug 13 '13 at 07:33
  • I mostly default to ServiceBus, but that's not appropriate for your situation. I've heard good things about RabbitMQ and ZeroMQ see this: http://stackoverflow.com/questions/731233/activemq-or-rabbitmq-or-zeromq-or -- However for your case those might be overkill as well. I would take a look at what other PHP developers are using and see what you can find from there. http://stackoverflow.com/questions/tagged/queue+php?sort=votes&pagesize=50 – Timothy Strimple Aug 13 '13 at 07:44
  • Thanks a lot! I think I'll go with ActiveMQ! – Passionate Engineer Aug 13 '13 at 07:57