Last week I launched Gumcrm, and I wanted to share a situation I had.
My app syncs Gumroad sales and contacts. Since I'm using my SaaS boilerplate, every sale row needs a lot of Database queries:
entity.contacts.createevents.rows.created webhook event(churned, active....)entity.contacts.tags if didn't exist (for filtering/reporting...)18 database calls for each sale. I have 1,396 gumroad "sales" (some are $0), that's 1,396 x 18 = 25,128 DB calls.
On local development (M1 + postgres.app), it took 15 seconds to insert those 1k+ sales + contacts + companies.
On "launch day" (last Friday), I was ready to test in production (Supabase), but the first problem I encountered was:
Error: Timed out fetching a new connection from the pool. Please consider reducing the number of requests or increasing the `connection_limit` parameter (https://www.prisma.io/docs/concepts/components/prisma-client/connection-management#connection-pool). Current limit: 10.
I thought doing what the error message said would fix it:
connection_limit to 0, 1, 2, 5, 10, 20...pool_timeout to 0, 1, 2, 5, 10, 20...But this led to the next problem.
It seemed like I "fixed" the problem... connection pool now waited for all incoming DB calls. But now I got a Vercel timeout error:
504 Error 'FUNCTION_INVOCATION_TIMEOUT'.
After a bit of research, I remembered that Vercel's Hobby plans allow for 10-second functions and Pro plans give 60-second functions, but my function lasted more than that, and +60s-functions is only for Enterprise customers.
I tried everything:
logs, webhook calls, company inserts...Prisma.createMany function (does not return created IDs)connection_limit and pool_timeout
I was really worried that my boilerplate was not production-ready, but then I thought of something in the shower at 4 am.
It may seem obvious now, but the solution was simple:
Send batches of 250 rows (yes, I tried batches of 100, 200, 500, 1,000...).
This is how it looks now on development (15s) and production (55s).
I hope this was somewhat useful if you're using a serverless environment.
Happy to answer any technical questions :D