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

# EVALSHA_RO

> Run a cached script that does not write.

Use `EVALSHA_RO` to run a script cached by its SHA1 digest. The script may not write to the database.

It is the read-only form of [`EVALSHA`](/docs/redis/commands/scripting/evalsha): any write command called from the script fails, which lets the server run it on replicas. As with `EVALSHA`, a digest that is not in the cache produces a `NOSCRIPT` error, and the caller is expected to fall back to [`EVAL_RO`](/docs/redis/commands/scripting/eval-ro) with the script body.

Read-only scripts take the global lock like any other script unless the cached body's shebang sets the `allow-key-locking` flag, for example `#!lua flags=no-writes,allow-key-locking`. With the flag, the call takes shared read locks on the keys passed in `KEYS`. See [Key-Based Locking](/docs/redis/features/key-locking).

## Syntax

```redis theme={"system"}
EVALSHA_RO <sha1> <numkeys> [<key> [<key> ...]] [<arg> [<arg> ...]]
```

## Arguments

| Argument    | Required | Repeatable | Description                                             |
| ----------- | -------- | ---------- | ------------------------------------------------------- |
| `<sha1>`    | Yes      | No         | SHA1 digest of a script cached with `SCRIPT LOAD`.      |
| `<numkeys>` | Yes      | No         | Number of key arguments that follow.                    |
| `<key>`     | No       | Yes        | Redis key targeted by the command.                      |
| `<arg>`     | No       | Yes        | Additional argument, available to the script as `ARGV`. |

## Important points

* `numkeys` must equal the number of key arguments that immediately follow it; remaining arguments are available to the script or function as ordinary arguments.
* A read-only script still takes the global lock unless the cached body's shebang sets the `allow-key-locking` flag. See [Key-Based Locking](/docs/redis/features/key-locking).
* Pass every key the script reads through `KEYS` whether or not `allow-key-locking` is set. A key built inside the script is read from disk under the lock when it is not in memory, and it is rejected outright when the flag is set. See [Dynamic Keys and Latency](/docs/redis/features/key-locking#dynamic-keys-and-latency).

## 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    | Reply produced by the cached read-only script |
| RESP3    | Reply produced by the cached read-only script |

<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"}
    EVALSHA_RO fb67a0c03b48ddbf8b4c9b011e779563bdbc28cb 0 value
    ```
  </Accordion>

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

    const redis = Redis.fromEnv();

    const result = await redis.evalshaRo("fb67a0c03b48ddbf8b4c9b011e779563bdbc28cb", [], ["hello"]);
    console.log(result) // "hello"
    ```
  </Accordion>

  <Accordion title="upstash_redis" icon="python" iconType="brands">
    ```python theme={"system"}
    from upstash_redis import Redis

    redis = Redis.from_env()
    result = redis.evalsha_ro("<sha1>", args=["value"])
    print(result)
    ```
  </Accordion>

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

    const redis = new Redis(process.env.REDIS_URL!);
    const result = await redis.evalsha_ro("<sha1>", "0", "value");
    console.log(result);
    ```
  </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 })
      .on("error", console.error)
      .connect();
    const result = await client.evalShaRo("<sha1>", { arguments: ["value"] });
    console.log(result);
    ```
  </Accordion>

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

    client = redis.from_url(os.environ["REDIS_URL"])
    result = client.evalsha_ro("<sha1>", 0, "value")
    print(result)
    ```
  </Accordion>

  <Accordion title="go-redis" icon="golang" iconType="brands">
    ```go theme={"system"}
    package main

    import (
        "context"
        "fmt"
        "os"

        "github.com/redis/go-redis/v9"
    )

    func main() {
        opts, err := redis.ParseURL(os.Getenv("REDIS_URL"))
        if err != nil {
            panic(err)
        }
        client := redis.NewClient(opts)
        result, err := client.EvalShaRO(context.Background(), "<sha1>", nil, "value").Result()
        if err != nil {
            panic(err)
        }
        fmt.Println(result)
    }
    ```
  </Accordion>

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

    import redis.clients.jedis.Jedis;

    try (Jedis jedis = new Jedis(new URI(System.getenv("REDIS_URL")))) {
      Object result = jedis.evalshaReadonly("<sha1>", java.util.List.of(), java.util.List.of("value"));
      System.out.println(result);
    }
    ```
  </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 mut command = redis::cmd("EVALSHA_RO");
        command.arg("<sha1>");
        command.arg("1");
        let result: redis::Value = command.query(&mut connection)?;
        println!("{result:?}");
        Ok(())
    }
    ```
  </Accordion>
</AccordionGroup>


## Related topics

- [EVALSHA_RO](/docs/redis/sdks/py/commands/scripts/evalsha_ro.md)
- [Changelog](/docs/redis/overall/changelog.md)
- [Key-Based Locking](/docs/redis/features/key-locking.md)
- [Overview](/docs/redis/sdks/ts/commands/overview.md)
