Samuel Lock (Serverless Sam)
1 min readNov 30, 2022

--

The way lambda work under the hood is to initialize many low spec containers. After a period of idle activity, a container will terminate. However whilst this container is online, it may take on one or many separate lambda invocations, but they are never concurrent. Concurrent Lambda's will always be executed in separate containers.

So this is why it is a common pattern to open a DB connection outside the handler function, so it is done *once* during the containers initialisation and not during every single invocation. Sparing the handler function of having to run and bill unnecessary code.

This isn't a perfect pattern as it's common to exhaust connection pools to DBs in serverless architectures. That's why the RDS Proxy service was developed.

So without any code changes and simply enabling StartSnap:

- Lambda container initializes, creating a connection to the DB via the connection pool
- Snapshots the container, including memory
- Some time passes and a lambda invocation triggers a new container to spin up from the snapshot.
- Either the connection has been terminated or (maybe worse?) it's already in use by another lambda container instance.

--

--

No responses yet