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

# Redis

> Run an authenticated Redis service with persistent data

<Tooltip tip="This example publishes Redis to a port on the computer running the CLI, which is not available on microsandbox cloud."><span className="msb-badge-local">Local-only <Icon icon="circle-info" size={11} /></span></Tooltip>

This example starts Redis 7 with password authentication, a persistent named volume, and a localhost-only forwarded port.

## Run Redis

<Steps>
  <Step title="Start Redis">
    Set a temporary password in the host shell:

    <CodeGroup>
      ```sh macOS & Linux theme={null}
      export REDIS_PASSWORD="$(openssl rand -hex 24)"
      ```

      ```powershell Windows theme={null}
      $bytes = New-Object byte[] 24
      $rng = [Security.Cryptography.RandomNumberGenerator]::Create()
      $rng.GetBytes($bytes)
      $rng.Dispose()
      $env:REDIS_PASSWORD = -join ($bytes | ForEach-Object { $_.ToString('x2') })
      ```
    </CodeGroup>

    Start Redis:

    <CodeGroup>
      ```sh macOS & Linux theme={null}
      msb run -d --name redis-demo --replace \
        --cpus 1 --memory 512M --root-disk 1G \
        -p 127.0.0.1:56379:6379 \
        --mount-named redis-data:/data \
        redis:7-alpine -- redis-server \
          --bind 0.0.0.0 \
          --protected-mode yes \
          --appendonly yes \
          --requirepass "$REDIS_PASSWORD"
      ```

      ```powershell Windows theme={null}
      msb run -d --name redis-demo --replace `
        --cpus 1 --memory 512M --root-disk 1G `
        -p 127.0.0.1:56379:6379 `
        --mount-named redis-data:/data `
        redis:7-alpine -- redis-server `
          --bind 0.0.0.0 `
          --protected-mode yes `
          --appendonly yes `
          --requirepass $env:REDIS_PASSWORD
      ```
    </CodeGroup>
  </Step>

  <Step title="Verify reads and writes">
    Write a test value:

    <CodeGroup>
      ```sh macOS & Linux theme={null}
      msb exec redis-demo -- \
        redis-cli -a "$REDIS_PASSWORD" --no-auth-warning SET example ready
      ```

      ```powershell Windows theme={null}
      msb exec redis-demo -- `
        redis-cli -a $env:REDIS_PASSWORD --no-auth-warning SET example ready
      ```
    </CodeGroup>

    Read it back:

    <CodeGroup>
      ```sh macOS & Linux theme={null}
      msb exec redis-demo -- \
        redis-cli -a "$REDIS_PASSWORD" --no-auth-warning GET example
      ```

      ```powershell Windows theme={null}
      msb exec redis-demo -- `
        redis-cli -a $env:REDIS_PASSWORD --no-auth-warning GET example
      ```
    </CodeGroup>

    Finally, check that the server responds:

    <CodeGroup>
      ```sh macOS & Linux theme={null}
      msb exec redis-demo -- \
        redis-cli -a "$REDIS_PASSWORD" --no-auth-warning PING
      ```

      ```powershell Windows theme={null}
      msb exec redis-demo -- `
        redis-cli -a $env:REDIS_PASSWORD --no-auth-warning PING
      ```
    </CodeGroup>

    The three commands should return `OK`, `ready`, and `PONG`. Host applications connect to `127.0.0.1:56379`.

    <Warning>
      `--requirepass` places the demo password in the guest process arguments. For production, use an ACL file delivered through the [secrets workflow](/sandboxes/secrets) and do not expose the forwarded port beyond loopback.
    </Warning>
  </Step>

  <Step title="Clean up or keep the data">
    Remove the sandbox while retaining the append-only database:

    ```sh theme={null}
    msb rm -f redis-demo
    ```

    Delete all persisted Redis data:

    ```sh theme={null}
    msb volume rm redis-data
    ```

    Clear the password from the host shell:

    <CodeGroup>
      ```sh macOS & Linux theme={null}
      unset REDIS_PASSWORD
      ```

      ```powershell Windows theme={null}
      Remove-Item Env:REDIS_PASSWORD
      ```
    </CodeGroup>
  </Step>
</Steps>
