# ARRING

> Append values to a fixed-size ring.

Use `ARRING` to append values to an array that wraps around after a fixed number of slots.

The array is confined to indexes `0` through `<size> - 1`. Appends advance the same cursor [`ARINSERT`](/redis/commands/array/arinsert) uses, but wrap back to `0` on reaching the end, so the newest `<size>` values are kept and older ones are overwritten in place. That bounds the memory a stream of writes can consume without any trimming command, which is what makes it a fit for rolling windows such as the last N samples or the last N log lines.

The size is stored with the key and every call restates it. Calling `ARRING` with a different size reshapes the ring: the most recent values that still fit are kept and relaid from index `0`, and anything outside the new window is dropped. The reply is the index the last value was written to; read the window back with [`ARLASTITEMS`](/redis/commands/array/arlastitems), which follows the cursor and returns the values in write order.

See the [array command overview](/redis/commands/array/overview) for the data model these commands share.

## Syntax

```redis
ARRING <key> <size> <value> [<value> ...]
```

## Arguments

| Argument | Required | Repeatable | Description |
| --- | --- | --- | --- |
| `<key>` | Yes | No | Array key targeted by the command. |
| `<size>` | Yes | No | Number of slots in the ring. Must be greater than `0`. |
| `<value>` | Yes | Yes | Value to append. Repeat to append several values in one call. |

## Important points

- The reply is the index the last value was written to, not the number of values written.
- Calling `ARRING` with a size different from the stored one rebuilds the ring, keeping the most recent values that fit in the new size.
- Writing more values than `<size>` in one call leaves only the last `<size>` of them.

## 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 | Integer, or bulk string when the index exceeds the signed 64-bit range |
| RESP3 | Integer, or Big number when the index exceeds the signed 64-bit range |

<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
ARRING my-array 100 hello world
```

</Accordion>

<Accordion title="@upstash/redis" icon="node-js" iconType="brands">

<Note>
  This command is not supported yet in `@upstash/redis`.
</Note>

</Accordion>

<Accordion title="upstash_redis" icon="python" iconType="brands">

<Note>
  This command is not supported yet in `upstash_redis`.
</Note>

</Accordion>

<Accordion title="ioredis" icon="node-js" iconType="brands">

```ts
import Redis from "ioredis";

const redis = new Redis(process.env.REDIS_URL!);
const result = await redis.arring("my-array", 100, "hello", "world");
console.log(result);
```

</Accordion>

<Accordion title="node-redis" icon="node-js" iconType="brands">

```ts
import { createClient } from "redis";

const client = await createClient({ url: process.env.REDIS_URL })
  .on("error", console.error)
  .connect();
const result = await client.arRing("my-array", 100, ["hello", "world"]);
console.log(result);
```

</Accordion>

<Accordion title="redis-py" icon="python" iconType="brands">

```python
import os
import redis

client = redis.from_url(os.environ["REDIS_URL"])
result = client.arring("my-array", 100, "hello", "world")
print(result)
```

</Accordion>

<Accordion title="go-redis" icon="golang" iconType="brands">

```go
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.ARRing(context.Background(), "my-array", 100, "hello", "world").Result()
    if err != nil {
        panic(err)
    }
    fmt.Println(result)
}
```

</Accordion>

<Accordion title="jedis" icon="java" iconType="brands">

```java
import java.net.URI;

import redis.clients.jedis.Jedis;

try (Jedis jedis = new Jedis(new URI(System.getenv("REDIS_URL")))) {
  Object result = jedis.arring("my-array", 100, "hello", "world");
  System.out.println(result);
}
```

</Accordion>

<Accordion title="redis-rs" icon="rust" iconType="brands">

```rust
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("ARRING");
    command.arg("my-array");
    command.arg("100");
    command.arg("hello");
    command.arg("world");
    let result: redis::Value = command.query(&mut connection)?;
    println!("{result:?}");
    Ok(())
}
```

</Accordion>

</AccordionGroup>
