WATCH
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.
Return value
Section titled “Return value”| Case | RESP2 | RESP3 |
|---|---|---|
| Always | Simple string OK |
Simple string OK |
| Inside a transaction | Error ERR WATCH inside MULTI is not allowed |
Error |
Examples
Section titled “Examples”127.0.0.1:6379> SET balance 100OK127.0.0.1:6379> WATCH balanceOK127.0.0.1:6379> GET balance"100"127.0.0.1:6379> MULTIOK127.0.0.1:6379(TX)> DECRBY balance 30QUEUED127.0.0.1:6379(TX)> EXEC1) (integer) 70Compatibility
Section titled “Compatibility”Same as Redis 7 and Valkey 8.
See also
Section titled “See also”Was this page helpful?
Thanks for the feedback.