In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to solve the problem that Redis transactions do not support rollback". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Redis transaction
Any database must have its own transaction control mechanism. Redis transaction can execute multiple commands at a time, and its essence is a set of commands. All commands in a transaction are serialized and the commands in the queue are executed sequentially as the transaction is executed. Command requests submitted by other clients will not be executed until the transaction is completed.
In general: a redis transaction is an one-time, sequential, exclusive execution of a series of commands in a queue, so you have to understand the Redis notes first!
The difference between redis transactions and other database transactions:
1. Redis transaction is divided into three stages: start transaction, command queue, and execute transaction.
2. Redis transactions do not have the concept of isolation level: before redis sends exec commands, the command operation is only put into the queue cache and will not be actually executed, so it cannot be similar to relational data, querying changed operations within the transaction, and clients outside the transaction can not query the data within the transaction.
3. Redis transactions are not guaranteed to be atomic: redis transactions only guarantee that all commands will be executed if the command format is correct, or none of them will be executed. However, the whole transaction is not guaranteed to be atomic, and there is no rollback. When any one of the commands in the transaction fails, the rest of the commands will still be executed.
Redis command syntax structure:
1, watch key1 key2, etc.: monitor one or more KEY. If the value of key is changed by other commands during the execution of the transaction, the transaction is interrupted and none of them is executed. Redis completes the optimistic locking of the transaction through this mechanism.
2. Multi: used to specify the start of the redis transaction.
3. Exec: used to specify that redis transactions begin to execute (order, execute commands in all transactions at once). Once exec is executed, the monitoring locks added before will be canceled.
4. Discard: used to cancel the transaction. Discard all commands in the transaction.
5. Unwatch: cancel the monitoring of key in watch.
Let's analyze the execution process of redis transaction in detail through an example:
Example 1. Redis normal transaction flow: multiset key1 helloset key2 freeset key3 worldget key2exec
Example 2. Cancel the transaction with the following code: multiset key1 hello_1set key2 free_1set key3 world_1discardget key3
Example 3. When a command in a redis transaction goes wrong (that is, a command that does not exist, not a syntax error), all commands in the transaction will not be executed, as follows: multiset key1 hello_1setok key2 free_1set key3 world_1get key3exec
Example 4. There is a grammatical error in a command in a redis transaction. When exec is executed, other commands are executed as follows: multiincr key1 set key2 free_1set key3 world_1get key3exec
Example 5. Use watch to monitor the change of a key value to do optimistic locking for redis transactions. There are 100 yuan in the simulated keya account and 20 yuan in the keyb account, and then a transfers 50 yuan to b.
First, add the test data, the code is as follows:
Set keya 100set keyb 20 watch keya multidecrby keya 50incrby keyb 50exec get keya
Then, we open the second client and reduce the an account by 50 CNY in advance before the transaction is executed (exec) to see whether the optimistic locking mechanism of watch works. The code is as follows:
When a redis transaction commits, if the value of the watch command monitoring key changes while the transaction is in the cache queue, none of the commands in the transaction will be executed. At the same time, it returns a prompt that the transaction consumer failed to execute the transaction.
This is the end of "how to solve the problem that Redis transactions do not support rollback". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.