4.3 Cache Invalidation, Capacity, and Failover
Key Takeaways
TTL jitter and bounded cache-fill coordination reduce stampedes.
Memorystore Standard uses asynchronous replication and clients must reconnect after failover.
Cloud Run reaches private caches through a supported VPC egress path.
Cache Invalidation, TTL Strategies & Mitigating Stampedes
Maintaining cache consistency requires careful key lifecycle management.
Time-to-Live (TTL) Expiration
Always assign a TTL to cached records using Redis expiration commands (SET key value EX seconds or EXPIRE key seconds). Without a TTL, abandoned or updated keys consume memory indefinitely until forced out by eviction.
Cache Stampede (Thundering Herd Problem)
A cache stampede occurs when an intensely accessed key expires, causing hundreds of concurrent client threads to simultaneously experience a cache miss and execute identical expensive queries against Cloud SQL, resulting in connection pool exhaustion and database collapse.
+--------------------------------------------------------------------------+
| Cache Stampede Mitigation: Distributed Mutex Locking |
+--------------------------------------------------------------------------+
| |
| 1000 Concurrent Requests arrive for expired key 'catalog:item:42' |
| | |
| v |
| [ Thread 1 ] ---> Acquires Redis Mutex: SET lock:42 NX PX 5000 (Success) |
| | |
| +---> Queries Cloud SQL -> Updates Redis 'catalog:item:42' + TTL |
| | |
| +---> Releases Mutex Lock |
| |
| [ Threads 2-1000 ] -> Mutex Acquisition FAILS |
| | |
| +---> Sleep 50ms & Retry Cache OR Return Stale Value Gracefully |
+--------------------------------------------------------------------------+
Stampede Defenses
- Randomized TTL Jitter: Add random variance to key expiration windows to prevent synchronized mass expiration:
- Cache-Fill Coordination with Expiring Locks: When a cache miss occurs, the worker attempts to acquire an atomic distributed lock in Redis (
SET lock:product:101 random_token NX PX 5000). Only a successful lock holder should refill the entry; release with a compare-and-delete check of the unique token so an expired holder cannot delete its successor's lock. Bound retries and tolerate duplicate cache fills. Such a lock is not a durable transaction or payment deduplication record.
Redis Maxmemory Eviction Policies
When a Memorystore for Redis instance reaches 100% of its provisioned memory capacity (maxmemory), its configured eviction policy dictates whether incoming writes succeed or fail.
| Eviction Policy | Mechanics & Use Case |
|---|---|
allkeys-lru | Recommended for pure caches. Evicts the Least Recently Used (LRU) keys out of the entire keyspace, regardless of whether a TTL is set. Provides eviction under memory pressure; writes can still fail for other resource or operational reasons. |
volatile-lru | Evicts the Least Recently Used keys only among keys that have an explicit expiration (TTL) configured. Keys without TTL are preserved. Ideal when mixing ephemeral cache with persistent session data. |
allkeys-lfu | Evicts the Least Frequently Used (LFU) keys across all keys. Tracks access frequency counters; protects popular items that haven't been accessed recently. |
volatile-ttl | Evicts keys with the shortest remaining TTL first. |
noeviction | No automatic eviction. Does not evict any data. When memory is full, all incoming write commands (SET, HSET, LPUSH) return out-of-memory (OOM) errors, crashing application write workflows. |
Serverless Connectivity: VPC Access Connectors vs. Direct VPC Egress
Memorystore instances are provisioned exclusively with private RFC 1918 IP addresses inside your Virtual Private Cloud (VPC). They have no public IP addresses and cannot be reached over the public internet.
Because serverless runtimes (Cloud Run, Cloud Functions, App Engine standard) operate inside Google-managed multitenant networks, they cannot communicate with private VPC IPs out of the box.
+--------------------------------------------------------------------------+
| Serverless to Private Memorystore Networking |
+--------------------------------------------------------------------------+
| |
| [ Cloud Run Service (Serverless Compute) ] |
| | |
| +--- Option A: Direct VPC Egress (Recommended - High Throughput) |
| | Attaches container network interface directly |
| | |
| +--- Option B: Serverless VPC Access Connector |
| Routes packets via dedicated /28 connector VMs |
| | |
| v |
| [ Customer VPC Network: 10.0.0.0/16 ] |
| | |
| v |
| [ Memorystore for Redis Standard Tier HA (Private IP: 10.0.0.4:6379) ] |
+--------------------------------------------------------------------------+
Option A: Direct VPC Egress (Recommended for Modern Cloud Run)
- Architecture: Cloud Run allocates network interfaces directly into your VPC subnet without requiring intermediary connector VMs.
- Benefits: Delivers lower latency, higher throughput (within Cloud Run instance and network throughput limits), and zero additional VM compute charges.
- Configuration CLI:
gcloud run services update catalog-service \ --network=custom-vpc \ --subnet=serverless-subnet \ --vpc-egress=private-ranges-only
Option B: Serverless VPC Access Connector
- Architecture: Provisions managed e2-micro or f1-micro proxy VMs within a dedicated, unallocated
/28subnet in your VPC. - Throughput Limits: Scalable between 2 and 10 connector instances; total bandwidth is constrained by connector VM capacity.
High Availability, Replication & Automated Failover
Google Cloud offers two tiers for Memorystore for Redis:
Basic Tier
- Topology: Single standalone Redis node in a single zone.
- High Availability: None. No replication, no SLA. If the underlying host fails, memory is reinitialized and data is completely wiped.
Standard Tier (High Availability)
- Topology: Provisions a primary node in one zone and a cross-zone standby replica in a second zone within the same region.
- Replication: Asynchronous replication keeps the standby replica synchronized with the primary.
- Automated Failover: If the primary node fails or undergoes maintenance, Google Cloud detects the health probe failure and promotes the standby replica to primary after failure detection; failover commonly takes around 30 seconds and is not a fixed deadline.
- IP Endpoint Preservation: The connection IP address and port (
10.x.x.x:6379) automatically repoint to the promoted node. Application containers do not need to re-resolve DNS or alter connection strings.
Official references checked October 18, 2026: Google Cloud 1.
A retail microservice deployed on Cloud Run frequently reads product catalog records from Cloud SQL. To minimize database load, the development team implements a Cache-Aside pattern using Memorystore for Redis. During holiday flash sales, when hundreds of cache keys expire simultaneously, thousands of concurrent requests bypass the cache and hit Cloud SQL directly, causing database CPU saturation and connection timeouts. What architectural strategy should the developer implement to mitigate this cache stampede?
Switch the Redis eviction policy from allkeys-lru to noeviction.
Replace Memorystore for Redis with Memorystore for Memcached.
Disable TTL on all catalog keys and rely exclusively on manual cache invalidation.
Add randomized jitter to TTL expirations and use distributed locks or mutexes around database queries on cache misses.
A developer is configuring a Cloud Run microservice to query a private Memorystore for Redis instance in the same Google Cloud region. The Cloud Run service fails to establish a TCP connection to the Redis private IP (10.0.0.4:6379), throwing connection timeout errors. What is the root cause and the required resolution?
Cloud Run services run outside the VPC network by default; the developer must configure Direct VPC Egress or a Serverless VPC Access connector to route traffic into the VPC.
Memorystore requires an external public IP and an SSL/TLS forwarding rule to accept connections from serverless runtimes.
The Cloud Run service must be deployed in Standard Network Tier rather than Premium Network Tier to communicate with private RFC 1918 IPs.
Redis requires an API Gateway proxy to translate HTTP requests from Cloud Run into Redis RESP socket protocol frames.
Sections you finish are checked off in the contents.