In theory, if you were to open two computers that were perfectly synced together on a website that has a form. This form has fields where say for example the username has to be unique. Assuming both computers have the same information on the form, and in theory let's say that the submit button was pressed at the same time, and that these two computers have the exact same build and internet speed and the same response time from the server, whose information would be submitted to the database and whose information would be denied knowing the username field is unique.
-
I'm not sure there's any certain way to know whose form will be rejected in this case.– FrustratedWithFormsDesignerCommented Oct 28, 2013 at 19:25
-
4There will always be one process or resource (eg network card) that can only process a single item at any give moment in which case very tiny fractions of a second will decide which one will be processed first.– thorsten müllerCommented Oct 28, 2013 at 19:27
-
6FYI this is a good example of what is called a race.– user7043Commented Oct 28, 2013 at 19:29
-
2-1, "this question does not show any research effort"– GrandmasterBCommented Oct 28, 2013 at 19:46
-
4it's sometimes difficult to research what you cannot yet name– Wim OmbeletsCommented Oct 28, 2013 at 20:00
2 Answers
Given that both transactions reach the DBMS simultaneously, the database concurrency control mechanism would guarantee isolation of the transactions, in effect choosing one transaction to precede the other. Given the unique constraint of the username, the second would be rejected.
UPDATE:
In effect, avoiding / dealing with concurrency is done at the very lowest level, not even at the DBMS level, but at the CPU level. The instruction pointer is exactly that: it points at a single instruction. Even though instruction blocks (and thus, effectively, 'concurrent' database transactions) are interleaved at execution time, one will always precede the other or be committed first, causing the other to be forcefully rolled back. For completeness:
Concurrent Transaction Processing
When two transactions are being processed against a database at the same time, they are termed concurrent transactions. Although it may appear to the users that concurrent transactions are being processed simultaneously, this cannot be true because the CPU of the machine processing the database can execute only one instruction at a time. Usually, transactions are interleaved, which means that the operating system switches CPU services among tasks so that some portion of each transaction is carried out in a given interval. This switching among tasks is done so quickly that two people seated at browsers side by side, processing the same database, may believe that their two transactions are completed simultaneously; in reality, however, the two transactions are interleaved.
Kroenke & Auer, Database Processing Fundamentals: Design, Implementation (12th Ed.), Part 4 Multiuser Database Processing, p.342
-
How does this remain true on multi-cpu or multi-core systems? Commented Oct 28, 2013 at 20:55
-
@MatthewFlynn on multi-core/multi-cpu (sub) systems, threads can run truly concurrent. If they were to engage a race condition, the database management system would still assume concurrency control. Logical consistency would remain to be enforced by means of locking mechanisms. Apparently it has something to do with pooling locks and locking requests in a hierarchical manner, but tbh, that stuff is beyond me. Commented Oct 28, 2013 at 21:19
The winner would essentially be random. Whichever request is received first will win.
The definition of "received" could mean a lot of things. If we assume that your server processes all requests in the order that the network packets were received, and there are no software bugs, then once a packet enters the server's receive queue it is considered "received".
In the way you phrased your question, the packets will take different routes to the server and one will inevitably end up first. However even if they both arrive at the same time, one will "win" and be processed first.
Read up on the OSI model for an understanding on how networks function. Once you get down to the physical layer, the winner is whichever electron get access to the network interface first.
-
3Your first paragraph is correct. The other two seem fishy. Arriving at the network card is not what seals the deal, the web application has to receive the (probably buffered data), which means it must be assigned CPU time by the OS, and a some point it'll write something to a shared resource (database, some file, etc.). Or if the code is not written carefully enough, both requests may be accepted, leading to a violation of the "unique username" invariant. Or something even worse happens. Races are evil.– user7043Commented Oct 28, 2013 at 19:32
-
1@delnan You are correct, there are more systems involved than what I considered. Commented Oct 28, 2013 at 20:16