Use Servlets or Server APIs

If you have to generate dynamic content, user your server API or Java servlets rather than slow CGI. CGI depends on forking user processes for each request. This works well with light loads, but becomes a bottleneck as the load gets heavier, for the same reason that inetd-spawned servers don’t scale well. Servlets and server API’s scale better.

CGI Internals and Performance Problems

Though the CGI mechanism for generating dynamic web content is very versatile, the basic structure of CGI limits its performance. The main performance penalty is that a new instance of the program is executed for each user’s request. This process exits immediately after sending its output back to the web server for forwarding to the browser. If the CGI program opens a database connection, the database connection must be reopened for the next instance of the CGI. This load on the operating system severely limits the number of CGI requests that can be serviced per second. CGI execution time is likely to be the bottleneck under any but the lightest loads. CGIs typically take far more CPU and other resources than serving HTML pages.

Let’s take a closer look at the sequence of events in starting a CGI program and where the performance problems are. When a CGI request comes in, the web server must parse the input URL and the request headers, recognize that the user desires to execute a CGI program, and begin the CGI with the fork() and exec() system calls. The parsing and the ...

Get Webmaster in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.