====== Optimizing Server Performance======

Normally, one of the web server's default configurations for SoSci Survey is perfectly sufficient for 500 or 1000 questionnaires per hour. However, if all that is available is less powerful software (e.g. a laptop as a mobile survey server), or if a larger number of questionnaires are running at the same time, the configuration of the web server has to be optimized slightly. 

If a large number of parallel request is expected, using ''nginx'' instead of ''apache2'' can save lots of RAM.


===== Reduce the Load on the Database =====

SoSci Survey uses a caching mechanism for all questions and items so that these do not have to be loaded constantly from the database. Furthermore, the next questionnaire page is already sent before responses have been written to the database -- this means the participant does not have to wait for an unnecessarily long amount of time.

In the default configuration, SoSci Survey saves data collected in the questionnaire in the database after "Next" is clicked on anyway. This ensures maximal security for the data, but overloads the database somewhat. SoSci Survey therefore offers a [[caching|Write Caching for Questionnaires]]. This means questionnaires are saved entirely in cache files and are only written to the database after a time-delay, as soon as the server load allows this. To ensure questionnaires are transmitted regularly to the database, the [[:en:server:crontask|script for scheduled tasks]] has to be run at regular intervals. 

In terms of performance, it is important that the database can fit indexes in RAM and not have to access the hard disk constantly. If the installation has to hold a lot of questionnaires (over 500,000), it makes sense to modify the storage configuration ([[prepare1#mysql_configuration|Server Preparation I: MySQL Configuration]]).



===== Persistent Database Connections =====

PHP (and so SoSci Survey as well) starts a new script for every request. This means a new script is started each time after "Next" is clicked on, and every time an item is inserted. Each script must first be connected to the database. 

This connection costs time that could actually be saved as the last script can just leave the database connection open. This technique is known as persistent database connection and can be enabled in SoSci Survey's installation routine. Call up ''/admin/install.php'' in the browser to do this. 

However, there is a dark side to persistent connections: if the web server starts a lot of processes in parallel, these can take up all of the connections to the database. You therefore have to make sure that the database accepts at least as many parallel connections as the server starts parallel server instances (threads). If this is not guaranteed, requests to the database will be acknowledged with errors when there is a high sever load. The corresponding MySQL variables are explained in the  [[start#mysql_settings|Installation Manual]].

Also, make sure that web server connections are not kept open for too long. Two seconds are perfectly sufficient for SoSci Survey. The dwell time for a page in the questionnaire normally amounts to 1-2 minutes -- you cannot and, indeed, do not want to keep the connection open for any longer than this. Then you can release it for another user straightaway.



===== Web Server nginx =====

Many Linux servers use Apache as the program for the web server. Without doubt, this web server is extremely efficient, but requires a lot of storage in the default configuration if a lot of files are accessed by the server at the same time.

The Multi-Processing Module [[http://httpd.apache.org/docs/2.2/en/mod/worker.html|MPM Worker]] for Apache provides a solution to this -- however, a range of modifications to Apache and PHP configurations are necessary to ensure smooth operation.

Another solution is to switch from Apache to [[http://en.wikipedia.org/wiki/Nginx|nginx]], which is an extremely efficient open source web sever. After implementing PHP (PHP FPM), nginx and SoSci Survey work seamlessly together.