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

# Memorystore

> Connect to Memorystore for Redis and Memorystore for Valkey through bqto, with TLS and AUTH, and to Redis Cluster and Valkey in cluster mode with bqto Pro.

| Service | `--service` | Plan |
| - | - | - |
| Memorystore for Redis, Basic and Standard tiers | `redis` | Free |
| Memorystore for Valkey, cluster mode disabled | `valkey` | Free |
| Memorystore for Redis Cluster | `redis` | bqto Pro |
| Memorystore for Valkey in cluster mode | `valkey` | bqto Pro |

## Connect to an instance

```sh theme={null}
bqto connect --service redis  --project my-project --instance europe-west1/cache
bqto connect --service valkey --project my-project --instance europe-west1/cache
```

`--instance` takes the region and the name, as `REGION/NAME`.

bqto gives you a port forward. The local port is the port of the instance plus 10000, so 6379
becomes 16379. A Redis instance with TLS, on 6378, becomes 16378.

With `-- CMD`, use `redis-cli -h $BQTO_HOST -p $BQTO_PORT`.

## TLS and AUTH

With `-- CMD`, two variables tell your client what the instance needs:

* `BQTO_TLS` is `1` when your client must use TLS, and empty otherwise.
* `BQTO_AUTH` names the credential that the instance takes: `password` (an AUTH string), `iam`
  (an access token), or empty for none.

With TLS, the client needs the CA of the instance in `server-ca.pem`. On its `CA:` line, bqto prints
the gcloud command that writes this file. With `BQTO_AUTH=password`, set `REDISCLI_AUTH` to the
AUTH string of the instance before you start bqto.

Put the client lines in a script, such as `client.sh`:

```sh theme={null}
set --
if [ -n "$BQTO_TLS" ]; then set -- --tls --cacert server-ca.pem; fi
if [ "$BQTO_AUTH" = iam ]; then REDISCLI_AUTH="$(gcloud auth print-access-token)"; export REDISCLI_AUTH; fi
redis-cli -h "$BQTO_HOST" -p "$BQTO_PORT" "$@" PING
```

Then run it through the session:

```sh theme={null}
bqto connect --service redis --project my-project --instance europe-west1/cache -- sh client.sh
```

Pass passwords and tokens through the environment or a private file, never as command-line
arguments.

> [!WARNING]
>
> If transit encryption is off, an access token that you use as a password travels in cleartext
> from the bastion to the instance.

## Clusters

> [!NOTE]
>
> Memorystore for Redis Cluster and Memorystore for Valkey in cluster mode need bqto Pro.

```sh theme={null}
bqto connect --service redis  --project my-project --instance europe-west1/my-redis-cluster
bqto connect --service valkey --project my-project --instance europe-west1/my-valkey-cluster
```

bqto knows when an instance is a cluster. For a cluster, it opens a SOCKS5 proxy instead of a port
forward, because the client follows redirects to other nodes. The ready line prints the client command, such as
`proxychains4 -q redis-cli -c -h HOST -p PORT`, with a `proxychains.conf` whose last line is
`socks5 127.0.0.1 PORT`. `BQTO_REMOTE` holds the address to reach through the proxy.

## What it needs

* The API `redis.googleapis.com` for Redis, or `memorystore.googleapis.com` for Valkey, in the
  service project.
* The role `roles/redis.viewer` for Redis, or `roles/memorystore.viewer` for Valkey, in the service
  project, together with the roles that every session needs. See
  [Permissions](https://docs.bqto.dev/reference/permissions.md).


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