innodb - How does MySQL handle lock queuing order for SELECT ... FOR UPDATE? - Stack Overflow
I have a scenario where multiple jobs (job1, job2, and job3) may arrive at the same time and need to execute in a serialized manner. I'm using MySQL with the InnoDB engine and a locking mechanism with a lock_table table. Here's the table schema:
CREATE TABLE lock_table (
key_name VARCHAR(255) NOT NULL,
UNIQUE KEY (key_name)
) ENGINE = InnoDB;
Here’s the workflow I’m trying to achieve:
job1 acquires a lock using:
SELECT * FROM lock_table WHERE key_name = 'resource1' FOR UPDATE;
This prevents other jobs from accessing the same resource (resource1) until job1 completes.
While job1 is running, both job2 and job3 also request the same lock:
SELECT * FROM lock_table WHERE key_name = 'resource1' FOR UPDATE;
These jobs should wait until job1 finishes.
After job1 finishes, I want the jobs in the queue (job2 and job3) to execute in the order they requested the lock.
My questions are:
- Does MySQL's SELECT ... FOR UPDATE mechanism guarantee that the jobs (job2 and job3) will execute in the order they requested the lock?
- Is there an official MySQL documentation link that explains how MySQL handles the order of queued lock requests?
- Is this approach the best practice for serializing job execution, or is there a better way to achieve this?
I am designing a system where multiple jobs (job1, job2, job3, etc.) may attempt to process the same resource concurrently. To ensure serialized execution, I'm using MySQL’s SELECT ... FOR UPDATE to lock the resource based on its key_name.
I have a scenario where multiple jobs (job1, job2, and job3) may arrive at the same time and need to execute in a serialized manner. I'm using MySQL with the InnoDB engine and a locking mechanism with a lock_table table. Here's the table schema:
CREATE TABLE lock_table (
key_name VARCHAR(255) NOT NULL,
UNIQUE KEY (key_name)
) ENGINE = InnoDB;
Here’s the workflow I’m trying to achieve:
job1 acquires a lock using:
SELECT * FROM lock_table WHERE key_name = 'resource1' FOR UPDATE;
This prevents other jobs from accessing the same resource (resource1) until job1 completes.
While job1 is running, both job2 and job3 also request the same lock:
SELECT * FROM lock_table WHERE key_name = 'resource1' FOR UPDATE;
These jobs should wait until job1 finishes.
After job1 finishes, I want the jobs in the queue (job2 and job3) to execute in the order they requested the lock.
My questions are:
- Does MySQL's SELECT ... FOR UPDATE mechanism guarantee that the jobs (job2 and job3) will execute in the order they requested the lock?
- Is there an official MySQL documentation link that explains how MySQL handles the order of queued lock requests?
- Is this approach the best practice for serializing job execution, or is there a better way to achieve this?
I am designing a system where multiple jobs (job1, job2, job3, etc.) may attempt to process the same resource concurrently. To ensure serialized execution, I'm using MySQL’s SELECT ... FOR UPDATE to lock the resource based on its key_name.
Share Improve this question edited yesterday Mark Rotteveel 109k224 gold badges155 silver badges217 bronze badges asked yesterday Mandar DhupdhareMandar Dhupdhare 91 bronze badge New contributor Mandar Dhupdhare is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1- Is that row the only one locked in each transaction? – Rick James Commented 18 hours ago
1 Answer
Reset to default 0As the MySQL manual on scheduling transactions says:
The CATS algorithm prioritizes waiting transactions by assigning a scheduling weight, which is computed based on the number of transactions that a transaction blocks. For example, if two transactions are waiting for a lock on the same object, the transaction that blocks the most transactions is assigned a greater scheduling weight. If weights are equal, priority is given to the longest waiting transaction.
So, if you can guarantee that the transactions performing the for update
statements will always block the same number of transactions, then innodb
will grant the for update
locks in the order the transactions issue it.
I'm sorry, but your third question is loaded and is impossible to provide an objective answer based on a single known criterion.
- 苹果WWDC2014:iOS 8新功能都有哪些?
- 台北电脑展周二开幕:Windows 8成焦点
- Pre-increment and Post-increment in C - Stack Overflow
- Can I Trigger Console Application From Window Service in Visual Studio c#? - Stack Overflow
- excel - Sum with multiple criteria and first matches only (excluding duplicates) - Stack Overflow
- javascript - How to delete content from content-editable field on facebookmessenger.com? - Stack Overflow
- amazon web services - Why is my AWS CDK Stack failing for an unclear reason? - Stack Overflow
- Receiver incorrectly registered? Android, Kotlin - Stack Overflow
- ggplot2 - alluvial diagram in R, Error: Data not in recognizable format - Stack Overflow
- postgresql - How to divide two NUMERIC values and get a result that has the maximum number of digits the NUMERIC type supports?
- i tried to get read edit manage storage in android java and output permission denied and not found in device setting - Stack Ove
- python - Windows java.io.EOFException error when trying to show spark dataframe - Stack Overflow
- javascript - error in getting data from firebase "user": "[object Object]"} in firebase - St
- javascript - Why does the iterator close after a break in a for...of loop? - Stack Overflow
- class - Python: trying to use exec(), pathlib and __file__ in combination fails - Stack Overflow
- azure - Unable to get a token to create B2C user using Graph API - Stack Overflow
- https - ASP.NET app is not listening on port 5001 after deployment - Stack Overflow