I’m using the
pg module directly, and this helped me to shave off exactly 10 seconds of every request
The postgres module is, by default, pooling up connections, and doesn’t let them go until 10 seconds of idle time. Therefor the requests didn’t finish until the pool timed out the connection and closed it.
What’s very interesting is that Amazon themselves encourage this very behaviour:
Take advantage of container re-use to improve the performance of your function. Make sure any externalized configuration or dependencies that your code retrieves are stored and referenced locally after initial execution. Limit the re-initialization of variables/objects on every invocation. Instead use static initialization/constructor, global/static variables and singletons. Keep alive and reuse connections (HTTP, database, etc.) that were established during a previous invocation.