---
title: Redis
description: Run an authenticated Redis service with persistent data
icon: "database"
---

<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
export REDIS_PASSWORD="$(openssl rand -hex 24)"
```

```powershell Windows
$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
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
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
msb exec redis-demo -- \
  redis-cli -a "$REDIS_PASSWORD" --no-auth-warning SET example ready
```

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

Read it back:

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

```powershell Windows
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
msb exec redis-demo -- \
  redis-cli -a "$REDIS_PASSWORD" --no-auth-warning PING
```

```powershell Windows
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
msb rm -f redis-demo
```

Delete all persisted Redis data:

```sh
msb volume rm redis-data
```

Clear the password from the host shell:

<CodeGroup>
```sh macOS & Linux
unset REDIS_PASSWORD
```

```powershell Windows
Remove-Item Env:REDIS_PASSWORD
```
</CodeGroup>

</Step>
</Steps>
