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

Redis transaction parsing

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

1.redis transaction related commands

MULTI:

The MULTI command is used to open a redis transaction. MULTI changes the client that executes the command from a non-transactional state to a transactional state, and the return value of this command is always OK.

EXEC:

The EXEC command is used to execute all previously queued commands in the transaction and restore the connection state to normal. The server executes the commands in the transaction sequentially and returns the results of all the commands in an array to the client. Each element in the array is a reply to each command in the atomic transaction.

WATCH:

The WATCH command is mainly used to implement optimistic locking, namely CAS (I will write an article about the CAS algorithm later). If the key value of the WATCH is modified by other clients during the transaction execution, the server will directly reject the transaction execution and return an empty recovery (nil) representing failure, that is, the EXEC command will return a null value of null.

DISCARD:

The DISCARD command is used to abandon the transaction, and if the server receives the DISCARD command after receiving the EXEC command from the client, it will not execute the transaction. The return value of this command is always OK

UNWATCH

The UNWATCH command refreshes all WATCH KEY operations of the previous transaction, and if you call EXEC or DISCARD, you don't need to call UNWATCH manually. The return value of this command is always OK

The execution process of 2.redis transaction

(1)。 Open a transaction

The transaction starts with the MULTI command, and the MULTI command changes the client that executes the command from a non-transactional state to a transactional state.

(2)。 Order to join the team

After the transaction is started, the commands sent by the client to the server will not be executed immediately. The server will put these commands into a queue of transactions and return them to the client QUEUED at the same time.

(3)。 Transaction execution

After the client sends the EXEC command to the server, the tasks in the server transaction queue will be executed sequentially and an array will be returned. The elements in the array are the result of each command execution.

Characteristics of 3.redis transactions

We all know the nature of database transactions, ACID, so does redis meet the characteristics of ACID?

A: atomicity

The atomicity of a database means that a transaction is either executed or not executed.

Redis transactions are atomic, and the commands in the transaction queue either succeed or are abandoned. However, if there is an error in the execution of a command, it will not affect the execution of other commands. In the official explanation given by redis, it is believed that the redis command will fail only if it uses the wrong syntax or key with the wrong data type, and the command does not detect an error during the queue. It is also because redis officials feel that redis will complicate redis if it supports transaction rollback.

C: consistency

Http://www.chacha8.cn/detail/1132398239.html

Database consistency means that the operation of transactions does not change the consistency of data in the database.

Redis also supports consistency:

After the transaction is started, if an error occurs when the command is queued, it will not be returned to the client QUEUED. In this way, the client can determine whether there is an error, and then call the DISCARD command to abandon the transaction. If the client still sends the EXEC command at this time, the redis server will also refuse to execute it, thus ensuring consistency.

If an error occurs in one command during execution, other commands will be executed normally to ensure consistency.

When the server is down, because the data of redis is stored in memory, all the data in memory will be lost, which ensures consistency.

I: isolation

Database isolation refers to the state in which more than two transactions are not interlaced. Because this may lead to data inconsistency.

Redis is single-threaded, and redis guarantees that transactions will not be terminated, so redis also supports isolation.

D: persistence

Zhengzhou infertility hospital: http://wapyyk.39.net/zz3/zonghe/1d427.html

The persistence of a database transaction means that after the transaction is executed successfully, the changes made by the transaction to the database are persisted in the database and will not be rolled back for no reason.

Redis does not support persistence in non-persistent memory mode, but redis provides two persistence mechanisms: AOF and RBD. RBD does not execute BGSAVE until certain conditions are met, so it does not have persistence, while in AOF mode, redis has persistence when appendfsync is always.

Summary of 4.redis transaction parsing

The execution process of redis transaction: watch listens to the specified Key,MULTI to open the transaction, the command enters the transaction queue, EXEC executes the command and returns the execution result.

Redis supports atomicity, consistency, isolation of the database, and persistence only under certain circumstances.

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