Kendra Little walks through using OStress to test concurrency:
Even though I’m taking out UPDLOCKS, the following race condition pattern can still occur
- Session A takes out an UPDLOCK, sees that key 266 does not exist, releases its UPDLOCK, and prepares to insert
- Session B takes out an UPDLOCK, sees that key 266 does not exist, releases its UPDLOCK, and prepares to insert
- Session A runs its insert
- Session B attempts to run its insert, but fails because of a duplicate key error
We need to hold that lock.
Understanding concurrency is one of the toughest parts of being a database developer, especially because it’s historically been difficult to test it. I like what Kendra’s done here, making the process easy to follow.