In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Redis's support for transactions is relatively simple at the moment. Redis can only guarantee that commands in a client-initiated transaction can be executed continuously without inserting commands from other client. This is easy because redis handles all client requests in a single thread. In general, redis will process and return the processing result immediately after receiving a command from client, but when a client issues a multi command in a connection, the connection will enter a transaction context, and the subsequent command of the connection will not be executed immediately, but will be placed in a queue first. When exec commands are received from this connection, redis executes all commands in the queue sequentially. The running results of all commands are packaged together and returned to client. The connection then ends the transaction context.
Redis and mysql things
Mysql
Redis
open
Start transaction
Muitl
Statement
Ordinary sql
Ordinary command
Fail
Rollback rollback
Discard cancel
Success
Commit
Exec
1. Under normal circumstances, when a thing is turned on, there is no problem with syntax checking. It is not submitted directly, but is placed in a queue QUEUED.
The process of simply verifying the normal situation of a thing: a simple bank transfer. Wang has 200 yuan, zhao has 700 yuan, and wang transfers 100 to zhao.
127.0.0.1 set wang 6379 >
OK
127.0.0.1 set zhao 6379 >
OK
127.0.0.1 purl 6379 > multi
OK
127.0.0.1 decrby zhao 6379 > 100 # decrby minus 100
QUEUED
127.0.0.1 incrby wang 6379 > add one hundred
QUEUED
127.0.0.1 6379 > exec # because the transaction is open, it can only be "commit"
1) (integer) 600
2) (integer) 300
127.0.0.1 purl 6379 > mget wang zhao
1) "300"
2) "600" # # here you can see that the initial value is different from that set.
2. When a transaction is opened, if a transaction syntax check fails to pass the exception, the whole transaction will fail and will be directly discard
127.0.0.1 6379 > multi # start the transaction
OK
127.0.0.1 decrby zhao 6379 > 100 # minus 100 # and prompt in the queue
QUEUED
127.0.0.1 6379 > df # input an error
(error) ERR unknown command 'df'
127.0.0.1 purl 6379 > exec
(error) EXECABORT Transaction discarded because of previouserrors.
127.0.0.1 purl 6379 > mget zhao wang
1) "600"
2) "300" # # found that the original subtraction of 100 did not work
Third, the simple transaction of Redis, the syntax itself is correct, but there is something wrong with the applicable object, such as zadd manipulating list object.
After Exec, when the transaction is in the queue, the correct statement is executed and inappropriate statements are skipped.
127.0.0.1 purl 6379 > mget zhao wang
1) "500"
2) "300"
127.0.0.1 purl 6379 > multi
OK
127.0.0.1 decrby zhao 6379 >
QUEUED
127.0.0.1 purl 6379 > sadd wang df
QUEUED
127.0.0.1 purl 6379 > exec
1) (integer) 400
2) (error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1 purl 6379 > mget zhao wang
1) "400"
2) "300"
4. In Redis transactions, optimistic lock is enabled, which is only responsible for monitoring that key has not been changed. If KEY is changed and things are cancelled, take buying a ticket as an example: if there is no monitoring of key, the money will be lost, the money will be deducted without buying the ticket, and it will not be verified by watch itself.
Implementation process:
Window 1
127.0.0.1 purl 6379 > mget zhao wang ticket
1) "600"
2) "600"
3) "1"
127.0.0.1 6379 > watch ticket # Monitoring ticket
OK
127.0.0.1 6379 > multi # start the transaction
OK
127.0.0.1 decrby zhao 6379 > 200 # minus 200
QUEUED
127.0.0.1 6379 > decr ticket # get TICKET
QUEUED
Window 2:
127.0.0.1 decr ticket 6379 > purchase tickets directly, acting faster than window 1
Window 1:
127.0.0.1 purl 6379 > exec
(nil)
127.0.0.1 purl 6379 > mget zhao wang ticket
1) "600"
2) "600"
3) "0"
Watch can monitor multiple key. If one of the multiple key changes, the transaction will be cancelled.
Unlisten unwatch
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.