Help understanding edge runtime behaviour in docker container
Unanswered
Giant panda posted this in #help-forum
Giant pandaOP
Hello,
I have just encountered the known problem of serverless functions exhausting Postgres connection pool.
I get why this would happen running on Vercel, but I dont get why this would happen running the app locally or on docker.
My current understanding is that in a Vercel deployment API routes will be deployed in serverless functions/in different containers, i.e in different processes.
Otoh running the app locally or on docker I would expect these API routes to be ran in the same process - I honestly don't know how they could run in a different process.
Why would the serverless /edge runtime affect a docker or a local deployment?
My hunch is that this is not process related, but something else. Any information would be greatly appreciated.
I have just encountered the known problem of serverless functions exhausting Postgres connection pool.
I get why this would happen running on Vercel, but I dont get why this would happen running the app locally or on docker.
My current understanding is that in a Vercel deployment API routes will be deployed in serverless functions/in different containers, i.e in different processes.
Otoh running the app locally or on docker I would expect these API routes to be ran in the same process - I honestly don't know how they could run in a different process.
Why would the serverless /edge runtime affect a docker or a local deployment?
My hunch is that this is not process related, but something else. Any information would be greatly appreciated.
30 Replies
what driver do you use? do you release the connection to the pool?
do you use react cache to dedup the request?
Giant pandaOP
@Ray Yes number of connections grow high and db becomes unresponsive.
Tbc I dont have pgbouncer set yet, but I would expect this not to be a problem/necessary in docker.
Tbc I dont have pgbouncer set yet, but I would expect this not to be a problem/necessary in docker.
I use node postgres (pg-pool)
I am not caching the connection in a global variable for local dev as I have seen done in some places.
do you release the connection to the pool?Yes I do.
Am I right in thinking this should not be a problem?
not sure how you fetch the data on the page but you could try to use react cache to dedup the request
https://react.dev/reference/react/cache#cache-expensive-computation
https://react.dev/reference/react/cache#cache-expensive-computation
Giant pandaOP
I mostly talking about API calls made server side in getServerSideProps.
oh ok so you are using page router?
Giant pandaOP
Yes I am
then I don't think it would get exhausted if you are fetching in
getServerSideProps onlyGiant pandaOP
So what you are saying is that I should not face the problem of API routes running in different serverless functions if deploying the app in a single docker container and that the problem is instantiation/caching ?
how do you start the app? do you use something like pm2?
Giant pandaOP
No it is running on gcp cloud run.
It is started with node as the container will self heal in case of errors
@Ray 1 replica?
Giant pandaOP
Yes
@Ray I thought you were using app router
Giant pandaOP
Sorry got confused. App router is the Next13 new folder structure right? No We did not migrate to it.
are you using this package?
https://www.npmjs.com/package/pg-pool
https://www.npmjs.com/package/pg-pool
and what is the error you got?
Giant pandaOP
I am using
"pg": "^8.11.3", as such:import { Pool, PoolConfig } from 'pg';
const DATABASE_URL = process.env.TT_DEV_DB_URL;
const NODE_ENV = process.env.NODE_ENV;
const createConnectionPool = () => {
let poolOptions: PoolConfig = NODE_ENV === 'production' ? { ssl: { rejectUnauthorized: false } } : {};
poolOptions = {
...poolOptions,
connectionString: DATABASE_URL,
idleTimeoutMillis : 30000,
};We do not get errors. The db queries remain in a stalled state. Restarting app server restores state.
does this issue look like your case?
Giant pandaOP
Looks very similar indeed.
you could try this package i think
https://github.com/porsager/postgres
https://github.com/porsager/postgres
Giant pandaOP
Thanks will try and report back.