How to use MemoryStore with Google Cloud Run?

Google Cloud Run And Google Memory Store

How to use MemoryStore with Google Cloud Run?


Whenever we create a Memorystore instance, it will be given a private IP address that will be visible to a single VPC network. Further, Memorystore only permits connections from resources that are contained within the same region. This provides immediate access to Memorystore from Compute Engines in the same region/VPC using VPC peering:
Google’s Memorystore service provides a fully managed Redis environment. Cloud Run provides a fully managed container hosting environment with automatic scaling. This opens up the opportunity to design solutions hosted by Cloud Run that leverage the services of Memorystore. In this article we will examine what is involved to set that up.


To access Memorystore from Cloud Run requires some additional steps. This is because Cloud Run is a managed serverless environment and isn’t associated with any specific VPCs such as the VPC with which Memorystore is peered. The solution to this is to use serverless VPC access.
We create a Serverless VPC Access connector in the same region as both Cloud Run and Memorstore and associated with the same VPC as Memorystore. With this in place, we can then instruct Cloud Run to leverage that connector to access Memorystore.
Let us now work through a sample that illustrates this story in action. First, we create an instance of Memorystore:

gcloud redis instances create redis1 --region us-central1

The instance will be called “redis1” and exist in the “us-central1” region. This command will take several minutes to complete. When the instance has been created, we will want to make note of the IP address that was allocated for us.

gcloud redis instances describe redis1 --region us-central1

Within the information presented, we will find an entry with the label “host”. Make note of the IP address as this is what we will use to connect with the Redis instance. Now we can create the VPC access connector:

gcloud compute networks vpc-access connectors \
  create my-vpc-connector \
  --network default \
  --region us-central1 \

This will create an access connector called “my-vpc-connector” associated with the default VPC network and the us-central1 region. The IP addresses for the connector will be taken from the CIDR range
We can now create a Cloud Run hosted application that will leverage Redis. Here is an example:

const express = require('express');
const cors = require('cors')
const redis = require("redis");

const app = express();

const client = redis.createClient({
  "host": process.env.REDIS_IP
client.on("error", function(error) {
app.get('/', (req, res) => {
  res.set('Cache-Control', 'no-store');
  client.set("key", "value!", redis.print);
  client.get("key", (err, reply) => {

Connecting to Redis at: ${process.env.REDIS_IP}
Value of key just read: ${reply}%MINIFYHTML6bd8ad043bd5b455b65d4bbd66e2ee808%
const port = process.env.PORT || 8080;
app.listen(port, () => {
  console.log(`Listening on port ${port}`);

Notice the use of an environment variable called REDIS_IP that is expected to be provided. This will contain the IP address of the instance of Memorystore to be used.
We then build a container image (not shown). Once the container image has been built, we can deploy to Cloud Run:

gcloud run deploy cloud-run-app \
  --image[PROJECT_ID]/cloud-run-app \
  --max-instances 1 \
  --platform managed \
  --region us-central1 \
  --vpc-connector my-vpc-connector \
  --allow-unauthenticated \
  --set-env-vars "REDIS_IP=[IP_ADDRESS]"

Note that we set the environment variable called REDIS_IP to be the IP address of the Redis instance. Note also that we supply the VPC connector we created earlier to provide the linkage between Cloud Run and Memorystore.

Google Cloud Exam Dumps:
Source: Medium

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *