WATCH

Since 1.1.0Transactions

Syntax

WATCH key [key ...]
  • Flags: fast, readonly
  • ACL categories: @fast, @transaction
  • Arity: -2

Watches keys for the next transaction on this connection: if any of them is changed, by any client, before EXEC, the transaction runs nothing and EXEC replies nil. This is optimistic locking: read, decide, and write only if nothing changed meanwhile, or try again.

Of two transactions racing on a watched key, the second always sees the first's write. Keys stop being watched at EXEC, DISCARD or UNWATCH.

Reply

  • OK.
  • ERR WATCH inside MULTI is not allowed.

Examples

client 1> SET bal 100
OK
client 1> WATCH bal
OK
client 1> MULTI
OK
client 1> DECRBY bal 10
QUEUED
client 2> SET bal 50
OK
client 1> EXEC
(nil)
client 1> GET bal
"50"