Watch, multi, and other commands involved in redis transactions

Source: Internet
Author: User
Redis watch command

Purpose:
Monitors one or more keys. If the key is changed by another command before the transaction is executed, the transaction is interrupted.
Usage:

redis 127.0.0.1:6379> WATCH key1 key2OK
Redis unwatch command

Purpose:
This command is used to cancel monitoring of all keys by the watch command.
Usage:

redis 127.0.0.1:6379> UNWATCHOK
Redis multi command

Purpose:
Used to mark the start of a transaction block. Multiple commands in the transaction block are put into a queue in sequence, and finally executed by the exec command atomicity (atomic.
Usage:

Redis 127.0.0.1: 6379> multi # MARK transaction start okredis 127.0.0.1: 6379> incr user_id # multiple commands are queued in order for queuedredis 127.0.0.1: 6379> incr user_idqueuedredis 127.0.0.1: 6379> incr 2017127.0.0.1: 6379> pingqueuedredis 127.0.0.1: 6379> exec # execute 1) (integer) 12) (integer) 23) (integer) 34) Pong
Use watch to implement incr
The procedure is as follows: Watch mykey val = get mykey val = Val + 1 multi set mykey $ Val Exec

The watch command is used to monitor the key before obtaining the value of mykey, and then the SET command is surrounded in the transaction, so that each connection can be effectively guaranteed before exec is executed, if the value of mykey obtained by the current connection is modified by the client of another connection, the exec command of the current connection will fail to be executed. In this way, the caller can determine whether the Val has been reset successfully after determining the return value.

Note:
  • Because the watch command only blocks the execution of a transaction after the monitored key value is modified, it cannot be ensured that other clients do not modify this key value, therefore, in general, we need to re-execute the entire function after the exec execution fails.
  • After the exec command is executed, the monitoring of all keys is canceled. If you do not want to execute the commands in the transaction, you can also use the unwatch command to cancel monitoring.
Example:
Open two redis-CLI command line windows session 1 and Session 2 Session 1: redis 127.0.0.1: 6379> set Test 1 # Set test = "1" okredis 127.0.0.1: 6379> get test # obtain the value of test as "1" 1 "redis 127.0.0.1: 6379> watch test # monitor testokredis 127.0.0.1: 6379> multi # enable transaction okredis 127.0.0.1: 6379> set Test 2 # Set test to "2" queuedredis 127.0.0.1: 6379> exec # execute the exec command of Session 1 after session 2 is executed, and the execution fails (nil) redis 127.0.0.1: 6379> get test # obtain the value of test. We found that the value of test is "3" "3" redis 127.0.0.1 set in session 2: 6379> unwatch # unwatch all keyokredis 127.0.0.1: 6379> set Test 4 # The value of test for non-transactional changes is "4" okredis 127.0.0.1: 6379> get test # Get test = "4" "4" Session 2: redis 127.0.0.1: 6379> get test # Get the test = "1" 1 "redis 127.0.0.1: 6379> watch test # monitor testokredis 127.0.0.1: 6379> multi # enable transaction okredis 127.0.0.1: 6379> set Test 3 # Set test to "3" queuedredis 127.0.0.1: 6379> exec # execute transaction 1) okredis 127.0.0.1: 6379> get test # Get test = "3" 3"

Watch, multi, and other commands involved in redis transactions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.