Make your app fast: cache hot data in memory with Memorystore for Redis, and serve content from edge locations near your users with Cloud CDN — including cache invalidation after a deploy.
Both speed things up by avoiding repeated work, at different layers. Memorystore caches DATA in memory so your app skips slow database hits. Cloud CDN caches CONTENT at Google's edge locations worldwide so users download from nearby. Why together: one cuts backend load, the other cuts distance and latency.
echo "Memorystore = managed in-memory cache (Redis / Memcached)"echo "Cloud CDN = global edge cache, built into the HTTP load balancer"Memorystore is a managed Redis — an extremely fast in-memory store. Why: reading a value from memory takes microseconds vs milliseconds from a database, so caching frequent reads (sessions, leaderboards, computed results) slashes latency and database load.
Create a small 1 GB Basic-tier Redis instance
gcloud redis instances create learn-redis \
--size 1 --region us-central1 --tier basicGet the host and port your app connects to (private IP in your VPC)
gcloud redis instances describe learn-redis --region us-central1 \
--format "value(host, port)"The common way apps use a cache: look in the cache first; on a miss, read the database and store the result with an expiry (TTL). Why: subsequent reads are served from memory until the entry expires. This is logic in your app — these are the Redis commands it runs (from a VM in the same VPC).
Connect with redis-cli to the instance's private IP
redis-cli -h 10.0.0.3 -p 6379Inside redis-cli — cache a value for 300 seconds, then read it back
SET user:100 "Alice" EX 300GET user:100TTL user:100Cloud CDN serves cached content from Google's edge locations, close to users, and is enabled on a backend of your HTTP(S) load balancer. Why: a user in Tokyo gets cached files from a Tokyo edge instead of your origin in the US — far faster, and it shields your origin from load.
Enable Cloud CDN on the backend service from the load-balancing lesson
gcloud compute backend-services update web-backend --global \
--enable-cdnOr enable it on a storage bucket backend (for static sites)
gcloud compute backend-buckets create site-backend \
--gcs-bucket-name learn-uploads-7f3k --enable-cdnA cache mode decides what gets cached: CACHE_ALL_STATIC caches static files automatically; you can also force caching and set how long copies live (TTL). Why: cache static assets aggressively for speed, while letting dynamic, personalized responses pass through uncached.
Cache static content and keep copies for 1 hour by default
gcloud compute backend-services update web-backend --global \
--cache-mode CACHE_ALL_STATIC \
--default-ttl 3600 --max-ttl 86400Because Cloud CDN holds copies, a freshly deployed file can keep serving the old version until its TTL expires. An invalidation drops cached copies now. Why: you run one after every deploy so users get the new build immediately.
Invalidate everything after a deploy (or a specific path)
gcloud compute url-maps invalidate-cdn-cache web-map \
--path "/*"More surgical: just the changed files
gcloud compute url-maps invalidate-cdn-cache web-map \
--path "/index.html"