Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

A Google Cloud Function cannot reach Memorystore Redis and shows 127.0.0.1:6379

ErrorError: connect ECONNREFUSED 127.0.0.1:6379

Short answer

The address 127.0.0.1:6379 means the Redis client got no host and fell back to localhost, which inside a function is the function itself. Pass the Memorystore IP and port as environment variables, and connect the function to the VPC network the instance is authorised on, because the instance has a private address.

  • Applies to Node.js 22, node-redis 5, Cloud Functions 2nd gen, Memorystore for Redis
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Get the instance's IP address, port and authorised network. Then compare the IP with the host variable your deployed function really has.

    gcloud redis instances describe INSTANCE_ID --region REGION
  2. 2Match what you see, then jump to its fix

    1. 1The error names 127.0.0.1 even though your local .env has the right addressThe Redis host variable is not set in the deployed functionFix
    2. 2The host is right but connections time out or are refusedThe function has no route into the instance's VPC networkFix

What does the error look like?

  • Error: connect ECONNREFUSED 127.0.0.1:6379

Cause 1 of 2

The Redis host variable is not set in the deployed function

1Check

Google's Node.js sample reads REDISHOST and REDISPORT from the environment and falls back to localhost when they are missing. A .env file on your laptop does not travel with the deploy.

gcloud redis instances describe INSTANCE_ID --region REGION

2Fix

Pass the instance's IP address and port when you deploy, and build the client from those values.

# add to your existing deploy command
--set-env-vars REDISHOST=REDIS_IP,REDISPORT=REDIS_PORT

// index.js
const redisClient = createClient({
  socket: { host: process.env.REDISHOST, port: process.env.REDISPORT },
});

3Verify

Log the host the client is built with at start; it shows the Memorystore address, not localhost.

console.log('redis host', process.env.REDISHOST);

Confirmed by docs.cloud.google.com

Did this fix it?

Cause 2 of 2

The function has no route into the instance's VPC network

1Check

The function must have access to the authorised VPC network of the Redis instance. Find that network name from the instance description.

gcloud redis instances describe INSTANCE_ID --region REGION

2Fix

Give the function access to that network. Google recommends Direct VPC egress over connectors; it uses the network and a subnet of /26 or larger. Serverless VPC Access is the other documented route.

# add to your existing deploy command
--network NETWORK --subnet SUBNET

3Verify

Redeploy and call the function; the counter or PING returns.

curl -s https://YOUR_FUNCTION_URL

Confirmed by docs.cloud.google.com

Did this fix it?

How do I stop it coming back?

  • Keep Redis settings as deploy-time variables and fail at start if the host is missing, instead of letting the client default to localhost.
  • Create the Memorystore instance and the function's network access in the same infrastructure change.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.