Why to use a daemon ?

In a standard web context, i.e. with a server executing PHP scripts to response clients' requests, every action asked by the clients is executed during the time the server build the response. For example sending a email to a lot of recipients will cause the execution of the following actions :

  • Get every email addresses the mailing should go to.
  • Foreach email addresses send the mail
These actions, more precisely the one sending the mail, can take some time, and repeating it hundreds of times could cause a timeout to the client's request. What happens then ? The emails not send before the timeout will never be sent, and the system will remain in an knwow state.

Asynchronous calls

To avoid this bad behaviour, a method called asynchronous call can be used. The principle is the following : insead of doing the whole action during the response, the action is put into a queue and is performed by another process which is independant of the client's request, process call "daemon".

The daemon will then look at the queue to check if there is some action to take, and do them until the queue is empty. When the queue is empty, it will wait until some new job come into the queue.

Asychronous calls are then implemented with two parts :
  • A queue which will contain the jobs to do
  • A daemon which will do the jobs in the queue.
This two parts will be largely used later in the concept and in the implementation.