8 ms·
Running PHP fast at the edge with WebAssembly
- vander_elst 2y agoTrying to understand better the solution. Why isn't it possible to restrict the application via process isolation (nsjail, cgroups, docker...) and wasm is needed instead?
- Twirrim 2y agoWhereby fast is "About half the speed of native PHP"
- syrusakbary 2y agoAuthor here. Faster than it has ever been in the Edge via WebAssembly :) But you are completely right pointing out that there's still some room to improve, specially when compared to running PHP natively. Right now there's some price to pay for the isolation and sandboxing, but we are working hard to reduce the gap to zero. Stay tuned for more updates on this front!
- stracer 2y agoIs this tech meant for developers' needs only, or can regular, already existing PHP websites (e-shops...) somehow take advantage of it as well?
- the_duke 2y agoIt's meant for direct usage, not just for developers. You can deploy apps on the Wasmer Edge cloud, and also host things yourself if you want to, though in the later case the setup will be non-trivial.
- maxloh 2y agoGiven that we run PHP on the edge, what is the point of running the PHP interpreter on top of a WebAssembly interpreter (Wasmer) instead of just running the PHP interpreter directly? The latter will always be faster.
- Cyberdog 2y agoFrom what I can tell it's because some of these "edge" service providers will expect you to give them a WASM binary instead of a PHP script. The other caveats about "edge" throughout this discussion aside, if I needed to do this, I'd try to write something in Zig or (gag) JS or something else that compiles to WASM directly rather than writing a script for an interpreter that runs under WASM.
- mike_d 2y agoHey you should really check out fly.io if you want to run stuff on the edge. They have it pretty much figured out. FrankenPHP is also really good and probably a much smarter play than trying to get your code running under WebAssembly.
- vundercind 2y agoNb PHP is already really fast. Some major things built on it are slow, mostly due to things like poor data access patterns or architecture, but its culture since the beginning has basically been “get out of PHP and into C as fast as possible” and it shows (this is basically the trick to any scripting language “being fast”, PHP just embraced it hard from the very beginning). If you’re on PHP and need more speed in the language itself, basically every other scripting language (yes, including Node) is off the table immediately. Lateral move at best. (All that to say, yeah, I entirely expected that the headline would lead to an article about making PHP slower)
- mschuster91 2y ago> PHP just embraced it hard from the very beginning Which is also the reason why a lot of the PHP standard library functions are so inconsistent. They're straight wrappers around the C libraries. Upside is, unless you have to do shit with pointer magic or play around the edges of signed/unsigned numbers, it's fairly easy to port C example code for any of the myriad things PHP has bindings to to PHP.
- pjmlp 2y agoI don't know which hype is worse, AI or WebAssembly on the server.
- secondcoming 2y agoIs JS on the server still a thing?
- pjmlp 2y agoYes, and to use alternatives there is no need for WebAssembly.
- deleted 2y ago[deleted]
- csomar 2y agoThis is trying to solve a solved problem with lots of difficult technology that doesn't apply here. Most of the PHP websites are WordPress. The solution to have a speedy WordPress site is to compile it to static HTML. Calls to the server should happen with JavaScript. The server will always remain relevant as WordPress uses a Database and thus the "Edge" makes no sense here.
- jw1224 2y agoGetting WordPress running in WASM was a huge milestone. It was one of the first big PHP/WASM achievements, but was never the end goal, just a proof-of-concept. The target market for this tech is not WordPress bloggers.
- ahofmann 2y agoI'm still trying to understand what this does and what's the use case. Is the "edge" a server? The browser? Why should I compile WordPress or Laravel to wasm?
- tredre3 2y agoIn this context the edge is a fancy word to say "serverless". It just means that your PHP interpreter will be started on-demand on a node closer to your customer's request. So if your website receives no requests, it costs you nothing. And requests have less latency for the user. That's the theory anyway, in my experience reality is a lot more nuanced because the serverless node still has to reach a database and so on.
- pjmlp 2y agoPHP nowadays already has a JIT compiler and its own bytecode, no need for WebAssembly.
- jw1224 2y agoThis seems like a misunderstanding, WebAssembly has nothing to do with PHP’s internal performance mechanisms. WASM is a compilation target, this means you can take code which needs to be compiled (like PHP’s core binary), and compile it to be run in a browser. PHP in WASM means developers can run actual, real, native PHP code in the user’s browser, without the user needing to have PHP installed locally, or nginx, etc…
- pjmlp 2y agoBecause I always wanted to run PHP in the browser.
- pavlov 2y agoYou could do that, but the post here is in fact about running PHP on a server on top of a Wasm runtime. “At the edge” basically means “close to the user”, with the details left as an exercise to whoever is selling you their “edge.” In this case, it’s a Wasm runtime company.
- TekMol 2y agoLet me try to understand this "Edge" thing: - The user sends an HTTP request to somesite.com - Their DNS query for somesite.com gets resolved to some datacenter near them - The HTTP request arrives at the datacenter where the PHP in WebAssembly is executed at half the speed of native PHP - The PHP in Webassembly sends database queries to a central DB server over the internet - The PHP in Webassembly templates the data and sends it back to the user How is that faster than resolving somesite.com to the central server, sending the HTTP query to the central server where PHP runs at full speed and talks to the DB on the same machine (or over the LAN)? Even if PHP ran at full speed at the "Edge", won't the two requests over the internet user --1--> edge PHP server --2--> central Db server take longer than the one http request when the user connects to the central server directly? user --1--> central PHP+DB server In reality, the PHP script on the "Edge" server probably makes not one but multiple queries to the DB server. Won't that make it super slow compared to having it all happening on one machine or one LAN?
- afavour 2y agoThe post mentions a cache. I think the key here would be not going to the central DB and instead going to a distributed cache. Not an unreasonable concept when I assume the vast majority will be read operations.
- Y-bar 2y agoThat's OPcache they mentioned, a specific language-level cache native to PHP. https://www.php.net/manual/en/intro.opcache.php https://www.php.net/manual/en/intro.opcache.php
- moomoo11 2y agoCan someone ELI5 what is does "edge" computing means? The way I understand it is that is moving some operations closer to the client to avoid bandwidth costs and improve performance. I thought of the Tesla car computer as edge computing, as it does a lot of processing within the car that would otherwise add latency and reliance on a internet connection. But for web browsers? Going to some websites? What sort of apps need this functionality? Seems like over-engineering, so I'm looking for someone to explain me.
- Joel_Mckay 2y agoHi, traditionally for our purposes it solved a few problems. 1. latency 2. intermittent access 3. distributed "meaningful" data preparation/filters One may consider routers with squid proxies, VoIP trunks, and p2p cache are essentially similar "edge" technologies. There are additional use-cases, but we don't want to educate the lamers stealing resumes off jobs sites... having no clue what they are talking about. Have a nice day, =)
- moomoo11 2y agoOh ok I see lol
- flemhans 2y agoCan i do it on my own hardware?
- maxloh 2y agoGiven that we run PHP on the edge, what is the point of running the PHP interpreter on top of a WebAssembly interpreter (Wasmer) instead of just running the PHP interpreter directly? The latter will always be faster.
- lxgr 2y agoOne thing WASM runtimes usually do really well is sandboxing. Various interpreters might or might not have a good capability/permissioning model (Java's is capable but complex and not supported by many applications, for example); even if they do, there might be exploitable bugs in the interpreter itself.
- tambourine_man 2y agoBut we already have containers and VMs are cheap. I find WASM interesting from a technical perspective, but not from a practical one.
- lxgr 2y ago“Containers are not a sandboxing mechanism”, I hear reasonably often (although that seems surmountable at least in theory?). VMs are cheap, but not “let’s run thousands of them on ‘the edge’ in case we get a request for any of them!” cheap.
- fhuici 2y agoOn kraft.cloud we can (done internal stress tests for this) run thousands of specialized VMs (aka unikernels) scaled to zero, meaning that when a request for one of them arrives we can wake it up and respond within the timescales of an RTT. You can take it out for a spin, just use the -0 flag when deploying to do scale to 0 (https://docs.kraft.cloud/guides/features/scaletozero/ https://docs.kraft.cloud/guides/features/scaletozero/).
- lxgr 2y ago
- choutianxius 2y agoI think this will face the same problems as Next.js edge runtime: your database cannot be moved to the edge
- eddd-ddde 2y agoThere are already edge databases. Also, not everything requires a database.
- joshstrange 2y agoHonest question, what kind of requests are you thinking of? In my projects I’m always fetching or changing data in a database on each request and if I’m not then I’m probably moving that logic to the frontend.
- eddd-ddde 2y agoA basic example is some compute service, say image transformation. You just run computations where all the input is in the request, and all the output goes to the response.
- joshstrange 2y agoI feel like you still need to the DB for that, for auth at a minimum (which might not need to hit your main DB I guess) if not for logging (spend credit/record usage/just analytics). I guess all of that could be skipped with something like a JWT and log ETL process. I'm intrigued by edge computing but the DB always seems like the bottleneck so thank you for the example.
- mike_d 2y agoPlease stop calling it "at the edge." You have seven locations all in highly developed Equinix datacenters. Edge means getting embedded into ISP networks, cell towers, smaller metros, etc.
- deleted 2y ago[deleted]
- fhuici 2y agoActually it means both, in an unfortunate case of term overload. Though I can understand the embedded/IoT world being frustrated by this, as the term existed first within that context.
- mike_d 2y agoBoth what? There is no definition other than running a workload "at the edge" near the requestor. This is like the owner of a restaurant with two locations in the same city calling themselves a nationwide chain. It is just a flat out fabrication.
- deleted 2y ago[deleted]
- irq-1 2y agohttps://news.ycombinator.com/item?id=38829557#38834787 https://news.ycombinator.com/item?id=38829557#38834787 >> Ask HN: What are your predictions for 2024? > Server-side WASM takes off with the re-implementation of PHP, Ruby/Rails, Python, and others, and a WASM based virtual server (shell, filesystem, web server, etc..) Cost more but has better security for both the host and user. Guess I was wrong about it costing more? > … we can run PHP safely without the overhead of OS or hardware virtualization. But it only runs at half the speed of PHP, so you need more resources.
- tambourine_man 2y agoThis looks interesting, but as a feedback, I found the copy a bit repetitive and lacking substance.