Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

request

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!

like image 335
Alex Avatar asked Mar 19 '11 17:03

Alex


People also ask

How does a server handle multiple requests?

The server opens a socket that 'listens' at port 80 and 'accepts' new connections from that socket. Each new connection is represented by a new socket whose local port is also port 80, but whose remote IP:port is as per the client who connected. So they don't get mixed up.

What happens if 2 requests are sent at nearly the same time data to the server?

Also, it is not possible for two requests to "come in at the same time" over the network, if there is only one network card - if they come in at the same time, EXACTLY (on a gigabit network), then the two packets will collide; both requests will be retried after a slightly random time, until there is no collision.

Which server can process multiple requests at a time?

A concurrent server handles multiple clients at the same time.

How does Apache handle multiple requests?

Requests are handled in parallel by the web server (which runs the PHP script). Updating data in the database is pretty fast, so any update will appear instantaneous, even if you need to update multiple tables.


1 Answers

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.

like image 147
Pascal MARTIN Avatar answered Oct 20 '22 16:10

Pascal MARTIN