7 ms·
Web CGI programs aren't particularly slow these days
- milliams 3y agoWhat's "serverless" if not basically CGI scripts? (only slightly tongue-in-cheek)
- deleted 3y ago[deleted]
- kimi 3y ago(That's what I thought as well)
- danielmg 3y agoWhat's WASM but Java applets for Gen-Z?
- actionfromafar 3y agoJust run the Java in WASM! https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/java-in-the-browser-webassembly-tutorial-wasm-teavm-html-javascript https://www.theserverside.com/blog/Coffee-Talk-Java-News-Sto...
- cryptos 3y agoYeah, but CGI has fallen out of fashion and needed a re-branding!
- aitchnyu 3y agoAnd are we leaving performance on the table since jit based platforms never warm up?
- randomdata 3y agoNothing, really. The application not having an in-process web server, à la CGI, is what 'serverless' refers to. Although CGI is a defined protocol. Serverless is a more broad term, not limited to any particular protocol.
- terinjokes 3y agoI run a number of sites on a single server behind a reverse proxy, and the fastest to load is a CGI service, cgit. I use Traefik as the reverse proxy nowadays, which doesn't support CGI directly, so I've bridged it with Go's net/http/cgo. Even with this extra hop, it the performance is still excellent.
- oldpersonintx 3y agoweb serving is "done" you can serve most moderate-traffic services with any tech you like its time for smart people to move on to other problems
- nu11ptr 3y agoThis seems like a bit of a strange article to me. When we talk about being "slow", that is typically relative to a non-CGI technology, essentially something that keeps the code in memory ready to run. The reason CGI was "slow" was because 1) It had to start a new process for each invocation 2) When using tech that needed some form of warmup (I think PHP did??), you paid that for each invocation as well. In short, for some hobby or small setup it probably doesn't matter, and perhaps that is the authors point, but relative to an in-memory solution the situation probably hasn't changed much.
- sargun 3y agoAlso memory usage. An interpreter per active request would chew through RAM. I forget which Python project it was, but it introduced forking per request, and man that saved a lot of memory.
- cryptos 3y agoBoth points could be addressed by using a file system cache (or RAM disk) and a compiled language. A CGI application could, for example, be written in Go (https://pkg.go.dev/net/http/cgi https://pkg.go.dev/net/http/cgi).
- habibur 3y agoCGI as in a command line program lying on the disk, executed on every request and the output sent to browser is dog slow. Convert this one to fcgi, and it will get as fast as any other solution. In fcgi mode it runs as a service listening to a socket and reads and sends response over it while never terminating. Even php running on fcgi mode is far faster than the old styled mod_php.
- toast0 3y agoIf you have any reasonable OS and enough ram, the sectors your program actually uses will be in disk cache. Sure, there's maybe some syscalls and vm faults to get it mapped into the process, but it's not dog slow. If you don't have enough ram for it to be in the disk cache, chances are your fcgi daemon is going to get swapped out too. Now if your program insists on taking forever to start up, that's something that a long running daemon can help. I haven't benchmarked, but I really don't see why php in fcgi mode would be much faster than mod_php. I've run both modes and they're both very fast. But then someone uses one of the popular frameworks and hello world takes 20-40ms, so it barely matters how you run php.
- chrismorgan 3y agoEven with warm disk caches, interpreted languages tend to have pretty slow startup times, because they spend a lot of CPU time doing the same stuff over and over again. On my machine, the fastest of a dozen runs of just the interpreter is 66ms for Node.js (`time node -e ''`), and 14ms for CPython (`time python -c pass`). And my measurements a few years ago had overhead of about 1ms per module/file in Node.js, so if your source is spread over 100 files (which is honestly unrealistically low if you use any popular libraries and don’t bundle anything), your typical startup floor is now 200ms of CPU time, apart from disk time, on a good day. And some of those modules probably incur other costs, too. I have no figures on PHP, though if its execution model hasn’t changed from a decade ago most of this probably won’t affect it much because it effectively didn’t even persist modules. But I don’t know.
- toast0 3y ago
- Retr0id 3y ago> Yes, your CGI may slow down if you're getting a hundred hits a second. How likely is that to happen, and if it does, how critical is the slowdown? It's slightly ironic to be reading this phrase via the front page of HN.
- rootusrootus 3y agoIt's a good point, though, for the many developers who are writing software for a limited audience, such as internal tools. Can't be reached by HN, can't be hugged to death.
- j45 3y agoSpeed of iteration is most important for most development in the beginning.
- mrweasel 3y agoThe site is running as a cgi script, according to the article, and hitting the HN front page doesn't seem to have slowed it down in any way that I've noticed. So I think the point is well made.
- Retr0id 3y agoThe page was served to me with `Last-Modified: Thu, 28 Dec 2023 04:02:49 GMT`, which to me implies either aggressive caching, or that the publishing system acts like a static site generator.
- siebenmann 3y agoThe publishing system takes some pains to provide an accurate last-modified value for HTTP caching purposes (and an ETag too); internally it tracks the most recent modification time of all components that go into the page as it's (dynamically) assembled from various pieces. (I am the author of the linked-to article and also the author of the software it's running. Said software also has (on-disk) caching, but that's not why the last-modified is back in December of last year.)
- nwellnhof 3y ago> A real Python CGI would take longer to start because it has more things to import, but even then it's not necessarily terribly slow. That's where the author is wrong. Complex Python, Ruby or Perl scripts can easily take several hundred milliseconds just to import required modules.
- bee_rider 3y ago“not necessarily terribly” is a little weasel-wordy, but it does, I think, give them enough space to consider hundreds of milliseconds as not a big deal. Edit: see the responses for why “not necessarily terrible” is kind of annoying, now I have a comment that says there exists a use case where they care about it, and there exists one where they don’t.
- j45 3y agoIt might not be consistent to compare the entire request lifecycle instead of the original comment in only firing up.
- arp242 3y agoHundreds of milliseconds can be "not a big deal" depending on what your program is, how often it's run, etc. The last time I used CGI was for the "sign up to my newsletter" on my website; the endpoint for that would lock a file, append the email from the form, and close it. Even if that takes a second (which it didn't, more like 50ms) then ... that would actually be fine for this particular use case of a small newsletter. Of course for many other things it would be horrible. I think "not necessarily terrible" is a reasonable way to phrase things.
- surfingdino 3y agoDepends on the load levels or payment schedule. If we are talking about AWS Lambdas, hundreds of milliseconds are costing real money, which is why I write my Lambdas in Go instead of Python. That way we keep their exec time under 100ms which became unachievable with Python for the kind of code we're running. I love Python, but I don't love it enough to pay double or quadruple of what I pay for running Go code.
- deleted 3y ago[deleted]
- p0w3n3d 3y agoI would say that CGI might be cheaper in terms of CPU usage on cloud, as on idle it does not use CPU more than the system. I wonder what is the "idle" cost of java running spring for example. All I know is that if I run my application I am developing right now, the laptop I am using is trying to fly away (It's already warming up the engines), but it has also a postgres in another docker
- kevincox 3y agoAn idle persistent process doesn't use any CPU either (unless it has set up some background work) so there really shouldn't be much difference. I think at the extremes process-per-request and persistent processes look very similar. A fresh process will read data from the disk cache, but it may need to parse or transform that data before it is usable. A persistent process will have (some) pre-parsed data in memory. If you have swap enabled it will also be swapped out to disk if the process is idle. This may be faster if parsing that data into an in-memory structure is slow or slower if this results in more IO than the source data (which may be stored more efficently).
- kevincox 3y agoThe reason why CGI is slow is because it is missing many things that would typically be cached in persistent processes. Loading and starting the executable or interpreter can be slow (especially if you have lots of imports) but that probably isn't the biggest issue. Bigger issues are backend connections such as databases or external APIs. If those are on localhost than it is entirely possible that CGI will perform well enough for your application. But even if they are a handful of milliseconds away it can add up real quickly. If you don't have these issues than by all means use CGI. There is nothing wrong with it. But at some point having an in-memory cache is just going to be faster, even if your executable and various other dependence are cached by the OS page cache. Your in-memory "cache" will have all of the common data it needs pre-parsed and ready to use and may have backend connections already warmed up.
- mrweasel 3y agoIsn't web-functions/lambda going to have similar issues? They'll perform well enough, if they aren't terribly complex, but adding to many dependencies start up times could be an issue.
- mplewis 3y agoSome web function runtimes like AWS Lambda let you put dependency initialization outside the function itself. Depending on what the runtime does, you may get to keep these cached values for future invocations.
- kevincox 3y agoSort of, but lambda-like runtimes typically act like something in between process-per-request and a long-lived persistent process. AWS lambda at least will reuse the same execution environment for multiple requests. So simple things like caching in a hashmap or using a database connection pool will work and avoid these startup costs. You still get CGI-like behaviour on cold starts but once warm they perform much more like a persistent process. This ends up being a pretty good trade off for many use cases because you use little resources when idle and get pretty good latency and resource usage when busy.
- jrmg 3y agoIt seems kind of obvious that CGI can’t be ‘slow’ in modern terms. We were serving hundreds of hits per second on 200MHz web servers in the late 90s. Everything is _so much faster_ now that surely the performance must be acceptable nowadays?
- neverartful 3y agoReally! Reading this thread makes me feel like I've been sent back in time. Just wait till we get NSAPI/ISAPI released!
- at_a_remove 3y agoI always stuck to plain ole CGI in my Python/Windows systems. They were relatively fast enough and, well, the "faster" stuff just wasn't well-documented for the Python/Windows ecosystem.
- wharvle 3y agoA shitload of stuff from the Bad Old Days is "not particularly slow" now that everything's in cloud-whatevers and k8s clusters. Anyone remember when you offloaded something to the server to make it faster? Now it's like "well this is blazing fast on my machine, but we'll see how it does when it's deployed to prod..."
- pjmlp 3y agoI always joke that cloud is Tanenbaum's revenge. All that monolithic kernel performance wasted in endless layers of virtualization.
- wharvle 3y agoEvery time I use some cloud-thingy that's just fucking scheduling, running, and managing multiple processes, and it's way harder and more error-prone and less reliable and burns a multiple more person-hours to set up and manage than just, I don't know, doing that on an operating system, which is a thing specifically designed for that kind of work, plus performs like dogshit, I die a little inside. [EDIT] Incidentally, I don't hate all this stuff—I like docker-as-a-cross-distro-package-and-process-manager better than most of the rest Linux has to offer on that front, I find k8s kinda-nifty, but wish it 1) didn't always seem to involve so much goddamn yaml, which is so shockingly horrible that I can't believe it's still in so many things, and 2) were better-integrated with the underlying operating system(s) rather than reinventing so very many wheels and being another layer, rather than part of the system. Like I can vaguely imagine, through a hazy cloud of dream, some kind of hypothetical k8s-alike integrated into FreeBSD that I'd absolutely love.
- no_wizard 3y agoIf you're into FreeBSD, you may want to look into jails[0] [0]: https://docs.freebsd.org/en/books/handbook/jails/ https://docs.freebsd.org/en/books/handbook/jails/
- zoogeny 3y agoI think it is one of those things where even if it is "fast enough" in most cases, why not just use one of the readily available faster options anyway? It's not like setting up FastCGI is onerous. Apache/Nginx front-ends to a FastCGI process are as straightforward as you could ever want and the vast majority of languages you might use in this configuration have decades old battle-tested libraries and tooling. In fact, I drastically prefer having no front-end requirement. Even if node is "slow" compared to some C FastCGI benchmark, I like the operational simplicity of deploying code onto some stripped down Linux with minimal explicit dependencies. Go offering a single binary is even more attractive. I was always frustrated running Apache/Nginx, then maybe some process manager like PPM, fighting with the configuration across a bunch of different formats. While I admit that modern container based orchestration can be overly-complex, it doesn't leave me nostalgic for the days of CGI.
- pmontra 3y agoMy home server is all CGIs in Lua, Python and Ruby. Some endpoints have less than one hit per month. I don't care much about how many milliseconds I can shave from the startup time as well as from the run time. It will be fast enough whatever I do. For my customers, no CGIs. They do run on Python and Ruby though.
- jancsika 3y agoCould you speed up CGI by, say, having an array of processes ready and waiting to receive data? Like an array of Haskell CGI processes or something...
- duskwuff 3y agoNot as CGI is currently designed. Many components of the request, like the HTTP headers, request method, and path, get passed to the CGI script as environment variables. You could redesign CGI to change this, of course. But at that point, you've lost compatibility with existing CGI scripts, and you might as well use FastCGI and/or HTTP over a socket instead.
- zokier 3y agoWith some hackery you might be able to do it even with plain CGI. I'm thinking doing something like user-land exec[1] and pausing the execution of child just before `main` (or whatever entrypoint). You still need to do one fork per request, but still you could improve average case latency by having children pre-forked. No, it absolutely would not be worthwhile to go through such hoops, but fun thought experiment. [1] https://news.ycombinator.com/item?id=37025843 https://news.ycombinator.com/item?id=37025843
- whartung 3y agoThis is the Fast CGI model, using pre-forking in something like Apache. Apache creates a new process for each incoming connection, but pre-forks them (so they sit idle without a connection), and when those processes fire up, Fast CGI starts up the "cgi" program and just waits for the data. After the request, the process and cgi program remain and linger, ready for the next request.
- tonymet 3y agoWith FastCGI, CGI can be performant on humble hardware. I run smokeping via FastCGI on a Raspberry Pi Zero W (a $10 SBC). Response times are very good. CGI's biggest weakness is the IO & CPU needed to "boot" the interpreter. If you can keep the interpreter resident, you can reach acceptable performance.
- markstos 3y agoI argued in favor of CGI scripts over a decade ago as more web work was being switched to PHP or mod_perl. Even on "slow" hardware, they could often run in about a second. Which was still fast enough for a small business with a form that gets submitted a few times per day. For a lot of a small sites, needing to serve more than one request per second would be a great problem to have.
- gwd 3y agoRight, so maybe you can help me understand. Literally the only thing I've seen anywhere, both in TFA and in all the responses (including yours) is "CGI isn't really that slow." So that's a reason not to not use it; but are there any reasons to use it? Even in TFA, he demonstrates using a simple Golang CGI binary; but writing a simple Golang server that you pass through as a reverse proxy is just as simple.
- markstos 3y agoCGI is a good fit if you don't maintain the server it runs on. You've got a basic "user" account and can just upload a CGI script. For example, you've got a static website and need a simple contact form handler. Apache had an option to run the scripts as your user, rather than default of "nobody". That allows segmenting the processes of different users on the same server.
- anonacct37 3y agoNo memory leaks (unless you use shared memory). No weird connection pooling and caching issues or delays in picking up a config change. Every request is a fresh start, enforced by the OS. Buggy code (like image processing can only crash the current request).
- randomdata 3y agoIf you avoid writing the simple server you can assert your familiarity with serverless on your resume.