WATCH

Introduction and Use Case(s)

WATCH is a Redis command used to provide the functionality of optimistic locking in transactions. It monitors the specified keys and ensures that the transaction only executes if the watched keys remain unchanged. Typical scenarios include implementing counters, maintaining financial balances, or other state changes where consistency is paramount.

Syntax

  1. WATCH key [key ...]

Parameter Explanations

  • key: One or more keys to monitor for changes. If any of these keys are modified before the EXEC command, the entire transaction will be aborted.

Return Values

  • OK: If the keys were successfully watched.

Example:

  1. dragonfly> WATCH mykey
  2. OK

Code Examples

  1. dragonfly> SET mykey "initial"
  2. OK
  3. dragonfly> WATCH mykey
  4. OK
  5. dragonfly> MULTI
  6. OK
  7. dragonfly> SET mykey "updated"
  8. QUEUED
  9. dragonfly> EXEC
  10. (nil) # This indicates that the transaction was aborted because another client modified 'mykey' after the WATCH command.

In another session:

  1. dragonfly> SET mykey "changed"
  2. OK

Back to the first session:

  1. dragonfly> EXEC
  2. (nil) # Transaction aborted due to change in 'mykey'

Best Practices

  • Use WATCH judiciously, as it can increase the complexity of your transaction logic.
  • Combine WATCH with appropriate error handling to retry transactions when they are aborted.

Common Mistakes

  • Not handling the nil response from EXEC, which can lead to silent transaction failures.
  • Watching too many keys, which increases the likelihood of transaction aborts.

FAQs

What happens if I issue a UNWATCH after WATCH?

UNWATCH will cancel all previously watched keys, ensuring the next EXEC will not be conditional based on those keys.

Can I use WATCH outside of a MULTI/EXEC block?

Yes, but doing so won’t have any effect since WATCH is designed to work within transactions.