Porting a long-lived Java server to share-nothing PHP
Porting a server from a long-lived JVM to process-per-request PHP: what to mirror, why caching a parsed structure only halves the cost, and why a resident daemon was the wrong answer.

On this page
A gadget server written for a long-lived JVM, ported directly to a runtime that discards memory after every request, spends nearly half of every page view re-reading the same feature files. The server was Apache Shindig, the open-source reference implementation of OpenSocial, a standard that let developers write an application once as an XML gadget and run it on any adopting social network. That portability shifted the hard engineering onto the networks: each host had to render gadget XML into iframe pages, resolve and serve the JavaScript feature libraries gadgets expected, proxy signed requests, and expose social data through a common API.
Java Shindig kept its parsed feature graph, gadget specs, and JavaScript in memory for the life of the process. Most social networks across Europe and Asia ran PHP, where every request starts empty. A Java tier beside the PHP site, or a thin shim, does not help when deployability on an ordinary PHP host is a product requirement. Porting across runtime models instead means keeping the original structure where it carries the specification, redesigning wherever the new process model forces each request to repay constant work, and profiling first to push that work into shared caches and then out of PHP into HTTP caches.
I wrote the PHP port of Shindig in about a week before joining Google and carried it almost single-handedly for two years. It grew to about 30,000 lines of PHP running on networks reaching more than 400 million users, with Friendster the largest at about 75 million registered.
Mirroring the reference implementation
OpenSocial was moving quickly under live integrations, so an independent architecture would have doubled the work of tracking the specification. I mirrored Java Shindig’s class structure and dispatch closely; Java servlet conventions look awkward in PHP, a fair trade for porting specification changes change for change.
Mirroring stopped where PHP’s process model forced a different design: feature loading, caching, and remote fetching. Java Shindig handed outbound HTTP requests to worker queues, whereas PHP had none, so the server fetched a gadget’s declared preloads in parallel through curl_multi rather than stacking sequential network round trips onto every render.
Profiling and the deserialization ceiling
Profiling the first working port put feature loading at about 45 percent of request time: reading XML feature definitions from disk, resolving their dependency graph, and assembling the JavaScript each gadget required. Caching individual files would have left every request redoing that work, so I processed the entire feature registry once and cached the resulting structure, nearly doubling speed.
Because adopting networks ranged from single machines to clusters, the cache layer was pluggable by deployment shape: local files for one server, memcache for a cluster, and APC, PHP’s opcode and data cache, for features. While a value was being written, a per-key lock made other processes wait instead of writing the same entry concurrently.
Caching the processed registry only halved the cost. The kernel’s page cache already made file reads cheap; the bottleneck was PHP parsing a large serialized structure many times a second, with per-process copies adding up across the worker pool.
Moving JavaScript bundles into HTTP caches
To keep that deserialization off the hot path, I stopped sending the bulk of feature JavaScript through PHP on each render. Rendered pages referenced external script bundles with a content checksum in the URL, served with far-future expiry headers so browsers and proxies cached them directly while PHP emitted only gadget markup and request-specific state. In my synthetic localhost benchmark, switching feature scripts from inline to external raised throughput from 420 to 630 pages a second. Inlining stayed configurable, since some containers preferred the server cost to an extra onload round trip.
The server behaved like a real HTTP server across its other endpoints too, sending Cache-Control and ETag headers, and answering conditional requests with 304 Not Modified, instead of leaving PHP uncacheable while browser and proxy caches sat idle.
Deployability over a resident daemon
A faster architecture existed even inside PHP: a long-lived, socket-multiplexing daemon holding the parsed feature graph and scripts in memory across requests would avoid per-request deserialization and outperform the process-per-request model on raw throughput. I rejected it because simple deployment on standard PHP hosts was the point of the port. Teams running PHP fleets already knew how to deploy, monitor, and scale stateless workers; asking them to supervise a custom resident daemon would have recreated the operational burden of a separate Java tier.
What a second implementation catches in a specification
Keeping the PHP server structurally aligned with Java Shindig also made it a practical test of the specification, since two independent implementations in different runtimes diverge wherever a standard is vague. Carrying that second implementation put me directly in the OpenSocial 0.8.1 and 0.9 specification process, where I drove proposals and shepherded the vote that aligned the REST API with Portable Contacts, an open schema for people and contact data. I implemented OAuth request signing and the body-hash extension early, and removed non-compliant behavior before live gadgets or networks could depend on it.
Chris Chabot, March 2010