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

The usage of redis transaction

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the usage of redis transactions for you. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Redis transaction

MULTI, EXEC, and DISCARD and WATCH are the basis of Redis transactions. Used to explicitly open and control a transaction that allows a set of commands to be executed in a step. And provide two important guarantees:

All commands in the ● transaction are serialized and executed sequentially. During the execution of a Redis transaction, there are no requests made by another client. This ensures that the command queue is executed as a separate atomic operation.

All commands in the ● queue are either processed or ignored. The EXEC command triggers the execution of all commands in the transaction, so when the client loses its connection to the server in the context of the transaction

If ● occurs before the MULTI command is called, no commands is executed

● if the EXEC command is called before that, all commands are executed.

At the same time, redis uses AOF (append-only file) to write transactions to disk using an additional write operation. In case of downtime, process collapse, etc., you can use redis-check-aof tool to repair append-only file to make the service start normally and resume some operations. (recommended: "Redis Video tutorial")

Usage

Use the MULTI command to explicitly open the Redis transaction. The command always responds with OK. At this point, the user can issue multiple commands, and instead of executing them, Redis queues them. After EXEC is called, all commands are executed. Calling DISCARD clears the commands queue in the transaction and exits the transaction.

The following example increments the bonds foo and bar atomically.

> MULTIOK > INCR fooQUEUED > INCR barQUEUED > EXEC1) (integer) 12) (integer) 1

As you can see from the above command execution, EXEC returns an array where each element is the result of a single command in the transaction in the same order as the command was issued.

When the Redis connection is in the context of the MULTI request, all commands reply with the string QUEUED (sent as a status reply from the perspective of the Redis protocol) and are queued in the command queue. Only when EXEC is called will the queued command be executed, and the real result will be returned.

So much for sharing the usage of redis transactions. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Database

Wechat

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

12
Report