1

I have been doing some research in to java servlets and I am having trouble understanding why it more efficient then a cgi based solution.

The reason for my lack of understanding is that java servlets run on the thread per request model, meaning that a new thread is spawned or taken from a pool of threads each time a request is made. While a cgi based solution would create a new process per request.

My question is, why would creating a process per request be less efficient then a thread, after all in each process is a a thread doing the work, So why is cgi less effecient?

  • 2
    Keep in mind that this is a really, really outdated concern. It was important 20 years ago and may have been a major factor in establishing server-side Java as a big thing, but nobody uses CGI anymore to do webservers. – Michael Borgwardt Jan 22 '17 at 11:12
6

java servlets perform way better than cgi due to several reasons. A few that come to mind:

  1. cgi programs typically have much more initialization to do while starting up than a servlet has to do in order to handle a request. In an unoptimized scenario an entire Java VM has to be started, it has to load classes, transform the bytecode to machine code, etc.

  2. Starting a process is far more expensive than starting a thread in any operating system that I have ever heard of.

  3. Even if starting a process was as cheap as starting a thread, still, fetching an existing thread from a thread pool is much, much, much more efficient than starting a brand new thread.

  4. When a cgi program terminates, all of its state is lost, so when the next request comes in, this state must be reconstructed, (even if it is just configuration,) which usually means loaded from disk. Servlets can retain important state in memory which is reused across requests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.