Save energy! Stop using CGI!
By Kai Seidler on Sep 18, 2009
The day CGI was invented was a great day for the Internet, but a dark day for the history of how-to-do-thinks-right. CGI was great, because it gave us (standard computer nerds) the ability to easily implement dynamically generated HTML pages - the predecessor of todays web applications. The interface was so ingeniously simple and powerful that everyone could use his favorite programming language for implementing web services. You could use C, Perl, AWK, PostScript or even the Bourne Shell to write your web application. And people did.
However, there is another side, a darker side, of CGI. It is not just ingeniously simple and powerful, in fact, it's also terribly slow. For every incoming HTTP request your system needs to fork a new process, and forking a process is an extremely expensive operation.
It was a great blessing, but nowadays there are so many programming languages especially suited for web development - designed to run in a web server environment - armed with the features and functions best fitting in the needs of a web application. I say PHP, you say Java. Whatever.
But if you stuck to a programming language of the past, please don't use CGI any more, use FastCGI or something comparable to connect your application with your web server. Or if you're lucky and your programming language is already aware of the web, there may be an even easier and better way.
For example, if your're a real Perl programmer, then use mod_perl. It's so simple to run unaltered(!) Perl-CGI scripts with mod_perl:
PerlModule ModPerl::PerlRun <Files ~ "\\.pl$"> SetHandler perl-script PerlResponseHandler ModPerl::PerlRun PerlOptions +ParseHeaders Options +ExecCGI </Files>
Compared to a simple Hello World in Perl with CGI...
% ab -n 1000 http://demo/helloworld.pl ... Requests per second: 304.84 [#/sec] (mean) ...
... the same script with mod_perl...
% ab -n 1000 http://demo/helloworld.pl ... Requests per second: 956.16 [#/sec] (mean) ...
...is three times faster. Without touching your Perl script.
And if I actually write a Hello World which takes advantage of mod_perl, I get:
# ab -n 1000 http://demo/helloworld ... Requests per second: 1777.50 [#/sec] (mean) ...
Nearly six times faster, and that's only a simple example script!
Stop using CGI! Green your IT! Save energy! Save the world! Save the Cheerleader!