Google Cloud Redis

raw JSON →
2.21.0 verified Tue May 12 auth: no python install: verified quickstart: stale

Google Cloud Redis (also known as Memorystore for Redis) is a fully managed Redis service on Google Cloud Platform. This Python client library allows developers to programmatically create, manage, and interact with Redis instances, supporting operations like instance creation, deletion, and listing. The library is actively maintained by Google and receives frequent updates, typically aligning with broader Google Cloud client library releases.

pip install google-cloud-redis
error ModuleNotFoundError: No module named 'google.cloud.redis_v1'
cause The `google-cloud-redis` Python client library, or its specific API version module `redis_v1`, is not installed or not accessible in the current Python environment.
fix
Ensure the library is installed using pip: pip install google-cloud-redis
error google.api_core.exceptions.PermissionDenied: 403 Permission denied
cause The authenticated Google Cloud identity (user or service account) lacks the necessary IAM permissions to perform the requested operation on the Redis instance or project.
fix
Grant the required IAM roles (e.g., roles/redis.admin, roles/owner) to the service account or user interacting with the Memorystore for Redis API.
error google.api_core.exceptions.InvalidArgument: 400 The IP address range ... is already in use
cause During Redis instance creation, the specified IP address range for private services access conflicts with an existing allocated range in your VPC network, or the network peering is misconfigured.
fix
Choose a different, unallocated IP address range for your Redis instance, ensure the VPC network peering is correctly established, or let Memorystore automatically allocate a range.
error google.api_core.exceptions.NotFound: 404 Not found
cause The Redis instance you are trying to access, update, or delete does not exist or its name/location is incorrect.
fix
Verify the Redis instance ID, project ID, and region/zone are correct, and ensure the instance actually exists in Google Cloud Memorystore for Redis.
gotcha Connecting to a Memorystore for Redis instance from your application typically requires setting up Serverless VPC Access or VPC Network Peering. Direct public IP access is not usually available, and applications must be within the same VPC network or connected via a connector to access the Redis instance. Ensure your application's region aligns with your Redis instance's region for optimal connectivity and performance.
fix Configure Serverless VPC Access for serverless environments (Cloud Functions, App Engine Standard) or VPC Network Peering for VM-based applications. Ensure client applications are deployed in the same region and VPC as the Redis instance, or have appropriate network connectivity configured.
gotcha This library (`google-cloud-redis`) is for *managing* Google Cloud Redis instances (creating, listing, deleting). To *interact with data* in a Redis instance (e.g., `GET`, `SET`, `HSET`), you need a separate Redis client library like `redis-py`.
fix Install `redis` (or `redis-py`) using `pip install redis` to perform data operations. Use the host, port, and optional AUTH string obtained from the `google-cloud-redis` client to connect `redis-py`.
breaking Google Cloud client libraries sometimes introduce breaking changes in major version releases. Always consult the changelog and release notes on the official GitHub repository or Google Cloud documentation when upgrading across major versions to understand API changes, parameter shifts, or behavioral modifications.
fix Review the official release notes and migration guides for the specific major version you are upgrading to. Test your application thoroughly in a staging environment before deploying to production.
gotcha Enabling Redis AUTH for your Memorystore instance provides an authentication token (UUID) for client connections. This enhances security but requires clients to explicitly authenticate. Furthermore, enabling in-transit encryption (TLS) is a critical best practice for securing data communication between your application and the Redis instance.
fix When creating or updating an instance, enable AUTH and transit encryption. Retrieve the AUTH string via `gcloud redis instances get-auth-string` or the client library, and configure your `redis-py` client (or equivalent) to use this password and TLS when connecting. Ensure your client library supports TLS.
gotcha Many Google Cloud operations, particularly instance creation or updates, are long-running operations (LROs). The client library returns an `Operation` object. You must explicitly call `.result()` on this object to wait for the operation to complete and retrieve its final status or result, or handle it asynchronously.
fix Always handle the `Operation` object returned by methods like `create_instance` or `update_instance`. Use `operation.result()` for synchronous waiting or `operation.add_done_callback()` for asynchronous handling to ensure the operation has finished successfully or to catch errors.
breaking The Google Cloud client libraries require proper authentication to make API calls. The `DefaultCredentialsError` indicates that the application could not find Application Default Credentials (ADC). This is a common issue when running applications locally without `gcloud auth application-default login` or in environments (like Docker containers) without service account keys or ADC configured via environment variables (e.g., `GOOGLE_APPLICATION_CREDENTIALS`).
fix Ensure your environment is correctly authenticated. For local development, run `gcloud auth application-default login`. For production environments, use service accounts attached to your compute resources (e.g., GKE, Cloud Run, Compute Engine) or provide service account credentials via the `GOOGLE_APPLICATION_CREDENTIALS` environment variable if running outside managed Google Cloud services.
breaking The `google-cloud-redis` client library (and most Google Cloud client libraries) requires authentication to interact with Google Cloud services. This error indicates that Application Default Credentials (ADC) or other authentication methods were not found, preventing the client from initializing and making API calls.
fix Ensure proper authentication is configured for your environment. For local development, run `gcloud auth application-default login`. For deployed applications, configure a service account, enable Workload Identity, or ensure the underlying compute resource (e.g., GCE VM, Cloud Run, Cloud Functions) has the necessary IAM permissions and scopes.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 1.69s 68.2M
3.10 slim (glibc) - - 0.99s 66M
3.11 alpine (musl) - - 2.42s 72.9M
3.11 slim (glibc) - - 1.74s 71M
3.12 alpine (musl) - - 2.49s 64.3M
3.12 slim (glibc) - - 1.93s 62M
3.13 alpine (musl) - - 2.37s 63.9M
3.13 slim (glibc) - - 1.96s 62M
3.9 alpine (musl) - - 1.54s 68.3M
3.9 slim (glibc) - - 1.19s 66M

This quickstart demonstrates how to initialize the Google Cloud Redis client and list existing Redis instances within a specified Google Cloud project and location. For authentication, ensure `GOOGLE_APPLICATION_CREDENTIALS` is set up (e.g., pointing to a service account key file, or rely on Application Default Credentials).

import os
from google.cloud import redis_v1

# Your Google Cloud Project ID
project_id = os.environ.get('GOOGLE_CLOUD_PROJECT', 'your-project-id')
# The region where you want to list instances, e.g., 'us-central1'
location = 'global' # Or a specific region like 'us-central1'

def list_redis_instances(project_id: str, location: str):
    """Lists all Redis instances in a given project and location."""
    client = redis_v1.CloudRedisClient()
    parent = f"projects/{project_id}/locations/{location}"

    try:
        # The `list_instances` method returns an iterable of Instance objects.
        instances = client.list_instances(parent=parent)

        if not instances:
            print(f"No Redis instances found in {location} for project {project_id}.")
            return

        print(f"Redis instances in {location} for project {project_id}:")
        for instance in instances:
            print(f"  Name: {instance.name}")
            print(f"  Host: {instance.host}")
            print(f"  Port: {instance.port}")
            print(f"  State: {instance.state.name}")
            print(f"  Tier: {instance.tier.name}")
            print(f"  Memory size (GB): {instance.memory_size_gb}")
            print("  ---")

    except Exception as e:
        print(f"Error listing Redis instances: {e}")

if __name__ == "__main__":
    # Set GOOGLE_CLOUD_PROJECT environment variable or replace 'your-project-id'
    # Ensure GOOGLE_APPLICATION_CREDENTIALS is set up for authentication
    list_redis_instances(project_id, location)