> ## Documentation Index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# WATCH

> Watch keys for changes.

Use `WATCH` to mark keys whose modification should cancel the next transaction, which gives you optimistic locking.

If any watched key is changed by another client between the `WATCH` and the [`EXEC`](/docs/redis/commands/transactions/exec), the transaction is not executed and `EXEC` replies with null instead. Nothing is locked in the meantime: other clients keep working normally, and the conflict is detected rather than prevented.

The usual loop is: watch the keys, read them, decide what to write, open [`MULTI`](/docs/redis/commands/transactions/multi), queue the writes, and call `EXEC`, retrying from the top when the reply is null. That is how a read-modify-write cycle stays correct without holding a lock. All watches are cleared by `EXEC` and [`DISCARD`](/docs/redis/commands/transactions/discard).

The raw command is TCP-only. Over HTTP, use the transaction or pipeline API of an Upstash SDK instead of sending this command directly.

## Syntax

```redis theme={"system"}
WATCH <key> [<key> ...]
```

## Arguments

| Argument | Required | Repeatable | Description                        |
| -------- | -------- | ---------- | ---------------------------------- |
| `<key>`  | Yes      | Yes        | Redis key targeted by the command. |

## Important points

* The raw command is TCP-only. For HTTP, use an Upstash SDK transaction API rather than sending this command directly.

## Response

The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.

| Protocol | Reply              |
| -------- | ------------------ |
| RESP2    | Simple string `OK` |
| RESP3    | Simple string `OK` |

<Note>
  Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
</Note>

## Examples

TCP examples use the TLS `REDIS_URL` from the Upstash console. REST examples use `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN`.

<AccordionGroup>
  <Accordion title="Redis CLI" icon="terminal">
    ```bash theme={"system"}
    WATCH balance
    ```
  </Accordion>

  <Accordion title="ioredis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import Redis from "ioredis";

    const client = new Redis(process.env.REDIS_URL!);
    await client.watch("balance");
    const result = await client.multi().set("balance", "100").exec();
    ```
  </Accordion>

  <Accordion title="node-redis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import { createClient } from "redis";

    const client = await createClient({ url: process.env.REDIS_URL }).connect();
    await client.watch("balance");
    const result = await client.multi().set("balance", "100").exec();
    ```
  </Accordion>

  <Accordion title="redis-py" icon="python" iconType="brands">
    ```python theme={"system"}
    import os
    import redis

    client = redis.from_url(os.environ["REDIS_URL"])
    with client.pipeline() as pipe:
        pipe.watch("balance")
        pipe.multi()
        pipe.set("balance", "100")
        result = pipe.execute()
    ```
  </Accordion>

  <Accordion title="go-redis" icon="golang" iconType="brands">
    ```go theme={"system"}
    ctx := context.Background()
    opts, err := redis.ParseURL(os.Getenv("REDIS_URL"))
    if err != nil { panic(err) }
    client := redis.NewClient(opts)

    err = client.Watch(ctx, func(tx *redis.Tx) error {
        _, err := tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
            pipe.Set(ctx, "balance", "100", 0)
            return nil
        })
        return err
    }, "balance")
    if err != nil { panic(err) }
    ```
  </Accordion>

  <Accordion title="jedis" icon="java" iconType="brands">
    ```java theme={"system"}
    import java.net.URI;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.Transaction;

    try (Jedis jedis = new Jedis(new URI(System.getenv("REDIS_URL")))) {
      jedis.watch("balance");
      try (Transaction transaction = jedis.multi()) {
        transaction.set("balance", "100");
        Object result = transaction.exec();
      }
    }
    ```
  </Accordion>

  <Accordion title="redis-rs" icon="rust" iconType="brands">
    ```rust theme={"system"}
    fn main() -> redis::RedisResult<()> {
        let url = std::env::var("REDIS_URL").expect("REDIS_URL is not set");
        let client = redis::Client::open(url)?;
        let mut connection = client.get_connection()?;

        let _: () = redis::transaction(&mut connection, &["balance"], |con, pipe| {
            pipe.set("balance", "100").ignore().query::<Option<()>>(con)
        })?;
        Ok(())
    }
    ```
  </Accordion>
</AccordionGroup>


## Related topics

- [DISCARD](/docs/redis/commands/transactions/discard.md)
- [UNWATCH](/docs/redis/commands/transactions/unwatch.md)
- [EXEC](/docs/redis/commands/transactions/exec.md)
- [Transactions commands](/docs/redis/commands/transactions/overview.md)
- [Live View](/docs/box/overall/browser/live-view.md)
