Google Cloud Redis
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.
Warnings
- 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.
- 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`.
- 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.
- 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.
- 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.
Install
-
pip install google-cloud-redis
Imports
- CloudRedisClient
from google.cloud import redis_v1
Quickstart
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)