Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example of using redis transaction

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the use of examples of redis transactions, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

First, let's briefly introduce the nature of redis transactions:

The essence of a Redis transaction is a collection of commands. Transactions support the execution of multiple commands at a time, and all commands in a transaction are serialized. During transaction execution, commands in the queue are serialized sequentially, and command requests submitted by other clients are not inserted into the transaction execution command sequence.

To sum up: a redis transaction is an one-time, sequential, exclusive execution of a series of commands in a queue.

Redis transactions have no concept of isolation levels:

Batch operations are cached before sending EXEC commands and are not actually executed, so there are no intra-transaction queries to see updates in the transaction, while out-of-transaction queries cannot be seen.

Redis does not guarantee atomicity:

In Redis, a single command is executed atomically, but the transaction is not guaranteed atomicity and there is no rollback. The execution of any command in the transaction fails, and the remaining commands will still be executed.

The three phases of a Redis transaction:

Start the transaction order to join the queue to execute the transaction

Redis transaction related commands:

Watch key1 key2.: monitor one or more key, and if the monitored key is changed by other commands before the transaction is executed, the transaction is interrupted (similar to optimistic locks)

Multi: marks the beginning of a transaction block (queued)

Exec: execute commands for all transaction blocks (once exec is executed, the previously added monitoring locks will be canceled)

Discard: cancels the transaction and discards all commands in the transaction block

Unwatch: cancel watch's monitoring of all key

Redis transaction use case:

(1) normal execution

(2) abandon the transaction

(3) if there are imperative errors in the transaction queue (similar to java compilation errors), all commands will not be executed when the EXEC command is executed.

(4) if there is a grammatical error in the transaction queue (similar to the runtime exception of java's 1ripple 0), when the EXEC command is executed, other correct commands will be executed and an error command will throw an exception.

(5) use watch

Case 1: use watch to detect balance, the balance data remains unchanged during the transaction, and the transaction executes successfully

Case 2: use watch to detect balance. After opening the transaction (marked 1), perform the operation in label 2 in the new window, change the value of balance, simulate other clients to change the data monitored by watch during the execution of the transaction, and then execute the command after marking 1. After the execution of EXEC, the transaction is not executed successfully.

Once the execution of EXEC starts the execution of a transaction, the monitoring of variables by WARCH will be canceled regardless of the success of transaction usage.

Therefore, when the transaction fails, it is necessary to re-execute the WATCH command to monitor the variables and open a new transaction to operate.

Summary:

The watch instruction is similar to an optimistic lock. If the value of any KEY in multiple KEY monitored by watch has been changed by other clients when the transaction is committed, the transaction queue will not be executed when the transaction is executed using EXEC, and a Nullmulti-bulk reply will be returned to notify the caller that the transaction failed.

Thank you for reading this article carefully. I hope the article "examples of the use of redis transactions" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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: 223

*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.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report