views:

75

answers:

3

I'm working on a web application that frequently requires a calculation intense query to be run, the results of which are stored in a separate table. Using MySQL, this query takes about 500ms (as optimized as possible, believe me). To eliminate this bottleneck, I've created a Java program that loads the relevant DB data into memory and performs the query itself; it takes about 8ms (something I'm a little bit proud of). I'd like to use this Java program to get the results, and if it fails or is unavailable, failover to having PHP run a MySQL query.

Since loading the data into the Java application takes some time, it's going to load it once and remain running as a background process. Now, the question is how do I communicate with this Java application via PHP?

Keep in mind:

  • Multiple instances of PHP may need to communicate with this Java process simultaneously.
  • If the Java instance cannot be found (eg: it crashes for some reason) PHP should progress by using the older and slower MySQL method.
  • An intermediary process, such as Memcache, is acceptable.
  • Ideally, the solution would withstand race conditions.
  • I would preferably not like to use MySQL as the intermediary.

I was going to use Memcache, where PHP would write to a known key and poll until that key changed to "completed", meanwhile Java would poll that key and once it found something perform the job and set it to "completed". However, this wouldn't work for two reasons. First, both PHP and Java read/write to Memcache using serialized objects, and there's no way to change that, and I don't want Java to unserialize PHP objects and vice/versa -- it's too messy. Second, this is not ACID compliant -- if a queue built up there would be race conditions.

For now, I'm stuck with polling MySQL "selects" to see if a job is off the queue or not, which is far from an optimal solution because the poll time will need to be slower so MySQL doesn't get pinged too frequently. I need a better solution!

Thanks.

Edit: Duh. It looks like I will be using some sort of SocketServer in Java, which I'm unfamiliar with. An example might help :)

+1  A: 

Hello Sambo.

My suggestion is to use WebServices... Write and run webservice in Java, and then request it in php by using f.e. NuSOAP. This solution have one more advantage - your webservice can be used easily in other applications like f.e. .NET ones...

Another option which might be easier if you have small number of methods is to build Servlet in Java which will take the parameters as GET request.

Both those solutions are strictly web-based, and both of them are working on separate threads so they guarantee you good performance.

ŁukaszW.pl
Thanks for the input! I opted to go with plain 'ol SocketServer which cuts down on the overhead of using WebServices. It's a good point that using WebServices would allow other applications to connect to it, but that's unnecessary at the moment.
Sambo
Nice, writing your own servlet will be just fine. If you satisfied with my answer anyway, please tag it as answered ;)
ŁukaszW.pl
+1  A: 

PHP/Java Bridge

Ankit Jain
This looks like a headache.
Sambo
+1  A: 

I'm using socket server on the Java end, and PHP sockets on the PHP end. Works great.

There's no need to overcomplicate things with PHP/Java bridge, and no need for overhead of creating a web server.

Sockets work great, and I'm actually a bit ashamed I even asked the question to begin work.

Sambo