Things to introduce

Sometimes we need redis to send multiple commands in a row without interruption. In this case, we need to use the special redis command function. Redis has 5 commands to execute multiple commands. They are WATCH, MULTI (open an object), EXEC (execute a command), UNWATCH and DISCARD (DISCARD an object).

Redis transactions require the use of MULTI and EXEC commands, which are different from rollback transactions in relational databases. Redis executes commands within the scope of MULTI and EXEC commands one by one; Other client commands will be executed only after the redis transaction command is executed; We can already understand that the MULTI command is executed first, followed by the command we want to execute inside the thing, and finally the EXEC command that completes the command in the queue. When Redis executes something, it will actually open the queue and put the order we want to execute into the queue. Therefore, the command must be executed one by one before the next command can be executed.

What sample

Run the multi command first

127.0.0.1:6379> multi # open things OK 127.0.0.1:6379>Copy the code

Next, enter our command and put it in the queue

127.0.0.1:6379> set z1 666
QUEUED
127.0.0.1:6379> set z2 555
QUEUED
127.0.0.1:6379> set z3 222
QUEUED
127.0.0.1:6379>
Copy the code

Finally, the EXEC command executes the commands in the queue

1) OK 2) OK 3) OK 127.0.0.1:6379>Copy the code

Atomicity of things

The atomicity of things means that things either all succeed or all fail; That is, when an exception occurs things will roll back; However, things in Redis are not atomic. As shown in the following example, things run the same command after an exception occurs.

127.0.0.1:6379> multi OK 127.0.0.1:6379> set K1 000 QUEUED 127.0.0.1:6379> set K2 66 66 66 QUEUED 127.0.0.1:6379> set K3 000 QUEUED 127.0.0.1:6379> exec 1) OK 2) (error) ERR syntax error 3) OK 127.0.0.1:6379> keys k* 1) "k3" 2) "k1" 127.0.0.1:6379 >Copy the code

The discard discarded

Discarding means discarding all commands in the queue so that the multi command has not occurred. Discard command must be executed before exec command;

127.0.0.1:6379> multi OK 127.0.0.1:6379> set S1 111 QUEUED 127.0.0.1:6379> set S2 222 QUEUED 127.0.0.1:6379> discard # OK 127.0.0.1:6379> exec (error) ERR exec without MULTI 127.0.0.1:6379>Copy the code

Watch monitoring

We all know that Redis distributed locks are pessimistic locks that provide a good concurrency solution; However, a better way is to use the watch command to monitor whether a value changes. If the value changes, the command executed by exec will return NULL, so the watch command is an optimistic lock solution.

127.0.0.1:6379> set s3 ooo OK 127.0.0.1:6379> watch S3 OK 127.0.0.1:6379> set S3 555 # modify variable OK 127.0.0.1:6379> multi OK 127.0.0.1:6379> set s3 222 QUEUED 127.0.0.1:6379> exec (nil) 127.0.0.1:6379>Copy the code

Redis cannot execute the watch command between multi and exec. It must be executed before multi or an error will be reported.

The unwacth command unmonitors a variable;