Can someone confirm good performance using PHP + Java webservice (for heavy duty tasks) running on NGINX (or Apache) because PHP is slow in completing some heavy tasks and I want to make a web service in java to communicate with PHP via SOAP or REAT or JSON.

The fact I'm asking this question is that I already know PHP at a very good level (and Java and I don't want to start learning ASP or Ruby) and HTML5, CSS3 and I want to develop a big portal that will have increased number of requests (1000 / 2000 request/sec) and I don't want to code the whole application in Java (cause PHP has more sense), I only need the heavy work to be done by a webservice.

For my project PHP is slow in doing some Math computation and I/O file access every second. It consume a lot of memory and a lot of CPU. At the rate of 1000 req/s it almost crushes.

Is this a good practice? Is there another solution?

closed as not a real question by Arseni Mourzenko, gnat, Walter, ChrisF Feb 19 '12 at 0:19

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Voted to close as not a real question. You can build web services with PHP, Java or whatsoever. Can you do it in your specific scenario? We can't possibly know. Profile it, and if you find that the bottlenecks can be solved only by migrating from PHP to Java, do it. – Arseni Mourzenko Feb 17 '12 at 10:22
  • PHP is slow in completing some heavy tasks how do you know that? did you do profiling / benchmarking? – gnat Feb 17 '12 at 10:23
  • php is consuming a lot of memory and cpu and does some math computation very slow compared to java and also I/O file access and I ONLY ASKED if there are performance improvements (as big site's like linkedin or twitter are using Java for performance) not how to do it. I know how to do it. – pufos Feb 17 '12 at 10:26

If you need better performance out of your PHP code, these are the steps you should be taking, in order:

  1. Identify the bottlenecks. Add some simple profiling to see where your code spends most of its time. Focus on that part.
  2. Check your algorithmic complexity ("big-O"). If you can reduce an O(n^3) algorithm to O(n^2), do it.
  3. For everything you do, check if there is a PHP library function that can do the same thing. Many of those are implemented in C, and they run much faster than equivalent PHP code.
  4. Balance CPU vs. RAM usage. For most problems, you can trade one for the other: if you're using too much RAM, you can generate some of your data procedurally when needed (you trade CPU cycles for RAM), if you're CPU bound, you can often precalculate results and reuse them (using more RAM to save CPU cycles).
  5. Use caching. Lots of it. If you aren't using memcached already, do so, and use it to cache final results, intermediate results, or anything you are going to need a lot. You may even want to cache at several levels, e.g., cache database query results, intermediate values, final values, and raw responses.
  6. Write faster PHP. PHP and its interpreter have a few quirks, so it's not always obvious if and why a certain code variation runs faster, so you really need to profile in order to know.
  7. If you have to, implement the most performance-critical part of your code in C, and call the C code from PHP. There are several ways of achieving this - you can write a PHP extension, you can simply shell out to the C program, you can run the C program as a CGI binary, or you can design the C part as a daemon and communicate over named pipes or local sockets. If in doubt, the shellout is probably the easiest option to implement.
  8. Throw more hardware at it - that is, buy a faster server.
  9. Distribute the load over several servers. This usually requires drastic changes to your architecture, so you should investigate thoroughly if this is both necessary and feasible. Also note that there are several strategies to this.
  • Cool answer .. thanks man . I was going in depth with java to acomplish faster web apps (with php). I had doubts that PHP is going to be this fast to serve 1000 req/s . I did'n thought of implementing in C , is there something native (Call the C code from PHP) ? Anyway thank you very very much – pufos Feb 17 '12 at 12:21
  • As I said, the easiest solution to calling C code from PHP is through a shellout (look into this section of the PHP documentation: php.net/manual/en/ref.exec.php). Adding extensions to PHP itself is also possible; something like this: devarticles.com/c/a/Cplusplus/… should help you get started. The other two methods involve running the C part as a standalone process, and you can communicate between PHP and C as you would between any two processes. – tdammers Feb 17 '12 at 13:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.