每天处理2亿次CGI请求
Serving 200M requests per day with a CGI-bin

原始链接: https://simonwillison.net/2025/Jul/5/cgi-bin-performance/

Jake Gold在避免了CGI(90年代的网页技术,每个请求都会产生一个新进程)数十年之后,又重新审视了它。起因是他开发的`datasette-ripgrep`插件的成功,该插件使用了`ripgrep`命令行工具。他在现代硬件上测试了一个基于Go语言和SQLite的CGI程序。 结果令人惊讶:一台16线程的AMD 3700X处理器能够每秒处理超过2400个请求,每天超过2亿个请求。他将此归功于CPU速度、核心数量的提升,以及使用Go和Rust等更快语言的优势。 虽然传统的CGI架构在Perl和Java等较慢的语言下开销很大,但现代语言的效率以及通过进程隔离利用多CPU核心的优势使得CGI成为一个可行的选择。Jake建议重新考虑CGI风格的Web应用程序架构,将它的优点与Go和Rust的速度结合起来。

Hacker News上的一篇文章讨论了如何使用CGI-bin每天处理2亿个请求。一些人承认CGI具有怀旧的吸引力,另一些人则认为它与现代替代方案相比效率低下且可能不安全。核心争论围绕着CGI的安全性漏洞,尤其是在输入验证方面。 一位用户指出,过去的CGI漏洞源于不充分的输入清理。反驳意见认为,现代语言提供了减轻这些风险的库,并且正确的服务器管理可以防止注入攻击。反对CGI的论点是它需要完美地实现输入验证,这很难做到。 一位用户建议新进程可以带来安全优势,并与Postgres进行了类比,后者每个连接使用一个新的后端进程。共识是,虽然CGI简化了多语言Web应用程序的开发并提供了进程隔离,但现代代理设置可以达到类似的结果。
相关文章

原文

Serving 200 million requests per day with a cgi-bin (via) Jake Gold tests how well 90s-era CGI works today, using a Go + SQLite CGI program running on a 16-thread AMD 3700X.

Using CGI on modest hardware, it’s possible to serve 2400+ requests per second or 200M+ requests per day.

I got my start in web development with CGI back in the late 1990s - I was a huge fan of NewsPro, which was effectively a weblog system before anyone knew what a weblog was.

CGI works by starting, executing and terminating a process for every incoming request. The nascent web community quickly learned that this was a bad idea, and invented technologies like PHP and FastCGI to help avoid that extra overhead and keep code resident in-memory instead.

This lesson ended up baked into my brain, and I spent the next twenty years convinced that you should never execute a full process as part of serving a web page.

Of course, computers in those two decades got a lot faster. I finally overcame that twenty-year core belief in 2020, when I built datasette-ripgrep, a Datasette plugin that shells out to the lightning fast ripgrep CLI tool (written in Rust) to execute searches. It worked great!

As was pointed out on Hacker News, part of CGI's problem back then was that we were writing web scripts in languages like Perl, Python and Java which had not been designed for lightning fast startup speeds. Using Go and Rust today helps make CGI-style requests a whole lot more effective.

Jake notes that CGI-style request handling is actually a great way to take advantage of multiple CPU cores:

These days, we have servers with 384 CPU threads. Even a small VM can have 16 CPUs. The CPUs and memory are much faster as well.

Most importantly, CGI programs, because they run as separate processes, are excellent at taking advantage of many CPUs!

Maybe we should start coding web applications like it's 1998, albeit with Go and Rust!

联系我们 contact @ memedata.com