S
S
Snorway2016-02-27 16:01:51
MySQL
Snorway, 2016-02-27 16:01:51

What type of mutex to use?

Yii2 has two types of mutexes out of the box: FileMutex and MysqlMutex.
The first locks with flock(), the second with GET_LOCK. The question is, in what cases is it preferable to use each type of lock? After all, as far as I understand, both the first and the second solve the same problem and lead to the same result.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
justpusher, 2016-03-04
@Snorway

If all processes are on the same server, it is better to use flock, it is simpler and more reliable.
For example, if mysql is restarted or the connection to it is lost for another reason, you lose the lock obtained by GET_LOCK, but you may not know about it.
If you need to get locks on different servers, then GET_LOCK can help out. But carefully read the pitfalls. For example, in older versions of mysql, calling GET_LOCK again releases previous locks, while in newer versions, it acquires new locks in parallel.
PS if you need a lock to perform simple actions on a table, such as fetching a new job for processing, use SELECT FOR UPDATE.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question