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

# Redis

> How to connect to a Redis Resource using the Formal Connector

## Requirements

### Networking

Certain ports must be accessible to connect to Formal Resources. You can listen on multiple ports simultaneously for various Resources.

For accessing Redis, you may assign any port except 8080 (Connector's health check port). Ensure your security groups are configured to allow traffic on the designated port.

## Connect to Redis

### redis-cli

To connect using redis-cli, execute the following command:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
redis-cli -h CONNECTOR_HOSTNAME -p PORT -a FORMAL_PASSWORD
```

Make sure to replace `CONNECTOR_HOSTNAME`, `PORT`, and `FORMAL_PASSWORD` with
the right values. To map Formal identities to upstream credentials, see
[Select a default Native User](https://docs.formal.ai/docs/guides/core-concepts/resources/native-users.md#select-a-default-native-user).

> [!NOTE]
>
> You can access your Formal Credentials in the [Control Plane](https://app.formal.ai).

> [!NOTE]
>
> You are free to use any preferred client.

### Upstream authentication

Native Users are optional for Redis. If the Resource has no Native Users, the
Connector connects to Redis without sending `AUTH`. Use this for Redis servers
that allow unauthenticated access.

If the Resource has Native Users, each connection must resolve one. Set a
default Native User, or request one with an `@<label>` suffix. Otherwise, the
Connector rejects the connection.

### Redis Cluster

Formal supports Redis Cluster when clients connect to the Connector over TLS. Cluster nodes often advertise private addresses in redirects and topology replies. The Connector rewrites those addresses to Connector hostnames and routes reconnects to the right node using SNI.

The Resource hostname should be the cluster endpoint, or another stable seed endpoint that returns cluster topology. Any cluster-aware Redis client should work.

> [!WARNING]
>
> `redis-cli -c` cannot be used with Connector-routed Redis Cluster today. It applies one `--sni` value across cluster-mode reconnects, but the Connector routes each node reconnect using that node's rewritten hostname. See the related [GitHub issue](https://github.com/redis/redis/issues/15602) we opened in the Redis repository.

### IAM authentication (Amazon ElastiCache)

Formal can authenticate to Amazon ElastiCache (Valkey 7.2+ or Redis OSS 7+) using AWS IAM. The Connector generates a short-lived IAM authentication token and sends it as the password in the upstream `AUTH` command.

Create an ElastiCache user with IAM authentication. Enter its user ID in the
Native User credential's **Username** field.

Configure the Redis [Native User](https://docs.formal.ai/docs/guides/core-concepts/resources/native-users.md) to use IAM authentication:

* **AWS IAM**: the Connector signs the token with the IAM role of its running environment. That role is the principal and needs the `elasticache:Connect` permission below.
* **AWS IAM Role**: provide a role ARN such as `arn:aws:iam::123456789012:role/formal-redis-role`. The Connector assumes that role before signing, and the assumed role is the principal that needs `elasticache:Connect`.

Grant the `elasticache:Connect` action to the IAM principal for both the cache and the user:

```json theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "elasticache:Connect",
      "Resource": [
        "arn:aws:elasticache:us-east-1:123456789012:serverlesscache:<cache-name>",
        "arn:aws:elasticache:us-east-1:123456789012:user:<user-name>"
      ]
    }
  ]
}
```

The signing region comes from the Connector's AWS configuration, so it must match the cache's region.

> [!NOTE]
>
> ElastiCache requires in-transit encryption (TLS) for IAM authentication.

## Smart Routing

A single Connector port can serve several Redis Resources. The Resource is selected by the TLS server name, as `<resource-name>.<host>`.

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
redis-cli -h RESOURCE_NAME.CONNECTOR_HOSTNAME -p PORT --tls \
  --sni RESOURCE_NAME.CONNECTOR_HOSTNAME \
  --user FORMAL_USERNAME --pass FORMAL_PASSWORD
```

Most Redis client libraries derive the TLS server name from the hostname they connect to. Pointing them at `RESOURCE_NAME.HOSTNAME_OF_CONNECTOR` is therefore enough. redis-cli is an exception, and requires `--sni` to be passed explicitly.

> [!WARNING]
>
> Smart routing for Redis requires TLS, because the Resource name travels in the TLS handshake. It also requires `*.<host>` DNS entries pointing at the Connector.

## Policy Evaluation

Formal supports the following policy evaluation stages for Redis:

* **Session**: Evaluate and enforce policies at connection time
* **Request**: Evaluate and enforce policies at request time before command execution

## End-user Identity Propagation

Formal supports end-user identity propagation for Redis commands. To enable this, you can use the `CLIENT SETNAME` command to set the end-user identity.

```text theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
CLIENT SETNAME formal_enduser=example@example.com
```

This will set the end-user identity to `example@example.com` for the current connection.

The Connector accepts `formal_enduser` only from a machine user. It ignores it for human users, whose end user is always themselves.


## Related topics

- [Smart Routing](https://docs.formal.ai/docs/guides/core-concepts/resources/smart-routing.md)
- [Connector](https://docs.formal.ai/docs/changelog/connector.md)
- [Connector Hostname and TLS](https://docs.formal.ai/docs/guides/core-concepts/connectors/tls.md)
- [Create resource](https://docs.formal.ai/api-reference/corev1resourceservice/create-resource.md)
- [Update resource (full object replacement)](https://docs.formal.ai/api-reference/corev1resourceservice/update-resource-full-object-replacement.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.