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

What's the use of redis transactions?

2025-01-16 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 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.

What is it

You can execute more than one command at a time, which is essentially a collection of commands. All commands in a transaction are serialized and executed sequentially without being inserted by other commands.

Execute more than one redis command at a time.

What can I do?

In a queue, a series of commands are executed one-time, sequentially and exclusively.

How to play?

The start of a redis transaction uses the MULTI command, which always replies to OK (I don't know if it will succeed), where the user can execute multiple commands at once instead of one by one. Redis will queue them up, and all commands will be invoked by the EXEC command

DISCARD aborts the batch operation.

Recommended (free): redis tutorial

Common command

The command describes that DISCARD cancels the transaction and discards all commands within the transaction block. EXEC executes commands within all transaction blocks. MULTI marks the beginning of a transaction block. UNWATCH unmonitors all key from the WATCH command. WATCH key [key...] Monitor one (or more) key, and if the key (or these) is altered by other commands before the transaction is executed, the transaction will be interrupted.

Case

Normal execution

Abandon transaction

All sit in a row

If there is one mistake, all the people will not even sit down and carry it out.

Unjust creditor

On this question, how to understand redis's support for transactions

Redis has partial support for transactions. In this part, the right is executed and the wrong is not executed.

Case:watch monitoring

Pessimistic lock / optimistic lock / CAS (Check and set)

Pessimistic lock (Pessimistic Lock), as its name implies, is very pessimistic. Every time you go to get the data, you think that someone else will modify it, so you lock it every time you get the data, so that others will block the data until it gets the lock. Many of these locking mechanisms are used in traditional relational databases, such as row locks, table locks, read locks, write locks and so on.

Table lock: lock the whole table. But this table may have many pieces of data, at this time a process has to make a wide range of changes, which will lead to more and more threads queued.

Row lock: lock each record

Optimistic lock

Optimistic lock (Optimistic Lock), as the name implies, is very optimistic. Every time you go to get the data, you think that others will not modify it, so you will not lock it. But when you update it, you can judge whether others have updated the data during this period. You can use mechanisms such as version number. Optimistic locks are suitable for multi-read applications, which can improve throughput.

Optimistic locking policy: the submitted version must be greater than the current version of the record to perform the update

Optimistic lock is not blindly optimistic, such as Zhang San changing WeChat account, Li Sigui qq account, at the same time, at the same time, the version number is 1, and then Zhang San changes the WeChat account to submit it. At this time, the version number is from 1 to 2, and Li Si will also submit it after the change. At this time, if it changes from 1 to 3, it will report an exception and revise it again.

Optimistic locks are usually used at work.

Initialize credit card available balance and arrears

Without tampering, monitor and then turn on multi to ensure that the two amount changes are in the same transaction.

During monitoring, it was found that another transaction modified the shared data, causing the transaction execution to fail.

Before you modify the data, you need to lock the watch, otherwise it will cause an error. If someone modifies my data, I will report an exception.

Tampering with tampering

The key is monitored. If the key is modified, the execution of the latter transaction will fail.

Unwatch

Unmonitor all key from the watch command

Once the exec is executed, the monitoring locks added before will be canceled.

Summary

Watch instruction, similar to optimistic lock, when a transaction commits, if the value of Key has been changed by another client, for example, a list has been passed by another client, the entire transaction queue will not be executed.

Multiple Keys are monitored before the transaction is executed through the WATCH command. If the value of any Key changes after the WATCH, the transaction executed by the EXEC command will be abandoned and a Nullmulti-bulk reply will be returned to notify the caller that the transaction failed.

3 stages

Open: start a transaction with MULTI

Queuing: queue up multiple commands into a transaction. Receiving these commands will not be executed immediately, but will be placed in the transaction queue waiting for execution.

Execution: transaction triggered by EXEC command

3 characteristics

Separate isolation operations: all commands in the transaction are serialized and executed sequentially. In the course of execution, the transaction will not be interrupted by command requests sent by other clients.

There is no concept of isolation level: commands in the queue are not actually executed until they are committed, because no instructions are actually executed before the transaction is committed, so there is no such a headache that "the query in the transaction needs to see the update in the transaction." this is a headache that can't be seen in an out-of-transaction query.

No guarantee of atomicity: redis if a command in the same transaction fails, subsequent commands will still be executed without rollback

Do not follow the AI in the traditional ACID

Thank you for reading this article carefully. I hope the article "what is the use of redis affairs" shared by the editor will be helpful to everyone? 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: 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