Alex
Alex

Reputation: 1322

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!

Upvotes: 15

Views: 6240

Answers (3)

Jay Brunet
Jay Brunet

Reputation: 3750

With a LEMP stack, most people are using PHP-FPM.

Overview points:

  • Nginx tends to multitask many requests easily, using very little memory.
  • PHP-FPM lets you define the number of "children" to process PHP requests concurrently. PHP has various timeout options, as these processes should do what they need to do and exit ASAP.
  • If you have X children, you'll need at least X database connections to serve those concurrent PHP processes, which requires even more memory. Of course the number of connections depends on the application. And "slow queries" can easily create a traffic jam in your system.

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 400972

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.

Upvotes: 9

John Parker
John Parker

Reputation: 54445

This is pretty much a non-issue, as modern operating systems will happily let multiple processes read a given file at the same time. (i.e.: They're "processed" in parallel.)

However, if a given file needs to be written to by multiple scripts at the same time, then PHP will effectively "stack" the pages that are attempting to gain write access until the file has become available again.

Upvotes: 5

Related Questions