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
WATCH key [key ...]
Parameter Explanations
- key: One or more keys to monitor for changes. If any of these keys are modified before the
EXECcommand, the entire transaction will be aborted.
Return Values
- OK: If the keys were successfully watched.
Example:
dragonfly> WATCH mykeyOK
Code Examples
dragonfly> SET mykey "initial"OKdragonfly> WATCH mykeyOKdragonfly> MULTIOKdragonfly> SET mykey "updated"QUEUEDdragonfly> EXEC(nil) # This indicates that the transaction was aborted because another client modified 'mykey' after the WATCH command.
In another session:
dragonfly> SET mykey "changed"OK
Back to the first session:
dragonfly> EXEC(nil) # Transaction aborted due to change in 'mykey'
Best Practices
- Use
WATCHjudiciously, as it can increase the complexity of your transaction logic. - Combine
WATCHwith 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.