Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does php and apache handle multiple requests?

Tags:

php

How does PHP handle multiple requests from users? Does it process them all at once or one at a time waiting for the first request to complete and then moving to the next.

Actually, I'm adding a bit of wiki to a static site where users will be able to edit addresses of businesses if they find them inaccurate or if they can be improved. Only registered users may do so. When a user edits a business name, that name along with it's other occurrences is changed in different rows in the table. I'm a little worried about what would happend if 10 users were doing this simultaneously. It'd be a real mishmash of things. So does PHP do things one at time in order received per script (update.php) or all at once.

like image 441
Norman Avatar asked Jan 24 '14 18:01

Norman


1 Answers

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.

Regarding the mish mash, for the DB, handling 10 requests within 1 second is the same as 10 requests within 10 seconds, it won't confuse them and just execute them one after the other.

If you need to update 2 tables and absolutely need these 2 updates to run subsequently without being interrupted by another update query, then you can use transactions.

EDIT:

If you don't want 2 users editing the same form at the same time, you have several options to prevent them. Here are a few ideas:

  1. You can "lock" that record for edition whenever a user opens the page to edit it, and not let other users open it for edition. You might run into a few problems if a user doesn't "unlock" the record after they are done.
  2. You can notify in real time (with AJAX) a user that the entry they are editing was modified, just like on stack overflow when a new answer or comment was posted as you are typing.
  3. When a user submits an edit, you can check if the record was edited between when they started editing and when they tried to submit it, and show them the new version beside their version, so that they manually "merge" the 2 updates.

There probably are more solutions but these should get you started.

like image 112
Fabien Warniez Avatar answered Oct 07 '22 06:10

Fabien Warniez