News for hackers. Stuff that matters.
Re:Running PHP fast at the edge with WebAssembly by TekMol on Thursday May 23, 2024 @02:46PM (#40458349)
Let 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?
Re:Running PHP fast at the edge with WebAssembly by kevincox on Thursday May 23, 2024 @02:56PM (#40458468)
Due to connection setup roundtrips, TCP slow start mechanics and quality of network connections it is usually better to terminate the client's TCP connection close to them. But I agree that moving the frontend rendering near the client doesn't really make sense for almost all cases. So the best general purpose setup would probably look like user → CDN → central PHP+DB.

Of course there are always exceptions. Often stale data is ok and you can ship data snapshots to the edge so that they can be served without going back to the central DB. But in many cases some basic cache policies with a CDN can be nearly as effective.

Re:Running PHP fast at the edge with WebAssembly by TekMol on Thursday May 23, 2024 @03:00PM (#40458537)
What I usually do is that I use two hostnames. host1 for http requests which can be cached. That host is behind a CDN. host2 for HTTP requests which can't be cached. That host points directly to my server.

Are you saying it would result in a better user experience when I only use host1 which is behind the CDN and add no-cache headers to the request that can't be cached?

Re:Running PHP fast at the edge with WebAssembly by kevincox on Thursday May 23, 2024 @03:03PM (#40458578)
It's complicated but typically yes. The simplest reason is that TCP+TLS handshakes require multiple round trips for a fresh connection. The CDN can maintain a persistent connection to the backend that is shared across users. It is also likely that the CDN to backend connection goes over a better connection than the user to backend connection would.
Re:Running PHP fast at the edge with WebAssembly by e1g on Thursday May 23, 2024 @03:36PM (#40458945)
> The CDN can maintain a persistent connection to the backend that is shared across users

We considered using Cloudflare Workers as a reverse proxy, and I did extensive testing of this (very reasonable) assumption. Turns out that when calling back to the origin from the edge, CF Workers established a new connection almost every time, and so had to pay the penalty of the TCP and TLS handshake on every request. That killed any performance gains, and was a deal breaker for us. It’s rather difficult to predict or monitor network/routing behavior when running on the edge.

Re:Running PHP fast at the edge with WebAssembly by bluk on Thursday May 23, 2024 @05:26PM (#40460094)
Were you using a Cloudflare tunnel for your origin?
Re:Running PHP fast at the edge with WebAssembly by e1g on Thursday May 23, 2024 @08:11PM (#40461469)
Yes, tried tunnels too. There is significant variability among individual requests, but when benchmarking at scale I found no meaningful difference in p50 and p90 between “Worker -> CF Tunnel -> EC2 -> backend app” and “Worker -> AWS Global Accelerator -> EC2 -> backend app”
Re:Running PHP fast at the edge with WebAssembly by kentonv on Thursday May 23, 2024 @06:41PM (#40460755)
This didn't sound right to me so I did some investigation and I think I found a bug.

Keep in mind that Cloudflare is a complex stack of proxies. When a worker performs a fetch(), that request has to pass through a few machines on Cloudflare's network before it can actually go to origin. E.g. to implement caching we need to go to the appropriate cache machine, and then to try to reuse connections we need to go to the appropriate egress machine. Point is, the connection to origin isn't literally coming from the machine that called fetch().

So if you call fetch() twice in a row, to the same hostname, does it reuse a connection? If everything were on a single machine, you'd expect so, yes! But in this complex proxy stack, stuff has to happen correctly for those two requests to end up back on the same machine at the other end in order to use the same connection.

Well, it looks like heuristics involved here aren't currently handling Workers requests the way they should. They are designed more around regular CDN requests (Workers shares the same egress path that regular non-Workers CDN requests use). In the standard CDN use case where you get a request from a user, possibly rewrite it in a Worker, then forward it to origin, you should be seeing connection reuse.

But, it looks like if you have a Worker that performs multiple fetch() requests to origin (e.g. not forwarding the user's requests, but making some API requests or something)... we're not hashing things correctly so that those fetches land on the same egress machine. So... you won't get connection reuse, unless of course you have enough traffic to light up all the egress machines.

I'm face-palming a bit here, and wondering why there hasn't been more noise about this. We'll fix it. Talk about low-hanging fruit...

(I'm the tech lead for Cloudflare Workers.)

(On a side note, enabling Argo Smart Routing will greatly increase the rate of connection reuse in general, even for traffic distributed around the world, as it causes requests to be routed within Cloudflare's network to the location closest to your origin. Also, even if the origin connections aren't reused, the RTT from Cloudflare to origin becomes much shorter, so connection setup becomes much less expensive. However, this is a paid feature.)

Re:Running PHP fast at the edge with WebAssembly by e1g on Thursday May 23, 2024 @07:41PM (#40461224)
> So if you call fetch() twice in a row, to the same hostname, does it reuse a connection?

In my testing, the second fetch() call from a worker to the same origin ran over the same TCP connection 50% of the time and was much faster.

We want to use Workers as a reverse proxy - to pick up all HTTP requests globally and then route them to our backend. So our use-case is mostly one fetch() call (to the origin) per one incoming call. The issue is that incoming requests arrive to a ~random worker in the user's POP, and it looks like each Worker isolate has to re-establish its own TCP/TLS connection to our backend, which takes a long time (~90% of the time).

What I want is Hyperdrive for HTTPS connections. I tried connecting to backend via CF Tunnel, but that didn't make any difference. Our backend is accessible via AWS Global Accelerator, so Argo won't help much. The only thing that made a difference was pinning the Worker close to our backend - connections to the backend becamse fast(er) because the TLS roundtrip was faster, but that's not a great solution.

Re:Running PHP fast at the edge with WebAssembly by kentonv on Thursday May 23, 2024 @08:45PM (#40461677)
> The issue is that incoming requests arrive to a ~random worker in the user's POP, and it looks like each Worker isolate has to re-establish its own TCP/TLS connection to our backend, which takes a long time (~90% of the time).

Again, origin connections are not owned by isolates -- there are proxies involved before we get to the origin connection. Requests from unrelated isolates can share a connection, if the are routed to the same egress point. Problem is that they apparently aren't being routed to the same point in your case. That could be for a number of reasons.

It sounds like the bug I found may not be the issue in your case (in fact it sounds like you explicitly aren't experiencing the bug, which is surprising, maybe I am misreading the code and there actually is no bug!).

But there are other challenges the heuristics are trying to solve for, so it's not quite as simple as "all requests to the same origin hostname should go through the same egress node"... like, many of our customers get way too much traffic for just one egress node (even per-colo), so we have to be smarter than that.

I pinged someone on the relevant team and it sounds like this is something they are actively improving.

> The only thing that made a difference was pinning the Worker close to our backend - connections to the backend becamse fast(er) because the TLS roundtrip was faster, but that's not a great solution.

Argo Smart Routing should have the same effect... it causes Cloudflare to make connections from a colo close to your backend, which means the TLS roundtrip is faster.

Re:Running PHP fast at the edge with WebAssembly by e1g on Thursday May 23, 2024 @09:34PM (#40461995)
Thank you for looking into it in such detail based on an unrelated thread!

Cloudflare seems to consistently make all types of network improvements behind the scenes, so I’ll continue to monitor for this “connection reuse” feature. It might just show up announced.

Re:Running PHP fast at the edge with WebAssembly by TekMol on Thursday May 23, 2024 @03:06PM (#40458624)
Very interesting, thanks. That would make my setup even simpler.
Re:Running PHP fast at the edge with WebAssembly by 1oooqooq on Friday May 24, 2024 @02:29AM (#40463435)
the "it's complicated" part assumes client side requests i guess.

none of this comment applies to back end + db in the same server or colo.

Re:Running PHP fast at the edge with WebAssembly by mschuster91 on Thursday May 23, 2024 @04:05PM (#40459273)
> Are you saying it would result in a better user experience when I only use host1 which is behind the CDN and add no-cache headers to the request that can't be cached?

Yes, because that way you can leverage the CDN to defend against DDoS issues, and you can firewall the origin server itself so only the CDN is allowed to communicate with it, but no one else.

Re:Running PHP fast at the edge with WebAssembly by 1oooqooq on Friday May 24, 2024 @02:41AM (#40463506)
if your market is not usa or Asia, you actually get DDoSed by using cloudflare et all more often (>1) than not (=0)
Re:Running PHP fast at the edge with WebAssembly by slaymaker1907 on Thursday May 23, 2024 @04:28PM (#40459523)
You're assuming that network latencies follow the triangle inequality, i.e., that A->C is smaller than A->B + B->C. However, that breaks down because of money. It's possible that the user's ISP is a cheapskate without good peering agreements such that while A->B is fast, they haven't setup the necessary agreements to make A->C fast.

I've personally seen this with a game recently and my own ISP. I can ping their West Coast servers in 20-40ms while pinging their Chicago servers takes 200ms+.

This is actually one huge flaw with the current way IP is done. Because your ISP is closely tied with the last-mile of routing, you have very little control over optimizing those backend layers of routing.

This doesn't explain why you wouldn't just run a thin proxy at the edge and instead want to run a full PHP app. For that, my guess is this makes sense when you don't want to run a full server all the time or if you want it to be rapidly scalable.

Re:Running PHP fast at the edge with WebAssembly by paulddraper on Thursday May 23, 2024 @04:30PM (#40459551)
Okay....but that's you providing a backbone for the user. Don't you just use a CDN at that point? (One that allows non-cached requests as well.)
Re:Running PHP fast at the edge with WebAssembly by the_duke on Thursday May 23, 2024 @02:58PM (#40458501)
That's a valid concern, and with a classical centralized DB the Edge solution will not be faster in many cases, especially considering how many DB requests some ORMs like Drupal or Wordpress need for each page load...

Some other providers have tried to solve the problem with replicated databases that are used for local reads.

I can't go into details yet, but Wasmer will offer a solution for this problem quite soon as well, which hopefully will be much more seamless than existing strategies.

(note: I work at Wasmer)

Re:Running PHP fast at the edge with WebAssembly by lovasoa on Thursday May 23, 2024 @03:25PM (#40458854)
Re:Running PHP fast at the edge with WebAssembly by meekaaku on Thursday May 23, 2024 @03:04PM (#40458597)
Isnt this true of everything that runs on the edge?

Unless you host the database in the same network as your edge provider, appliation <-> database is where the bottleneck is.

Re:Running PHP fast at the edge with WebAssembly by afavour on Thursday May 23, 2024 @02:55PM (#40458464)
The 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.
Re:Running PHP fast at the edge with WebAssembly by Y-bar on Thursday May 23, 2024 @02:57PM (#40458492)
That's OPcache they mentioned, a specific language-level cache native to PHP.

https://www.php.net/manual/en/intro.opcache.php

Re:Running PHP fast at the edge with WebAssembly by afavour on Thursday May 23, 2024 @03:05PM (#40458612)
Ah. Point still stands though, distributed storage is what really unlocks this concept.

Wordpress is a pretty bad example though, surely you’d just CDN cache the pages!

Re:Running PHP fast at the edge with WebAssembly by iruoy on Thursday May 23, 2024 @03:44PM (#40459044)
opcache doessn't cache user data. it's for caching bytecode in memory as long as the php script/server is running

i'd like to see a comparison against a firecracker vm and a docker container

Re:Running PHP fast at the edge with WebAssembly by fhuici on Friday May 24, 2024 @08:30AM (#40465502)
Hi, on kraft.cloud we use FC, along with a custom controller and very specialized VMs (unikernels) to have extremely efficient deployments (eg, millisecond cold starts). For a PHP web server, for instance, we can cold start things in about 30ms (https://docs.kraft.cloud/guides/php/). It's also possible to run wasm workloads/blobs (e.g., https://docs.kraft.cloud/guides/wazero/).

The builds are based on Dockerfiles, but for deployment we transparently convert that to unikernels.

Re:Running PHP fast at the edge with WebAssembly by eknkc on Thursday May 23, 2024 @05:14PM (#40459994)
I guess if you have a use case with higly cacheable data on edge, it might work great.

For example, a form builder like typeform could cache form definitions on edge and render them close to users. Submissions would require db communication but the entire experience could be better.

Otherwise it is just not worth it.

BTW, I don’t even think PHP running slower in wasm would be that important. These things generally depend on IO performance rather than runtime if you are not doing a lot of calculations on the edge. WASM is also pretty fast these days so..

Re:Running PHP fast at the edge with WebAssembly by 1oooqooq on Friday May 24, 2024 @02:50AM (#40463571)
> WASM is also pretty fast these days so..

ironically everything is petty fast and everyone waste that speed adding things that allow for a cheaper labour. wasm included. it went from a "have some expertly optimized code in the browser" to "just compile this hide cpp thing and ship as a react component" to then "it allows our servers to run any crap without knowledgeable staff in each language"

we added so many Layers to cheapen labour that now all the labour costs goes into managing the Layers.

Re:Running PHP fast at the edge with WebAssembly by eknkc on Friday May 24, 2024 @11:13AM (#40467034)
Ok hear this, I was toying with zig last week and compiled a quickjs wrapper that can run React and compiled that to wasm. So I have a JS runtime running wasm which runs quickjs runtime via zig and that runs React. Just trying to learn zig though but not much different than running PHP in WASM haha. Layers and layers of shit pressed together.
Re:Running PHP fast at the edge with WebAssembly by 1oooqooq on Friday May 24, 2024 @11:56AM (#40467438)
will definitely use it as the wrapper that launches my electron app
Re:Running PHP fast at the edge with WebAssembly by zackify on Thursday May 23, 2024 @07:56PM (#40461340)
That’s why you gotta use liteFS on fly.io or cloudflare d1. And of course, don’t use php
Re:Running PHP fast at the edge with WebAssembly by worewood on Thursday May 23, 2024 @09:33PM (#40461982)
I'm gonna counter the other comments that try to justify this somehow, and say:

I agree with you, and it's not faster. With the risk of sounding old (get off my lawn): I assure you the megabytes of unneeded Javascript they're downloading to the client are ten times slower to execute than the milliseconds they may be saving on the wire.

Re:Running PHP fast at the edge with WebAssembly by 1oooqooq on Friday May 24, 2024 @02:44AM (#40463528)
yes you are correct. the top comments disagreeing are either full of wrong basic concepts or are very dense in detail while hand waving the important parts and from CDN-for-hire employers.
Re:Running PHP fast at the edge with WebAssembly by shanemhansen on Thursday May 23, 2024 @11:15PM (#40462489)
I'm working on something in a similar space. It's not as mysterious as people make it sound:

You do caching so you don't always have to go back to the DB is the main thing. The other interesting capability is light scripting at the edge. Think "this page is cached but I'm going to add in a custom username".

There's some more exotic stuff around sharded database locality. But I'll skip over that because distributed mutable state is hard and its not my specialty.

Connection warming and shorter tls negotiation round trip times are also a thing but probably less important compared to the database thing you are mentioning.

But mostly this architecture makes sense if you can do some caching or if you're doing more data/log collection stuff.

For prior work see ESI (edge side includes). Assembling stuff at the edge does have some benefits but it has been hard to create a good interface for that.

Re:Running PHP fast at the edge with WebAssembly by Delmololo on Friday May 24, 2024 @03:16AM (#40463728)
Big providers have stable and fast and encrypted connection channels between edge and backend.

Client talks to closest system, TLS gets terminated as soon as possible.

Certain things can be cached on edge which normally can't be cached just through basic http (like as soon as you are logged in).