Skip to content

Ask the HotKV docs

Answers come only from these docs and can be wrong; check the linked pages before you rely on them.

WATCH

Last updated
WATCH key [key ...]
Group
Transactions
Complexity
O(1) per key
Arity
-2 (at least 2 words, counting the command name)
Flags
fast
Keys
first 1, last final argument, step 1
HotKV Cloud
All plans
On-Premise
no license needed

WATCH marks keys for optimistic locking. If any watched key is modified by another client, deleted, or expires before your EXEC, the transaction is not run and EXEC replies with a null. This lets you read values, decide what to write, and apply the writes only if nothing changed in between, retrying otherwise.

Watched keys are unwatched by EXEC, DISCARD and UNWATCH, and when the connection closes. Watching a key that is already watched keeps its original state. Watching a key is not an access to it.

WATCH must be sent before MULTI.

Case RESP2 RESP3
Always Simple string OK Simple string OK
Inside a transaction Error ERR WATCH inside MULTI is not allowed Error
127.0.0.1:6379> SET balance 100
OK
127.0.0.1:6379> WATCH balance
OK
127.0.0.1:6379> GET balance
"100"
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379(TX)> DECRBY balance 30
QUEUED
127.0.0.1:6379(TX)> EXEC
1) (integer) 70

Same as Redis 7 and Valkey 8.

UNWATCH, MULTI, EXEC