1. Transactions 1.1 multi and exec commands
Send the multi command to declare the transaction; execute the transaction through Exec
Redis does not support transaction rollback
Redis 127.0.0.1: 6379> set key 1 okredis 127.0.0.1: 6379> set key 2 okredis 127.0.0.1: 6379> sadd key 3 (error) Err operation against a key holding the wrong kind of valueredis 127.0.0.1: 6379> sadd key 3 4 (error) Err operation against a key holding the wrong kind of valueredis 127.0.0.1: 6379> sadd keynew 3 (integer) 1 redis 127.0.0.1: 6379> mutil (error) err Unknown command 'mutil 'redis 127.0.0.1: 6379> multiokredis 127.0.0.1: 6379> set key 3 queuedredis 127.0.0.1: 6379> set keynew 4 queuedredis 127.0.0.1: 6379> exec1) ok2) redis 127.0.0.1: 6379> get key "3" redis 127.0.0.1: 6379> Get keynew "4" redis 127.0.0.1: 6379>
1.2 watch command
Monitors a key value. If it is modified, subsequent transactions are blocked. Only defensive.
Redis 127.0.0.1: 6379> set key 1 okredis 127.0.0.1: 6379> watch keyokredis 127.0.0.1: 6379> multiokredis 127.0.0.1: 6379> set key 2 queuedredis 127.0.0.1: 6379> exec1) okredis 127.0.0.1: 6379> get key "2" redis 127.0.0.1: 6379>
Redis 127.0.0.1: 6379> set keywatch 1 okredis 127.0.0.1: 6379> set keywatch 2 okredis 127.0.0.1: 6379> watch keywatchokredis 127.0.0.1: 6379> set keywatch 3 okredis 127.0.0.1: 6379> multiokredis 127.0.0.1: 6379> set keywatch 8 queuedredis 127.0.0.1: 6379> exec (NiL) redis 127.0.0.1: 6379> Get keywatch "3" redis 127.0.0.1: 6379>
Redis advanced ---- transaction