What exactly is a pre-fork web server model?

Hery picture Hery · Sep 14, 2014 · Viewed 20.9k times · Source

I want to know what exactly it means when a web server describes itself as a pre-fork web server. I have a few examples such as unicorn for ruby and gunicorn for python.

More specifically, these are the questions:

  • What problem does this model solve?
  • What happens when a pre-fork web server is initially started?
  • How does it handle request?

Also, a more specific question for unicorn/gunicorn:

Let's say that I have a webapp that I want to run with (g)unicorn. On initialization, the webapp will do some initialization stuff (e.g. fill in additional database entries). If I configure (g)unicorn with multiple workers, will the initialization stuff be run multiple times?

Answer

Joe Doherty picture Joe Doherty · Sep 17, 2014

Pre-forking basically means a master creates forks which handle each request. A fork is a completely separate *nix process.

Update as per the comments below. The pre in pre-fork means that these processes are forked before a request comes in. They can however usually be increased or decreased as the load goes up and down.

Pre-forking can be used when you have libraries that are NOT thread safe. It also means issues within a request causing problems will only affect the process which they are processed by and not the entire server.

The initialisation running multiple times all depends on what you are deploying. Usually however connection pools and stuff of that nature would exist for each process.

In a threading model the master would create lighter weight threads to dispatch requests too. But if a thread causes massive issues it could have repercussions for the master process.

With tools such an Nginx, Apache 2.4's Event MPM, or gevent (which can be used with Gunicorn) these are asynchronous meaning a process can handle hundreds of requests whilst not blocking.