swammer
swammer8mo ago

Lambda container reuse

In high traffic environments lambda reuses the same container to serve multiple requests. The lambda adapter appears to create a new Context inside the handler for each request. Is there any way to keep things like database connections and s3 clients in the global scope outside of the handler? We sometimes see lambdas fail due to running out of file descriptors when many connections are spawned from the same container.
4 Replies
santi
santi8mo ago
Also facing the same issue. Did you figure this out @swammer ?
swammer
swammerOP8mo ago
No, I implemented a temporary workaround. I'll keep digging eventually and let you know if I figure out how to fix the root cause
santi
santi8mo ago
Any hint in terms of your workaround?
bill92
bill922mo ago
what high traffic are you experiencing? are you declaring your connections outside the handler?
const db = await db.connect()
// more connections

export async function handler(){
// .. logic omitted

}
const db = await db.connect()
// more connections

export async function handler(){
// .. logic omitted

}