Why has no one said concurrency? PHP is bad at handling concurrency, so a lot of the web applications written in it simply do not scale without significant modifications.
Depends what you mean by concurrency (I've found in a lot of discussions people are very loose with how they define concurrency).
If by concurrency you mean async then yes PHP is lacking native support, and Swoole is a hard sell for instance in my relatively conservative company when Python or Node.js handles those cases natively and organizational alignment points us to Python anyway. I've found PHP is probably the best tool when everything in the web can be boiled down to more or less stateless (session data, db, in mem db aside) request-response models. Which is most use cases (at least in my experience).
If by concurrency you mean handling parallel requests on multiple cores or just letting the OS handle context switching on a single core, then yes PHP-FPM does exactly this and is a battle tested tool. Multiple PHP interpreter processes handles concurrency albeit with the overhead of managing processes instead of threads. A vertically scaled PHP-FPM server will handle most use cases and the challenge of horizontal scaling beyond that will depend on the application.
If by concurrency you mean threading then well, I understand you can hack it but I have no experience here. When I find myself in the scenario where I have CPU-bound tasks, it's usually written in another language and the web request-response is more of a job queue submission.
If by concurrency you mean handling many long-lived connections like Websockets asynchronously then yes, PHP is just a pain. I understand it can be done but again, coming from a conservative sector where the preference is for standard library support, PHP is just not the tool I reach for in this case.
I would say that async is only lacking in functions and extensions that work with IO. Specifically, if they run in the context of Fibers, they should interact with them.
Heh yes in my case Swoole became a discussion point at the company as we were interested in it as a solution to an async behavior interacting with hardware. But being in a conservative national security-minded sector (power grid) we need to submit libraries to the pentest guys and their army of consultants (yes this includes the entirety of our node_modules... poor guys).
Swoole had already been flagged as a vulnerability (rightly or wrongly, wrongly in my view) due to the bundled .zip endpoint mishap which led to OpenSwoole. Given two alternatives we would normally go with the library with higher adoption, which is Swoole, but the docs are best used by google translating the Chinese docs. The whole thing just looked too sus for decision makers (again wrongly in my view but that's not my call). And anyway the problem was easily solved the grug brained way by sending off the hardware commands via IPC to an existing Python service and letting PHP communicate the request state to the front end using some simple database writes. These are remote power cycling commands which could take on the order of 20 seconds to 2 minutes to resolve depending on hardware and network conditions. While async would have been nice to handle request > send command to hardware > response to client (accepted command) > await hardware response > response to client (command resolution) all in one round-trip with less code and less coupling, upon further reflection it's just way more work trying to play politics and get pulled into multiple 90 minute meetings on the subject!
We still need the Apache-PHP layer as it the battle tested public access HTTP attack vector most familiar to our very experienced pentest team and consultants. But there is a growing sentiment among the developer teams to shift to Python where standard library support covers more ground. The shift has already started for internal tooling (aka webtools/dashboards etc that exist only in intranet without the public attack vector) but I don't think it'll every happen completely in my lifetime.
That's why I love go actually, you can just use go in front of a function and it will run in parallel. And you have channels to exchange data between go routines.
As much as I love PHP, it's not meant to be used as daemon, and jot great for concurrency.
Spanning multiple php process is not as efficient as just launching a new routine, if you need to access the DB, they will each initiate a new connection, and writing in a file to share data is super slow, and error prone.
Also Go would be way faster than PHP.
Don't get me wrong, I love PHP with symfony and now FrankenPHP seems really good, but for small services I wouldn't use something else than go. And deployment is so simple, one binary and that's it.
Let’s say your web app needs to call 6 services and pull the response from each into an API response for the user. The services are not required to call each other. Without concurrency you’d have to do one at a time, waiting for the response each time.
Spawning off another process for each HTTP request, having it report to a centralized data store then notifying the end user the request is complete is total overkill for something like this vs a simple async await and Promise.all in something like typescript
Have you seen the Laravel solution for this scenario?
It's a bit of a hack, but works great in practice. I'm not suggesting this is a replacement for a real language feature though, just thought I'd mention it.
You can but honestly is easier said than done. Since php was not meant to be running like this, several libraries may have memory leaks and you need to be careful. Also the docs are not that great, we tried using it at work with an api built with symfony, we faced a some issues and ended up using nginx unit as runtime since it runs the processes in isolation and was performing better than fpm
View all comments
19
u/rkozik89 Dec 29 '24
Why has no one said concurrency? PHP is bad at handling concurrency, so a lot of the web applications written in it simply do not scale without significant modifications.