This is going to sound a bit nuts, but I don’t lose anything asking this question!
At present, workers are written in JS and run in a V8 isolate container, etc.
Does Cloudflare plan to enable workers to run/execute a PHP runtime at the edge?
There’s a mega-ton of code out there executing PHP, and re-writing it is not possible.
Maybe there’s a way to transpile it or similar - and if so, ideas would be welcome!
I realize that caching would be a far easier solution - but running this PHP has to be done on-the-fly.
There’s two layers that I’m trying to move to the edge - the execution of PHP, and the database itself. The database is a totally different problem (a distributed/ACID RDBMS is not easy) - so I thought I’d start by thinking about the EC2/compute piece of it.
I might be able to answer my own question as a workaround for now.
If there’s EC2 instances that are globally distributed, the easy solution would be to route requests from certain origin countries to their nearest known EC2 service nodes e.g. a US request would serve from a US-based AWS region.
This leaves the ACID RDBMS to mull over e.g. cloud spanner, etc.
I imagine supporting a new language could be problematic since the current security model of workers is built around V8’s isolation, and supporting PHP would probably require Docker which would introduce cold start issues. See
But, I believe it should be possible using webassembly. I’m no expert, so just going to reference these blog posts:
@ExE-Boss@Judge thanks for the pointers to webassembly! Do you know any such efforts so far, specifically for execution of PHP in CF workers? I’m finding it difficult to start somewhere.
Likely some real low-level stuff still needed to get started I’m afraid, Web assembly in itself is very new (2017? based on the webassembly GitHub) so there is limited documentation as well as little pre-existing code.
We don’t plan to provide any runtimes other than JavaScript+WebAssembly, because essentially all other language runtimes require process-level isolation (e.g. containers or VMs), which is too inefficient to allow us to distribute your code to all our 165+ locations at once.
In theory, you could indeed run PHP inside WebAssembly. However, there is a lot that would need to be done there. The PHP runtime would need to be ported to WebAssembly, which is no small task (it’s like porting to a new operating system). Additionally, PHP is a dynamic, garbage-collected language. WebAssembly as it exists today works well for classic C, C++, and Rust applications, but poses some big challenges to languages that rely on JIT compilation or garbage collection. The WebAssembly community is working to address these issues, but it will take time. I expect this is a few years away from being a reality, unfortunately.
Ironically, it may actually be more feasible to transpile PHP to JavaScript, and indeed there appear to be some tools on the internet that do this. However, I don’t know how practical and robust they are; you’ll have to experiment.