Back to All Technical Blogs
Serverless
Sep 05, 2025
7 min read

Sub-Millisecond Caching for GCP Cloud Run with Cloud Memorystore for Redis

Sandip Basnet
Sandip Basnet
Senior Software Engineer & SRE

Sub-Millisecond Caching for GCP Cloud Run with Cloud Memorystore for Redis

High-throughput APIs hosted on GCP Cloud Run often encounter database query bottlenecks under heavy traffic spikes. Offloading repetitive read operations to an in-memory caching tier reduces database load and cuts API response times down to sub-milliseconds.

However, because Cloud Run services run outside private VPC networks by default, reaching a private Cloud Memorystore (Redis / Valkey) instance requires configuring Serverless VPC Access.


Architecture Diagram

  [ Client Request ] ──► [ Cloud Run Container ]
                                │
                      (Serverless VPC Connector)
                                │
                ┌───────────────┴───────────────┐
                ▼                               ▼
     [ Cloud Memorystore ]           [ PostgreSQL / Cloud SQL ]
     (Sub-ms Redis Cache)            (Primary Relational DB)

Setting Up Serverless VPC Access via gcloud CLI

  • Create a Serverless VPC Access connector in your GCP VPC network:
  • bashSnippet
    gcloud compute networks vpc-access connectors create redis-vpc-connector \
      --region=us-central1 \
      --subnet-project=my-gcp-project \
      --ip-cidr-range=10.8.0.0/28
  • Deploy your Cloud Run service connected to the VPC connector:
  • bashSnippet
    gcloud run deploy api-service \
      --image=gcr.io/my-project/api-service:latest \
      --region=us-central1 \
      --vpc-connector=redis-vpc-connector \
      --vpc-egress=private-ranges-only \
      --set-env-vars="REDIS_HOST=10.0.0.5,REDIS_PORT=6379"

    Caching Best Practices for Cloud Run

  • Connection Reuse: Initialize the Redis client instance outside the handler function so warm Cloud Run instances reuse established TCP connections.
  • Cache Invalidation & TTL: Enforce explicit Key Expiration (TTL) policies on all cached keys to prevent stale data accumulation.
  • Topic Tags:GCPMemorystoreRedisCloud RunServerless VPCCachingPerformance
    View All