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

Transactions / locks in Redis

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

Share

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

Redis supports simple transactions

Comparison of Redis and mysql transactions

Note: the difference between rollback and discard

If 2 statements have been successfully executed, the third statement has an error.

After Rollback, the sentence influence of the first two articles disappears.

Discard only ends this transaction, and the impact of the first two statements is still there.

Note:

In the statements that follow mutil, there are two possible situations when a statement goes wrong

1: there is a problem with grammar

In this case, when exec, an error is reported and all statements cannot be executed.

2: the grammar itself is correct, but there is something wrong with the object of application. For example, zadd manipulates list objects

After Exec, the correct statement is executed and inappropriate statements are skipped.

How can this be avoided if zadd manipulates list? This is the responsibility of the programmer)

(after using multi, it is not really executed, but is put into a queued (queue) and will only be executed after exec.)

Nothing has changed since discard, because it was just put in the queue. Strictly speaking, it can only be called cancel, not rollback)

Think about:

I am buying a ticket

Ticket-1, money-100

And there is only one ticket, if after my multi and before exec, the ticket is bought by someone else-that is, ticket becomes zero.

How should I observe this situation and no longer submit

Pessimistic thoughts:

The world is full of danger. Someone must rob me and lock the ticket. Only I can operate it. [pessimistic lock]

Optimistic thoughts:

There are not so many people robbing me, so I just need to pay attention.

-- is there anyone who can change the value of ticket [optimistic lock]

In Redis transactions, optimistic locks are enabled and are only responsible for monitoring that the key has not been changed.

Specific command-watch command

Example:

Redis 127.0.0.1 ticket 6379 > watch ticket (monitor ticket, if there is a change, the execution is not successful)

OK

Redis 127.0.0.1 6379 > multi

OK

Redis 127.0.0.1 6379 > decr ticket

QUEUED

Redis 127.0.0.1 6379 > decrby money

QUEUED

Redis 127.0.0.1 6379 > exec

(nil) / / returns nil, indicating that the monitored ticket has changed and the transaction is cancelled.

Redis 127.0.0.1 6379 > get ticket

"0"

Redis 127.0.0.1 6379 > get money

"200"

Watch key1 key2... KeyN

Function: monitor whether the key1 key2..keyN has changed. If so, the transaction will be canceled.

Unwatch

Function: cancel all watch snooping

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