# WATCH

> Watch keys so the next transaction runs only if they did not change.

URL: https://docs.hotkv.com/commands/watch/

```
WATCH key [key ...]
```

- Group: Transactions
- Complexity: O(1) per key
- 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](https://docs.hotkv.com/commands/exec/index.md), 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](https://docs.hotkv.com/commands/discard/index.md) and
[UNWATCH](https://docs.hotkv.com/commands/unwatch/index.md), 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](https://docs.hotkv.com/commands/multi/index.md).

## 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

```
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
```

## Compatibility

Same as Redis 7 and Valkey 8.

## See also

[UNWATCH](https://docs.hotkv.com/commands/unwatch/index.md), [MULTI](https://docs.hotkv.com/commands/multi/index.md),
[EXEC](https://docs.hotkv.com/commands/exec/index.md)