A Google Cloud Function cannot reach Memorystore Redis and shows 127.0.0.1:6379
Error: connect ECONNREFUSED 127.0.0.1:6379Short 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.
Fix it fast
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 REGION2Match what you see, then jump to its fix
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 REGION2Fix
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
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 REGION2Fix
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 SUBNET3Verify
Redeploy and call the function; the counter or PING returns.
curl -s https://YOUR_FUNCTION_URLConfirmed by docs.cloud.google.com
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
Back end
Node.js cannot reach Redis: ECONNREFUSED on port 6379
Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
ECONNREFUSED on 6379 means no Redis server accepted the connection at the address the client used.
4 causes, each with a fixServers
NestJS cannot resolve the host name redis when using Docker Compose
code: 'ENOTFOUND'
The name redis only exists inside the Compose network, as the service name.
3 causes, each with a fixServers
ng serve runs in the container but localhost:4200 does not open
The browser cannot open localhost:4200 although ng serve is running in the container
ng serve listens on localhost by default, which inside a container is the container itself.
3 causes, each with a fixServers
Azure App Service on Linux restarts the container and the site never starts
The container restarts again and again and the site returns an application error
App Service sends a warmup request to the container and restarts it if no HTTP answer comes back in time.
5 causes, each with a fixServers
bcrypt_lib.node: invalid ELF header inside a Docker container
Error: /app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node: invalid ELF header
bcrypt is a compiled addon, so the file in node_modules only runs on the operating system it was installed on.
3 causes, each with a fixServers
AWS CDK cannot find module '@aws-cdk/aws-ec2' (or another @aws-cdk/aws-* package)
Cannot find module '@aws-cdk/aws-ec2'
CDK v2 no longer ships one npm package per service.
3 causes, each with a fix