Connect to a Memorystore for Redis Cluster instance

This page explains how to connect to a Memorystore for Redis Cluster instance.

Client support

Memorystore for Redis Cluster does not provide its own client libraries, but rather supports existing third-party client libraries that support the Redis protocol. An example list of clients can be found on the Redis Clients page.

Redis-py client best practice

To connect to your Memorystore for Redis Cluster instance using the redis-py Python client, you must add the skip_full_coverage_check=True when declaring a Redis Cluster:

from rediscluster import RedisCluster
endpoints = [{"host": "IPADDRESS", "port": "6379"}]

rdb = RedisCluster(
    startup_nodes=endpoints,
    skip_full_coverage_check=True, # Required for Memorystore
    decode_responses = True)

print(rdb.set('PYTHON', 'CLUSTER'))
print(rdb.get('PYTHON'))
print(rdb.unlink('PYTHON'))

Allowlist ports on firewall

If you don't allowlist the correct ports on your firewall, your instance can encounter connection errors. For more information about allowlisting ports, see Troubleshoot issues.

Cross region access

You can access a Memorystore for Redis Cluster instance from a client located in a different region than where the Memorystore instance is located.

View your cluster's discovery endpoint

To view your Memorystore cluster's discovery endpoint, use the following instructions:

Console

  1. Go to the Memorystore for Redis Cluster page in the Google Cloud console.

    Memorystore for Redis Cluster

  2. Click your Cluster ID.

  3. In the Connect to this instance section, take a note of the IP address and port number next to Discovery Endpoint. These two values used together are your cluster's discovery endpoint.

gcloud

To view your cluster's discovery endpoint, run the describe gcloud CLI as explained in the view instance details section.

The output should be similar to the following:

authorizationMode: AUTH_MODE_DISABLED
createTime: '2023-08-04T17:49:07.988918793Z'
discoveryEndpoints:
- address: 10.142.0.13
  port: 6379
  pscConfig:
  - network: projects/my-project-364018/global/networks/default
name: projects/my-project-364018/locations/us-east1/clusters/my-cluster-1
pscConnections:
- address: 10.142.0.13
  forwardingRule: https://www.googleapis.com/compute/v1/projects/my-project-364018/regions/us-east1/forwardingRules/sca-auto-fr-5bc46f69-ec80-4c5a-8971-ce267a4d8ddd
  network: projects/my-project-364018/global/networks/default
  projectId: my-project-364018
  pscConnectionId: '25734306048376845'
- address: 10.142.0.10
  forwardingRule: https://www.googleapis.com/compute/v1/projects/my-project-364018/regions/us-east1/.
  forwardingRules/sca-auto-fr-817b64c5-85ac-48d0-82e3-c6fa99b4e67d
  network: projects/my-project-364018/global/networks/default
  projectId: my-project-364018
  pscConnectionId: '25734306048376842'
replicaCount: 0
shardCount: 3
sizeGb: 39
state: ACTIVE
tier: PERFORMANCE
transitEncryptionMode: TRANSIT_ENCRYPTION_MODE_DISABLED
uid: cddda5de-d91f-449b-8755-3a665430271a

Take a note of the endpoint address and port number listed in the discovery_endpoints: section. These two values used together are your cluster's discovery endpoint.

Connect from a Compute Engine VM using redis-cli

You can connect to the Memorystore for Redis Cluster instance from any Compute Engine VM that uses the Memorystore instance's authorized network.

To connect to the instance:

  1. If you don't already have a Linux Compute Engine VM that uses the authorized network for your Memorystore for Redis Cluster instance, create one and connect to it by following Quickstart to create a Linux VM.

  2. Install redis-cli version 6.0 or newer on the Compute Engine VM by following the instructions at Install Redis on Linux.

  3. View your cluster's discovery endpoint and make a note of it.

  4. Connect to your instance's discovery endpoint by running the following command:

    redis-cli -h DISCOVERY_ENDPOINT_ADDRESS -p PORT_NUMBER -c
    

    Where:

    • DISCOVERY_ENDPOINT_ADDRESS and PORT_NUMBER are the values you noted in the previous step.
  5. Run the CLUSTER SHARDS command to view your cluster topology. Take a note of one of the node's IP addresses and port numbers.

  6. Connect to your desired node by running the following command:

    redis-cli -h NODE_IP_ADDRESS -p NODE_PORT -c
    

    Replace the following:

    • NODE_IP_ADDRESS is the node's IP address you found in the previous step.
    • NODE_PORT is the node's port number you found in the previous step.
  7. Once connected to the node, enter some Redis commands:

    Enter:

    PING
    

    Result:

    PONG
    

    Enter

    SET HELLO WORLD
    

    Result:

    OK
    

    Enter:

    GET HELLO
    

    Result:

    "WORLD"
    
  8. Once you are done testing your connection to the Redis node, you should consider deleting the Compute Engine VM you used to connect to the Redis instance. Doing so helps you avoid incurring charges to your Cloud Billing account.

Connect to an in-transit encryption enabled instance

This section provides an example of how to connect to a Memorystore for Redis Cluster instance that has in-transit encryption enabled.

  1. If you don't already have a Compute Engine VM that uses the same authorized network as the Redis instance from the previous step, create one and connect to it by following Quickstart using a Linux VM.

    1. Install redis-cli version 6.0 or newer on the Compute Engine VM by following the instructions at Install Redis on Linux.
  2. Install your instance's certificate authority on the Linux VM by following the instructions at Install certificate authorities on your client.

  3. View your cluster's discovery endpoint and make a note of it.

  4. Connect to your instance's discovery endpoint by running the following command:

    redis-cli -h DISCOVERY_ENDPOINT_ADDRESS -p PORT_NUMBER -c --tls  --cacert /tmp/server_ca.pem
    

    Where:

    • DISCOVERY_ENDPOINT_ADDRESS and PORT_NUMBER are the values you noted in the previous step.
  5. Run the CLUSTER SHARDS command to view your cluster topology. Take a note of one of the node's IP addresses and port numbers.

  6. Connect to your desired node by running the following command:

    redis-cli -h NODE_IP_ADDRESS -p NODE_PORT -c --tls  --cacert /tmp/server_ca.pem
    

    Replace the following:

    • NODE_IP_ADDRESS is the node's IP address you found in the previous step.
    • NODE_PORT is the node's port number you found in the previous step.
  7. Once connected to the node, enter some Redis commands:

    Enter:

    PING
    

    Result:

    PONG