Increased duration in queries using Pool on Lambda
I have being playing more with Neon Pool driver and found that on Vercel Serverless Function (Lambda), the queries take significantly longer.
I'm using drizzle for this test and wrote 2 instances to test with.
1) Edge compatible Pool Driver
2) Normal Drizzle Adapter for Lambda
18 Replies
optimistic-goldOP•2y ago
I made 3 API routes for testing
1) A Cloudflare workers route. The average query time is around 45ms
2) An AWS lambda function using the neon pool driver where the average query time is around 350ms
3) An AWS lambda function using the postgres-js adapter for neon, where the average time is 4ms
Note: This isn't a Neon Cloud Databases issue. I did the same test using a Neon, Supabase, Railway and Render databases. All of them follow almost the same response timers
Lambda with postgres-js: 4ms
Cloudflare workers with pool: 45ms
Lambda with pool: 350ms
harsh-harlequin•2y ago
I'd like to replicate these. Is the 4ms an average? Seems wildly low! Best I've seen is maybe 6-9ms from Vercel IAD to us-east-1, but the average is probably in the teens.
What region is the Lambda in and what region is your Neon DB in.
I have a benchmark tool I've been building and here are some results from it. It uses the Neon Serverless Driver in HTTPs mode:
optimistic-goldOP•2y ago
4ms is the average using postgres-js and vercel iad1
also, i'm only measuring the db call, not the whole request
harsh-harlequin•2y ago
Same. What region is your Neon database hosted in?
Can you try measuring across 10 queries perhaps? And setting pool size to a fixed number, like 5? I'm thinking Pool has high initialisation overhead, but will be fast for future queries (assuming the function is "hot")
optimistic-goldOP•2y ago
All databases where host on US East
I will run it and hit you back with the results
Okay, running with 10 queries and the pool set to 5 in the first 2 runs:
Lambda with postgres-js: 8.65ms
Cloudflare workers with pool: 69.34ms
Lambda with pool: 550.43ms
Cloudflare Workers Pool code:
Lambda Pool code.
Lambda Postgres-js code:
harsh-harlequin•2y ago
Oooh, I see your
getDb code creates a pool for every query. That's always going to be slow.
The postgres.js time being so fast is impressive.optimistic-goldOP•2y ago
I thought it was necessary

optimistic-goldOP•2y ago
The example on the website do it

harsh-harlequin•2y ago
Can you share a repo with this code? I can't recreate these wild latency differences I just tested out a next.js application locally, and all drivers are quite similar.
Cold (warmed neon, cold vercel function):
Warm (warmed neon, warmed vercel function):
harsh-harlequin•2y ago
GitHub
Comparing main...performance-test · evanshortiss/vercel-neon-drizzl...
Contribute to evanshortiss/vercel-neon-drizzle-next development by creating an account on GitHub.
harsh-harlequin•2y ago
Here's the code. I think your
getDb function creating a new instance each time is the main issue
Hmm, maybe for Cloudflare this is accurate. For a Lambda it should be fine since those are just a container running Node.js. If you're being very strict about a connection per request, then you might want to take the approach mentioned.
I'll raise it internally.
BTW, we have an article coming out this week about using Cloudflare Hyperdrive. This will probably help.optimistic-goldOP•2y ago
I made some changes to my code mirroring your git repo, it works great for lambda, but for cloudflare, some of the requests get timeout as I mention here #Weird timeout on Cloudflare Workers
I was just trying to make a hybrid driver between both Vercel runtimes
harsh-harlequin•2y ago
Gotcha. I'll need to experiment with Cloudflare then. It does sound like a connection per worker might be necessary on Cloudflare if there's those weird timeouts occurring without any clear reason.
optimistic-goldOP•2y ago
If you need help with the experiment, feel free to tag me or dm me
harsh-harlequin•2y ago
Will do! Thank you!
And thank you for raising these sorts of queries. It's very helpful for us when we need to improve docs and understand interactions with other platforms etc.
harsh-harlequin•2y ago
My initial assumption (without testing yet) is that creating a pool keeps the Cloudflare worker active and consumes connection limits so future requests can't be processed which results in timeouts, e.g this limit is hit https://developers.cloudflare.com/workers/platform/limits/#simultaneous-open-connections
harsh-harlequin•2y ago
Just a guess right now though
optimistic-goldOP•2y ago
I also tried creating the Pool outside and then connect/end inside the request handler but It still throws an error
Error: Cannot use a pool after calling end on the pool