0
0
GCPcloud~5 mins

Memorystore for Redis caching in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes apps need to quickly remember data to work faster. Memorystore for Redis is a service that stores data in memory so apps can get it fast without waiting for slow databases.
When you want to speed up your website by storing user sessions temporarily.
When your app needs to cache frequent database queries to reduce load.
When you want to share data quickly between different parts of your app.
When you need a fast way to count things like page views or clicks.
When you want a simple, managed service to handle caching without managing servers.
Config File - redis-instance.yaml
redis-instance.yaml
apiVersion: redis.cnrm.cloud.google.com/v1beta1
kind: RedisInstance
metadata:
  name: example-redis
  namespace: default
spec:
  tier: BASIC
  memorySizeGb: 1
  region: us-central1
  redisVersion: REDIS_6_X

This file defines a Redis instance on Google Cloud Memorystore.

  • tier: BASIC means a single node for simple caching.
  • memorySizeGb: sets 1 GB of memory for caching data.
  • region: where the Redis instance runs.
  • redisVersion: specifies the Redis software version.
Commands
This command creates a new Memorystore Redis instance named example-redis with 1 GB memory in the us-central1 region using Redis version 6.x and basic tier.
Terminal
gcloud redis instances create example-redis --size=1 --region=us-central1 --redis-version=redis_6_x --tier=BASIC
Expected OutputExpected
Created [https://redis.googleapis.com/v1/projects/my-project/locations/us-central1/instances/example-redis].
--size - Sets the memory size in GB for the Redis instance.
--region - Specifies the Google Cloud region where the instance will run.
--tier - Chooses the service tier, BASIC is single node.
This command shows details about the example-redis instance to confirm it is running and check its settings.
Terminal
gcloud redis instances describe example-redis --region=us-central1
Expected OutputExpected
name: projects/my-project/locations/us-central1/instances/example-redis memorySizeGb: 1 tier: BASIC redisVersion: REDIS_6_X state: READY host: 10.0.0.5 port: 6379
--region - Specifies the region of the Redis instance to describe.
This command tests the connection to the Redis instance by sending a ping. A response of PONG means it is working.
Terminal
redis-cli -h 10.0.0.5 -p 6379 ping
Expected OutputExpected
PONG
Key Concept

If you remember nothing else from this pattern, remember: Memorystore for Redis stores data in memory to make your app faster by reducing slow database calls.

Common Mistakes
Trying to connect to Redis without using the correct IP address and port from the instance description.
The Redis client won't reach the server and will fail to connect.
Always run 'gcloud redis instances describe' to get the correct host and port before connecting.
Creating a Redis instance in a different region than your app runs.
This causes higher latency and slower caching performance.
Choose the same region for your Redis instance and your app to keep data access fast.
Summary
Use 'gcloud redis instances create' to make a new Redis cache instance.
Check the instance details with 'gcloud redis instances describe' to get connection info.
Test the Redis connection using 'redis-cli ping' to ensure it responds.