Google Cloud — Memorystore (Redis & Memcached)
Guide to Google Cloud Memorystore covering managed Redis and Memcached, high availability configuration, persistence options, VPC peering, and scaling best practices..
20+ years shipping production infrastructure and CI/CD at scale. Written from production experience, not tutorials.
- ✓Google Cloud account with billing enabled, gcloud CLI installed and configured (version 400+), basic knowledge of Redis or Memcached commands, familiarity with VPC networking and IAM, a Compute Engine VM or GKE cluster for testing connectivity.
Memorystore is like having a specialized tool that handles the heavy lifting so you don't have to build and manage the underlying infrastructure yourself — it just works with Google Cloud.
Memorystore provides fully managed in-memory data stores for Redis and Memcached on Google Cloud. It eliminates the operational burden of self-managing caching clusters while offering high availability, automatic failover, and integrated security. Choosing between Redis and Memcached depends on your data structure needs, persistence requirements, and throughput demands.
Why Memorystore Exists: The Cache Layer Problem
In production, your database is the bottleneck. Every query that hits PostgreSQL or MySQL adds latency and contention. The solution is a cache layer — but running your own Redis or Memcached cluster is a nightmare. You need to handle failover, patching, monitoring, and scaling. Google Cloud Memorystore removes that operational burden. It provides managed Redis and Memcached instances with automatic replication, failover, and maintenance. The trade-off? You lose some control over configuration and pay a premium. For most teams, that's a win. Memorystore integrates natively with Compute Engine, GKE, and Cloud Run via the VPC, keeping latency sub-millisecond. It's not just a cache; it's a session store, rate limiter backend, and pub/sub broker. But don't treat it as a database — it's ephemeral by design. Data persists only if you enable persistence (AOF or RDB), and even then, it's not a replacement for durable storage. Use it for hot data that can be regenerated.
Choosing Between Redis and Memcached
Redis and Memcached are both in-memory data stores, but they serve different purposes. Memcached is a simple key-value cache with no persistence, no replication, and no data structures beyond strings. It's fast and simple — ideal for caching database query results or API responses where data loss is acceptable. Redis, on the other hand, is a feature-rich data structure server. It supports lists, sets, sorted sets, hashes, streams, and pub/sub. It can persist data to disk, replicate across nodes, and run Lua scripts. For most production workloads, Redis is the better choice. Use Memcached only if you need pure caching with minimal overhead and can tolerate data loss. Memorystore offers both, but Redis has more configuration options (persistence, replication, maintenance windows). Memcached is simpler but less flexible. If you're unsure, start with Redis. The cost difference is negligible for small instances, and Redis's features will save you from building custom solutions later.
Network Architecture: VPC Peering and Private IP
Memorystore instances are not publicly accessible. They live inside a Google-managed VPC and connect to your resources via VPC peering or direct peering. When you create an instance, you specify a network (default or custom). Memorystore then provisions a private IP address in that network's subnet. Your Compute Engine VMs, GKE pods, and Cloud Run services can reach it if they are in the same VPC or use Serverless VPC Access. The latency is typically under 1ms within the same region. For cross-region access, you need VPC peering or Cloud VPN, but latency increases. Never expose Memorystore to the internet — use a proxy or Cloud NAT if absolutely necessary. Also, note that Memorystore uses a /29 subnet from your VPC's IP range. Plan your IP space accordingly to avoid conflicts. If you use Shared VPC, ensure the host project has sufficient IP space.
Configuring Persistence: AOF vs RDB
By default, Memorystore Redis instances are ephemeral — data is lost on failover or restart. To persist data, enable AOF (Append-Only File) or RDB (Redis Database Backup). AOF logs every write operation, providing near-real-time durability. RDB takes periodic snapshots. AOF is safer but uses more memory and disk I/O. RDB is lighter but can lose data between snapshots. In Memorystore, you can enable persistence during creation or update. For production, use AOF with fsync every second. This gives you at most 1 second of data loss. RDB is acceptable for caching where data can be regenerated. Note that persistence adds latency — test your workload. Also, Memorystore stores persistence data in Google Cloud Storage, not on the instance. This means failover is fast because the new primary reads from GCS. But it also means you pay for storage egress if you export backups.
Scaling Memorystore: Vertical and Horizontal
Memorystore supports vertical scaling (changing instance size) and horizontal scaling (adding replicas). Vertical scaling is straightforward — update the size parameter. The instance restarts with a brief downtime (seconds to minutes depending on size). For zero-downtime scaling, use a blue-green deployment: create a new instance, migrate data, switch traffic. Horizontal scaling with replicas improves read throughput and provides failover. In Standard tier, you get one primary and one replica. You can add more replicas (up to 5) for read-heavy workloads. Writes still go to the primary. For Redis, you can also use sharding with Cluster mode (Memorystore for Redis Cluster). This partitions data across multiple shards, each with a primary and replicas. Cluster mode is complex but necessary for datasets larger than 300GB or write throughput beyond a single instance. Plan carefully — cluster mode requires application changes (Redis Cluster client).
Monitoring and Alerting: The Metrics That Matter
Memorystore exposes metrics via Cloud Monitoring. Key metrics: CPU utilization, memory usage, keyspace hits/misses, connected clients, replication lag, and evictions. Set alerts on CPU > 80%, memory > 80%, evictions > 0, and replication lag > 10 seconds. Evictions mean your instance is too small — increase size or optimize TTLs. High replication lag indicates network issues or overloaded primary. Also monitor redis_connected_clients — if it spikes, you may have connection leaks. Use the redis-cli INFO command for deeper diagnostics. For Memcached, monitor curr_items, evictions, and get_hits. Set up a dashboard with these metrics. Don't forget to monitor the instance's health — Memorystore automatically fails over if the primary becomes unhealthy, but you should know when it happens.
Backup and Restore: Exporting and Importing Data
Memorystore allows you to export Redis data to a GCS bucket (RDB format) and import from GCS. This is useful for backups, migration, or seeding a new instance. Exports are manual or scheduled via Cloud Scheduler + Cloud Functions. There's no built-in scheduled backup — you must build it. The export process creates an RDB file in the bucket. Importing loads that file into a new or existing instance. Note: import overwrites all existing data. For incremental backups, you need to use AOF persistence and export the AOF file, but Memorystore doesn't support AOF export directly. Instead, use Redis replication to a secondary instance and export from there. Also, exports consume CPU and memory — do them during low traffic. Test your restore process regularly. We've seen teams assume backups work only to find corrupted files during a disaster.
Security: IAM, VPC, and Encryption
Memorystore security starts with IAM. Use roles like redis.editor and redis.viewer to control access to instance management. For data access, Memorystore supports Redis AUTH (password) — always enable it. The password is set at instance creation and can be rotated. Also, use VPC firewall rules to restrict traffic to only your application VMs. Memorystore does not support customer-managed encryption keys (CMEK) for data at rest — it uses Google-managed encryption. For data in transit, TLS is not supported natively for Redis; you must use a proxy like Envoy or HAProxy to add TLS. Memcached doesn't support TLS either. If you need encryption in transit, consider using Cloud VPN or VPC peering with private IPs, which are not exposed to the internet. Also, enable VPC Service Controls to prevent data exfiltration.
High Availability and Disaster Recovery
Memorystore Standard tier provides automatic failover within the same region across zones. If the primary fails, a replica is promoted. This takes seconds. For disaster recovery across regions, you need to set up cross-region replication manually. Memorystore does not support native cross-region replication. Use Redis replication with a secondary instance in another region, or use a multi-region deployment with application-level routing. For Memcached, there is no replication — data loss is expected. For Redis, you can configure a replica in another region using the gcloud redis instances create with --replica-of flag. This creates an async replication link. The replica is read-only. If the primary region goes down, you can promote the replica by detaching it. This is a manual process. Also, consider using Memorystore for Redis Cluster with multi-region sharding, but that's complex. Simpler: use a global cache with a single primary and read replicas in other regions.
Cost Optimization: Rightsizing and Reserved Instances
Memorystore pricing is based on instance size, tier, and region. Standard tier costs roughly 2x Basic tier. To optimize costs, start with the smallest instance that meets your performance needs. Monitor memory usage and evictions — if memory usage is below 50%, consider downsizing. Use reserved instances (1 or 3 year commitments) for predictable workloads to save up to 40%. For bursty workloads, use Basic tier with auto-scaling (not natively supported — you need to script it). Also, consider using a single larger Redis instance with multiple databases (select DB index) instead of many small instances. This reduces overhead and cost. For Memcached, you can use a single large instance as well. Avoid over-provisioning — Memorystore instances are billed per hour, so idle instances waste money. Use labels to track costs per team or environment.
Troubleshooting Common Issues
Common Memorystore issues include connection timeouts, high latency, and out-of-memory errors. Connection timeouts often stem from VPC firewall rules blocking port 6379/11211. Check firewall rules and ensure the client's network has VPC peering. High latency can be due to network congestion, large payloads, or slow commands (e.g., KEYS, SMEMBERS). Use the --latency flag in redis-cli to measure. Out-of-memory errors trigger evictions or OOM kills. Monitor used_memory and set maxmemory-policy to allkeys-lru to avoid crashes. Another issue: replication lag. Check master_repl_offset and slave_repl_offset. If lag grows, the replica may be overloaded or network is slow. For Memcached, common issues are connection limits (default 1024) and slab calcification. Increase -c for connections and adjust slab sizes. Always check logs in Cloud Logging for error messages.
Migration Strategies: Moving to Memorystore
Migrating an existing Redis or Memcached workload to Memorystore requires planning. For Redis, the simplest method is to set up replication from your existing instance to Memorystore. Use the SLAVEOF command or configure Memorystore as a replica of your existing instance (if it's accessible). Once replication is caught up, promote Memorystore to primary. For offline migration, export your data as RDB and import into Memorystore. This requires downtime. For Memcached, there's no replication — you must drain the cache and repopulate. Use a dual-write strategy: write to both old and new caches, then switch reads. For large datasets, use a blue-green deployment: create a new Memorystore instance, migrate traffic gradually. Always test with a subset of traffic first. Rollback plan: keep the old instance running for a few days.
| File | Command / Code | Purpose |
|---|---|---|
| create-redis-instance.sh | gcloud redis instances create my-cache \ | Why Memorystore Exists |
| redis_vs_memcached.py | r = redis.Redis(host='10.0.0.3', port=6379, decode_responses=True) | Choosing Between Redis and Memcached |
| check-peering.sh | gcloud redis instances describe my-cache --region=us-central1 \ | Network Architecture |
| enable-persistence.sh | gcloud redis instances update my-cache \ | Configuring Persistence |
| scale-instance.sh | gcloud redis instances update my-cache \ | Scaling Memorystore |
| monitor-redis.sh | redis-cli -h 10.0.0.3 INFO stats | grep -E 'keyspace_hits|keyspace_misses|evicte... | Monitoring and Alerting |
| export-import.sh | gcloud redis instances export my-cache \ | Backup and Restore |
| enable-auth.sh | gcloud redis instances create my-cache \ | Security |
| cross-region-replica.sh | gcloud redis instances create my-cache-replica \ | High Availability and Disaster Recovery |
| list-instances-cost.sh | gcloud redis instances list --region=us-central1 \ | Cost Optimization |
| troubleshoot-latency.sh | redis-cli -h 10.0.0.3 --latency -a 'password' | Troubleshooting Common Issues |
| migrate-replication.sh | redis-cli -h | Migration Strategies |
Key takeaways
Common mistakes to avoid
3 patternsNot understanding memorystore pricing model
Using default settings without tuning for memorystore
Missing IAM permissions and service account configuration
Interview Questions on This Topic
What is Memorystore and when would you use it?
Frequently Asked Questions
20+ years shipping production infrastructure and CI/CD at scale. Written from production experience, not tutorials.
That's Google Cloud. Mark it forged?
6 min read · try the examples if you haven't