this post was submitted on 04 Jan 2024
104 points (95.6% liked)
Programming
17398 readers
93 users here now
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Rules
- Follow the programming.dev instance rules
- Keep content related to programming in some way
- If you're posting long videos try to add in some form of tldr for those who don't want to watch videos
Wormhole
Follow the wormhole through a path of communities !webdev@programming.dev
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Finally someone who know how to do things properly.
At the end of the day...
...isn't that how every web framework works?
Anything JS / NodeJS doesn't work like that and that's precisely one of the issues with it. Node will also keep running a process in the background even if the website/app isn't ever accessed wherever PHP won't be running anything until a request comes.
Yikes, pretty bizarre considering stateless endpoints is the gold standard.
Re: persistent process, that doesn't seem like a big deal, to me. It's pretty normal since you often want to keep some common stuff going, like metrics. Unless you're doing something crazy it should really take next to no resources while idling.
That shouldn't require persistent processes, you can do metrics without persistent processes and Matomo is a good example of that. If something requires a persistent process is either poorly designed / executed or we're talking about edge case like a chat, socket or similar where having a connection open to clients will improve things considerably.
This "always running" thing is a cancer initially found in some Java backends (because java takes a lot of time to start anything) and later reintroduced in JS exactly because of the same reason.
Another thing with JS/Node is that it is all a single-threaded runtime environment, meaning that a program's code is executed line after line and there can't be two lines of a program running at the same time. To handle requests from multiple clients node simply queues them and processes them sequentially (search for nodejs run loop). In short you can't process / send a reply to two users at the same time, one will have to wait before getting a reply. Even worse, if a request from a user manages to crash the daemon then everyone will get an instant / queued requests discarded. If you've some kind of daemon management running it may restart Node, if not your application will just die.
There are solutions for this like PM2 that essentially lunch x node.js processes and can upscale / downscale automatically and do request load balancing to those instances but that essentially an afterthought instead of a real fix for the real issue.
In PHP this was never an issue, because since every requests will spin a new process they'll all get processed in parallel, some may crash, some may take a long time and others wont' get affected. No extra 3rd party daemons required to manage things (except for the webserver or php-fpm that comes out of the box).
The JS/Node model is fundamentally flawed however it is mostly pushed around by people that don't know how things works funded / sponsored by people that have interest in resource wasting and selling VPSs for everyone and everything (cloud providers). It is all about mangling and reconfiguring the tools and technologies and education developers have in order to push them into flawed technologies so they can then sell more resources, load balancers, build processes and other overly complex stuff that aren't required for the majority of people.
Look, I'm not saying there aren't good and valid uses cases for node and for the single thread model because there are. There are cases where it performs better than PHP and its (usually isolated) processes but unfortunately people aren't using it for those use cases, they're using it to build simple websites and APIs there would've been much better, more reliable and cheaper to run if developed with PHP's model.
Now imagine someone makes 500 websites with nodejs for small businesses, that's gonna be 500 always running processes, very prone to memory leaks and crashes running in order to server those two users a day those businesses get. That's at least 5GB of RAM + CPU load to keep things running. If you do the same with PHP, what's gonna happen is that you'll have nginx running idle on 15MB of RAM and a bunch of PHP processes to process the requests that die after the job is done. You'll probably be able to run those websites in a 1GB of RAM VPS instead of something like 8GB that node would require.
You may be surprised by this example but it is more common than you might think and people don't even notice it. Sometimes it isn't a single person managing 500 websites, its about 500 developers making 500 websites with node and and deploying them to "droplets" in DigitalOcean. Since we're in the subject what about power consumption and hardware? Where are all the environmentalists and whatnot? PHP's model is more reliable, cheaper and also eco-friendly.
for content sites, stateless is fine. for web apps you need states of all different kinds. even the smallest detail is a state in an application.
endpoints themselves are stateless, but the web application is stateful. you only have to build the world once, and its much friendlier for end users.
I wasn't talking about frontend state, just the server. Frontend state is kind of irrelevant, tbh.
I with there was a language with this model, but without the language itself being completely garbage.
Isn't that the same as modern languages? For example in ASPCore / C#, you can just register all your services with a lifetime scoped to the request, and then there's no shared state.
If you want there to be a shared state, you'd just have to register your services with a higher lifetime scope, like with a singleton scope
You can still use CGI with Apache. Apache will execute your program on each request and return its output from stdout as webserver response. If you have a form, it'll get POSTed to stdin when Apache execute your program. You can write your program with whatever language you want as long as you can read stdin and write to stdout. It's just tedious af so no one really use it these days. PHP was basically born because people got tired writing CGI program with pearl or C and want something more convenient. But with modern programming languages, perhaps CGI is not too bad, except the one process per request which will absolutely kill your server the moment you have visitors spike.
You can't use the CGI model with node and JS/Node because... unlike PHP, the thing isn't designed for a quick start and shutdown.
Why not? Is it because a typical nodejs app include hundreds of npm dependencies? As long as it can launch and finish within 60s (default timeout for apache), you should be able to use it.
Who wants to wait 60s for a website to load?
Not saying you should use CGI (who does these days), but saying you could still use it if you want
The point is that PHP is much more performant and doesn't waste resources as NodeJS does. While you can run PHP on CGI with a decent bootstrap performance that won't get people annoyed the same can't be said of NodeJS. Nowadays people do PHP-FPM which is way faster at scaling up that any NodeJS process manager out there and doesn't sit wasting resources when a particular application doesn't have requests.
And there is... its called PHP. JS doesn't have this model because it is complete garbage slow and wouldn't ever run fine and reasonable in that model.