Home  >  Article  >  Database  >  Redis transactions and application of optimistic locking

Redis transactions and application of optimistic locking

齐天大圣
齐天大圣Original
2020-05-15 12:16:381739browse

I believe that PHPer knows more or less about transactions, and transactions are often used in some scenarios. For example, if you purchase a product, you need to insert a piece of data into the order table, modify the balance field of the user table, etc. These two operations must either succeed together or both fail, otherwise data inconsistency will occur.

Redis also supports transaction features. Although it is not as powerful as the transaction function of a traditional relational database, it is very simple to use.

Transaction

MULTI

multi marks the start of a transaction. Subsequent instructions will be executed as an atomic when EXEC is executed.

DISCARD

The Discard command is used to cancel a transaction and abandon the execution of all commands within the transaction block.

EXEC

The exec command is used to execute commands within all transaction blocks

After introducing the above three commands, let’s complete a small function , User attention:

For example, if user A follows B, then B needs to be added to A's follow list, and A needs to be added to B's fans list; these two operations must be performed as an atom.

The implementation code is as follows:

$redis->multi()
    ->sadd('like:A', 'B')
    ->sadd('fans:B', 'A')
    ->exec();

Is it very simple to use?

Optimistic lock

First of all, let’s introduce what optimistic lock is and its corresponding pessimistic lock.

Pessimistic Lock: My thoughts are very pessimistic. I am not sure about my boyfriend. As long as I am with my boyfriend, no one can touch his boyfriend. Row locks, table locks, etc. in Mysql are all pessimistic locks.

Optimistic Lock: She is very optimistic and naive. She is assured of her boyfriend. When she is with her boyfriend and other friends chat with her boyfriend, she will not stop her. But every time the two of them are together, she will evaluate her boyfriend's recent status and determine whether there is something wrong with him. I will continue to be with him only if there is no problem, otherwise goodbye.

There is a scenario below: Redis stores token, but this token needs to be updated after a period of time. What pessimistic locking does is that I have to lock it before modifying it. At this time, no other request can modify it. The lock will not be unlocked until I complete the modification. The approach of optimistic locking is that it will not lock when modifying, but I will observe its status before modification. If it has not been modified, then I will make the modification. If the status has changed, then I will not make the modification. ,

How to use optimistic locking in Redis?

Redis provides a command Watch to mark all specified keys to be monitored and executed conditionally in the transaction.

The following is pseudo code:

$redis->watch('token');

$redis->multi()
    ->set('token','sfwefawfwefa323')
    ->exec();

Optimistic locks and pessimistic locks have their own usage scenarios. You can check the relevant information by yourself, so I won’t go into details here.

The above is the detailed content of Redis transactions and application of optimistic locking. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn