Acreol
Acreol

Reputation: 77

Close redis connection on google cloud function end

How do you close the connection to a redis memory store from a cloud function when the cloud function instance terminates? (I believe to close I need to call redis.quit(), but I just don't know when, and I cannot close them immediately after a function returns because the function instance can be reused)

Because I'm just leaving the connections open, right now I am getting "ECONNRESET" errors.

Alternatively if something like this is not possible:

process.on("exit", function(){//also process is not defined in cloud functions
    redisClient.quit();
});

Is the best option to specify a timeout in the redis config? (How do you do this in gcp memorystore?)

Upvotes: 2

Views: 1805

Answers (1)

John Hanley
John Hanley

Reputation: 81406

When your Cloud Function entry function returns, your container is eligible to be terminated without notice.

You have two choices:

  • Open and close connections at every function invocation
  • Use connection pooling and manage connection errors which goes against the intended usage of the Cloud Functions.

If your functions are keeping the container warm, connection pooling with error handling "might" have benefit at a not-insignificant cost of error handling and testing all possible problems. Cloud Functions apps should be designed to be "stateless". Trying to persist state (connections, data, etc) between invocations in Cloud Functions is not a good strategy.

I would design my system using option #1. Cloud Functions are "lightweight" meaning startup, do the task quickly and shutdown.

Upvotes: 3

Related Questions