# EVAL

```json metadata
{
  "schema_version": 2,
  "title": "EVAL",
  "description": "Executes a server-side Lua script.",
  "categories": ["docs","develop","stack","oss","rs","rc","oss","kubernetes","clients"],
  "arguments": [{"display_text":"script","name":"script","type":"string"},{"display_text":"numkeys","name":"numkeys","type":"integer"},{"display_text":"key","key_spec_index":0,"multiple":true,"name":"key","optional":true,"type":"key"},{"display_text":"arg","multiple":true,"name":"arg","optional":true,"type":"string"}],
  "syntax_fmt": "EVAL script numkeys [key [key ...]] [arg [arg ...]]",
  "complexity": "Depends on the script that is executed.",
  "group": "scripting",
  "command_flags": ["noscript","stale","skip_monitor","no_mandatory_keys","movablekeys"],
  "acl_categories": ["@slow","@scripting"],
  "since": "2.6.0",
  "arity": -3,
  "key_specs": [{"RW":true,"access":true,"begin_search":{"spec":{"index":2},"type":"index"},"find_keys":{"spec":{"firstkey":1,"keynumidx":0,"keystep":1},"type":"keynum"},"notes":"We cannot tell how the keys will be used so we assume the worst, RW and UPDATE","update":true}],
  "tableOfContents": {"sections":[{"id":"required-arguments","title":"Required arguments"},{"id":"optional-arguments","title":"Optional arguments"},{"id":"examples","title":"Examples"},{"id":"redis-software-and-redis-cloud-compatibility","title":"Redis Software and Redis Cloud compatibility"},{"id":"return-information","title":"Return information"}]}

,
  "codeExamples": []
}
```

> [!NOTE]
> This command's behavior varies in clustered Redis environments. See the [multi-key operations](https://redis.io/docs/latest/develop/using-commands/multi-key-operations) page for more information.

Executes a server-side [Lua](https:lua.org) script with the embedded Redis [Lua 5.1](https://redis.io/docs/latest/develop/programmability/lua-api) interpreter. The first argument is the script’s source code.

The second argument is the number of input key name arguments, followed by all the keys accessed by the script.
These input key names are made available to the script as the [`KEYS` global runtime variable](https://redis.io/docs/latest/develop/programmability/lua-api#the-keys-global-variable).
Any additional input arguments should not represent names of keys.

**Important:**
to ensure the correct execution of scripts, both in standalone and clustered deployments, all keys that a script accesses must be explicitly provided as input key arguments.
Scripts should only access keys whose names are given as input arguments.
Scripts should never access keys with programmatically-generated names or based on the contents of data structures stored in the database.

**Note:**
in some cases, users will abuse Lua `EVAL` by embedding values in the script instead of providing them as arguments, thus generating a different script on each call to `EVAL`.
These values are added to the Lua interpreter and cached in Redis, consuming a large amount of memory over time.

Starting with Redis 7.4, Redis evicts scripts loaded with `EVAL` or [`EVAL_RO`](https://redis.io/docs/latest/commands/eval_ro) from the script cache when the cache reaches a certain size. Redis evicts the least recently used scripts first. You can view the number of evicted scripts with the `evicted_scripts` field in [`INFO`](https://redis.io/docs/latest/commands/info).

Please refer to the [Redis Programmability](https://redis.io/docs/latest/develop/programmability) and [Introduction to Eval Scripts](https://redis.io/docs/latest/develop/programmability/eval-intro) for more information about Lua scripts.

## Required arguments

<details open><summary><code>script</code></summary>

The Lua script to evaluate.

</details>

<details open><summary><code>numkeys</code></summary>

The number of key names that follow. Arguments after the keys are passed as regular arguments.

</details>

## Optional arguments

<details open><summary><code>key [key ...]</code></summary>

The key names the script accesses, provided to it via the Lua global `KEYS` runtime variable. There must be exactly `numkeys` of them.

</details>

<details open><summary><code>arg [arg ...]</code></summary>

Additional arguments provided to the script via the Lua `ARGV` variable.

</details>

## Examples

The following example will run a script that returns the first argument that it gets.

```
> EVAL "return ARGV[1]" 0 hello
"hello"
```

## Redis Software and Redis Cloud compatibility

| Redis<br />Software | Redis<br />Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> |
|:----------------------|:-----------------|:------|
| <span title="Supported">&#x2705; Standard</span><br /><span title="Supported"><nobr>&#x2705; Active-Active</nobr></span> | <span title="Supported">&#x2705; Standard</span><br /><span title="Supported"><nobr>&#x2705; Active-Active</nobr></span> |  |

## Return information

**RESP2:**

The return value depends on the script that was executed.

**RESP3:**

The return value depends on the script that was executed.



