1

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?)

Acreol
  • 67
  • 8

1 Answers1

3

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.

John Hanley
  • 74,467
  • 6
  • 95
  • 159
  • The docs explicitly promote using connections across invocations as a performance optimisation: https://cloud.google.com/functions/docs/bestpractices/networking I think asking if there is some lifecycle hook or signal to determine that the function instance is being terminated to do clean-up, such as closing connections, is valid. I have the same question... – Wilhelm Uschtrin Jul 19 '22 at 09:54
  • @WilhelmUschtrin - this is a three year old answer. I recommend that you create a new question. Yes, Google docs recommend initializing items that take a long time in global space. However, as with most tasks, you must know what you are doing. Connection management is one of those items. The question is **How do you close the connection to a redis memory store from a cloud function when the cloud function instance terminates?**. Your comment's link does not solve that problem. Since you do not know that a container is being terminated, choice one is the recommended solution. – John Hanley Jul 19 '22 at 20:16
  • I agree, it doesn't. And I was hoping to find something somewhere in the docs, but didn't. And while choice one is technically correct, the overhead of opening and closing connections on each invocation seems like an unacceptable overhead. As this really seems like a shortcoming of Cloud Functions, maybe the solution then is to just use Cloud Run? – Wilhelm Uschtrin Jul 21 '22 at 08:35
  • 1
    @WilhelmUschtrin - The key is to understand how TCP works. If a service (Cloud Functions, Cloud Run, etc) is not providing CPU time to a connection, the connection will error out. Cloud Run supports always on CPU at a higher cost. Connections have a time cost but serverless also has tradeoffs. Use the right service for the job instead of trying to fit the job to the service. – John Hanley Jul 21 '22 at 08:57