Home >Database >Redis >What is the use of redis transactions

What is the use of redis transactions

藏色散人
藏色散人Original
2019-06-19 11:17:032927browse

What is the use of redis transactions

Redis Transaction

MULTI, EXEC, DISCARD and WATCH are the basis of Redis transaction. Used to explicitly start and control a transaction, they allow a set of commands to be executed in one step. And provides two important guarantees:

● All commands in the transaction will be serialized and executed in order. During the execution of a Redis transaction, no request issued by another client will occur. This ensures that the command queue is executed as a single atomic operation.

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

● If it occurs before calling the MULTI command, no commands are executed;

● If the EXEC command is called before, all commands will be executed.

At the same time, redis uses AOF (append-only file) to write transactions to disk using an additional write operation. If there is a downtime, process crash, etc., you can use the redis-check-aof tool to repair the append-only file, so that the service can start normally and resume some operations. (Recommended: "Redis Video Tutorial")

Usage

Use the MULTI command to explicitly start the Redis transaction. This command always responds with OK. At this point the user can issue multiple commands, and Redis will not execute these commands, but queue them. After EXEC is called, all commands will be executed. Calling DISCARD can clear the commands queue in the transaction and exit the transaction.

The following example atomically increments the keys foo and bar.

>MULTI
OK
>INCR foo
QUEUED
>INCR bar
QUEUED
>EXEC
1)(整数)1
2)(整数)1

As can be seen from the above command execution, EXEC returns an array, in which each element is the return result of a single command in the transaction, and the order is the same as the order in which the command was issued.

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

The above is the detailed content of What is the use of redis transactions. 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
Previous article:Where is redis used?Next article:Where is redis used?