Search code examples
phprequest

How are multiple requests to one file on a server dealt with?


This a bit of a theoretical question coming from someone very inexperienced in programming and servers, but here goes.

If I have a PHP file stored on a server, what happens if multiple people start accessing this script at the same time? Do multiple requests stack or can they be processed in parallel? What should I look up to better understand how servers work?

I hope this makes sense!


Solution

  • The webserver (Apache, for example) is generally able to deal with several requests at the same time (the default being 200 or 400 for Apache).

    If the requests correspond to read-only situations, there should be no problem at all : several process can read the same file at the same time -- and if that file is a PHP script, several process can execute it at the same time.

    If your script is query-ing a database, there should not be too much problems : databases are made to deal with concurrency situations (even if reads scale better than writes, which may have to be stacked, if they modify the same data).

    If your script is trying to write to a file, you should put some locking mecanism in place (using flock, for example), to avoid that ; as a consequence, each process will wait until there is no other writing to the file, before writing.