User logins - PHP SQL - race conditions?
Hi I'm new to this forum and relatively new to PHP / SQL.
My question is, in general, does SQL with PHP cause any race conditions?
eg. If a user navigates to a page that executes some SQL such as logging in /registering to a site, the server runs that script right? What would happen if two, three, or even a hundred people are all trying to register at the same time? If the server is halfway through one of the scripts, would the next user interrupt this and cause the server to execute it from the start?
How do large sites usually handle logins / register scripts to avoid this? I know about threads but afaik PHP doesn't allow multiple threads.
Thanks for reading.
I imagine they perform SQL_CONSTRAINT_VIOLATION (or some kind of constraint check if it is a different database) checks to make sure there are no collisions (people entering the same username for example at the same time).
Usually the only potential race condition for a registration system would be separate users trying to use the same user ID and/or email address. If you define that field as unique in the relevant DB table, as alluded to above you can simply test for the relevant constraint error (MySQL error number 1062). Then when you run your insert query, if it fails, check the mysql_errno() or mysqli_errorno() (hopefully you're using the latter?), and if it's a 1062 then let the user know that ID is already taken.
Thanks for the response. Yeah, I have a check for ID column that's unique.
Still a little hazy on the details.
Lets say there is a script that takes 3 seconds to complete.
0 sec - User 1 navigates to script
1 sec - User 2 navigates to script
2 sec - User 3 navigates to script
3 sec - User 1 script complete?
What would happen in this situation? Does the server finish executing User 1 before it starts User 2; so User 2 and 3 have to wait a long time?
Does User 3 interrupt the previous ones? Or does the server execute them like threads?
The actual sequence of events may vary depending on server OS, number of CPUs, (number of servers if using a load balancer), type of web server and PHP installation, and so forth.
Generally speaking, each request will run in its own process and memory space and won't care about any other requests. (While an individual PHP script may not be able to multi-thread, that does not mean the web server cannot spawn multiple, independent PHP processes.) Probably the only times they will have any sort of conflicts will be when hitting the database or during file I/O, and that's usually not an issue unless they are both trying to write to the same DB row or the same file. A DBMS usually handles simple cases of this just fine without you having to worry about it, though in more complex situations you may need to implement some DB transaction processing with table/row locking. (I think I've used transactions once in the past year, and don't remember when I last did any explicit DB table locking. I think I had to implement some file-locking once in the past year or 2, as well.)
Ah, thanks NogDog for clarifying this for me
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)