afterbuild/ops
ERR-324/Scale · pooling
ERR-324
Prisma P1001 · FATAL: sorry, too many clients already

appears when:Traffic spikes past 30-50 concurrent users and functions start returning 500s with P1001 or connection timeouts

app crashes under load

Serverless spawns one Postgres connection per isolate. Multiply by concurrent invocations and you blow past the database limit. Pooling, not scaling, is the fix.

Last updated 8 August 2026 · 8 min read · By Hyder Shah
Direct answer
app crashes under load is almost always database connection pool exhaustion. Switch DATABASE_URLto your provider’s pooled endpoint — on Supabase that is port 6543 in transaction mode; on Neon it is the same port with -pooler added to the endpoint ID in the hostname. Keep the unpooled hostname in DIRECT_URL for migrations. Append ?connection_limit=5 so each serverless isolate uses a small pool. Move slow downstream work (Stripe, OpenAI, email) to a queue (Inngest, QStash) so handlers return in under a second.

How do I fix app crashes under load?

.env (Vercel Production)
bash
01# .env — switch to pooled connection string (Supabase)02# Pooled endpoint on port 6543 — transaction mode03DATABASE_URL="postgresql://postgres.<ref>:<pw>@aws-0-<region>.pooler.supabase.com:6543/postgres?pgbouncer=true&connection_limit=5"04 05# Direct URL for migrations only (pgbouncer breaks prepared statements)06DIRECT_URL="postgresql://postgres.<ref>:<pw>@aws-0-<region>.pooler.supabase.com:5432/postgres"07 08# --- Neon: the pooler is a HOSTNAME change, not a port change ---09# Pooled  = endpoint ID + "-pooler"   (app traffic)10# Direct  = endpoint ID as-is          (migrations, pg_dump, logical replication)11# DATABASE_URL="postgresql://user:pw@ep-cool-darkness-123456-pooler.us-east-2.aws.neon.tech/db?sslmode=require"12# DIRECT_URL="postgresql://user:pw@ep-cool-darkness-123456.us-east-2.aws.neon.tech/db?sslmode=require"
Pooled DATABASE_URL on port 6543 fixes the most common AI-built scale crash — DIRECT_URL is for migrations only

Not a developer? We fix this for you at a fixed price: $299 emergency triage, done in 48 hours or fully refunded. Not sure what else is broken? Send the repo for the $49 audit.

What if the quick fix doesn't work?

01 · Move Stripe webhook processing to a queue

app/api/stripe/webhook/route.ts
typescript
01// app/api/stripe/webhook/route.ts02export async function POST(req: Request) {03  const body = await req.text();04  const sig = req.headers.get("stripe-signature")!;05  const event = stripe.webhooks.constructEvent(body, sig, secret);06 07  // Hand to Inngest — return 200 in <100ms08  await inngest.send({ name: "stripe/invoice.paid", data: event });09  return new Response("ok", { status: 200 });10}

02 · Add a rate limiter to the hot route

app/api/hot-route/route.ts
typescript
01import { Ratelimit } from "@upstash/ratelimit";02import { Redis } from "@upstash/redis";03 04const limiter = new Ratelimit({05  redis: Redis.fromEnv(),06  limiter: Ratelimit.slidingWindow(10, "10 s"),07});08 09export async function POST(req: Request) {10  const ip = req.headers.get("x-forwarded-for") ?? "unknown";11  const { success } = await limiter.limit(ip);12  if (!success) return new Response("rate limited", { status: 429 });13  // ...real handler14}

03 · Bound module-level caches with LRU

lib/cache.ts
typescript
01import { LRUCache } from "lru-cache";02 03// ❌ unbounded — leaks across warm invocations04// const cache = new Map();05 06// ✅ bounded, with TTL07const cache = new LRUCache<string, unknown>({08  max: 500,09  ttl: 60_000,10});

Neon and Supabase select the pooler differently — and the wrong guess is silent

“Switch to the pooled connection string” is the standard advice and it is provider-shaped, not generic. Supabase moves you to a different port: 6543 for transaction mode, 5432 for the direct one. Neon keeps the port and changes the hostname: you add -pooler to the endpoint ID, so ep-cool-darkness-123456 becomes ep-cool-darkness-123456-pooler. Carry the Supabase instruction to a Neon URL and you will change 5432 to 6543, get a connection failure or the same unpooled endpoint back, and conclude that pooling did not help.

ProviderPooled (app traffic)Direct (migrations)What changes
Neonep-<id>-pooler.<region>.aws.neon.techep-<id>.<region>.aws.neon.techHostname. Port stays the same
SupabasePort 6543 (transaction mode)Port 5432Port. Hostname stays the same

Why migrations need the endpoint your app does not use

Neon runs PgBouncer in transaction mode (pool_mode=transaction), so a connection returns to the pool the moment each transaction completes. That is the behaviour a request handler wants and the behaviour a migration cannot survive: two statements in the same session may land on two different backend connections, so anything that expects session state to persist between transactions breaks. Neon accordingly recommends the direct connection for schema migrations, because migration tools may not support transaction pooling; for pg_dump and pg_restore, because they use SET statements; for logical replication, which needs a persistent connection; and for long-running analytics queries, so they do not contend for the pool.

Four feature categories are documented as unsupported on a pooled connection: SET / RESET session variables, LISTEN / NOTIFY, WITH HOLD cursors, and PREPARE / DEALLOCATE SQL-level prepared statements. Protocol-level prepared statements are supported, and that distinction is the one that decides whether your ORM is affected: a client that prepares over the wire protocol is fine, while application code issuing SQL-level PREPARE is not. When something works on the direct endpoint and fails on the pooled one, check that list before concluding the pool is broken.

Tried the fix and still broken?

Send your app URL. A senior engineer records a 10-minute teardown.

Free, and fully async — no call to book. A senior engineer walks your app on video: what’s broken, what’s risky, what to fix first. In your inbox within 24 hours. Want the code read line by line instead? Send the repo for the $49 audit.

Why do AI-built apps hit app crashes under load?

Serverless functions are cheap to scale horizontally and expensive on every other axis. Vercel, Netlify, and Cloudflare Workers spin up a new isolate for each cold request. Each isolate opens its own database connection. Prisma opens 17 by default. Multiply by 30 concurrent invocations and you ask Postgres for 510 connections. Supabase free tier caps direct connections at 60. Neon free tier caps at 100. The app dies at the pool ceiling, not at server capacity.

AI-generated code almost never configures PgBouncer, Supavisor, or Neon’s pgBouncer pool. The model writes DATABASE_URL with the direct-connect hostname because that is the default in Supabase or Neon onboarding. Under 20 users it works fine. Over 30 it issues P1001 or P2024, queues requests, and crashes the function with a timeout. Fix is a one-line change — switch to port 6543 on Supabase or the pooled endpoint on Neon — but until someone diagnoses the symptom, founders add CPU and memory wondering why nothing helps.

The second structural cause is work that belongs in a queue running in the request handler. AI-built Stripe webhooks, image processors, and LLM call chains frequently run inline in the API route. A single slow Claude call takes 20 seconds. Two concurrent requests occupy the runtime for 40 seconds between them. Vercel starts returning 504s. Fix is Inngest, Trigger.dev, or a Supabase Edge Function with a queue, not scaling the endpoint.

Which AI builders ship app crashes under load?

How often each AI builder ships this error and the pattern that produces it.

AI builder × app crashes under load
BuilderFrequencyPattern
LovableEvery scale testDirect DATABASE_URL on port 5432 — no pooling
Bolt.newCommonInline Stripe/OpenAI calls in webhook handler
v0CommonUnbounded module-level Map caches
CursorSometimesNo rate limit on signup or submission routes
Replit AgentCommonMissing DIRECT_URL — migrations fail on pooled conn

How do I stop app crashes under load recurring?

Still stuck with app crashes under load?

Emergency triage · $299 · 48h turnaround
We restore service and write the root-cause report.
start the triage →

app crashes under load questions

Why does my app work for 10 users and crash at 50?+
Most AI-built apps hit a connection pool ceiling around 20 to 30 concurrent requests. Prisma defaults to 17 connections, Supabase free tier allows 60 direct connections, and each serverless function holds its own pool. At 50 concurrent users you exhaust the pool, new requests queue, timeouts cascade, and the process crashes with ECONNREFUSED or P1001. Fix with connection pooling via Supavisor or PgBouncer, not more servers.
What is a connection pool exhaustion error?+
Prisma P1001, P2024 (connection timeout), or Postgres FATAL: sorry, too many clients already. Each serverless invocation opens new Postgres connections without reuse. Both Neon and Supabase offer a pooled endpoint that consolidates many serverless clients onto a few database connections — but they are selected differently, and this is where most fixes go wrong. On Supabase the pooled endpoint is a different port (6543, transaction mode). On Neon it is a different hostname: you add -pooler to the endpoint ID, so ep-cool-darkness-123456.us-east-2.aws.neon.tech becomes ep-cool-darkness-123456-pooler.us-east-2.aws.neon.tech, on the same port. Changing the port on a Neon URL does nothing.
Which Neon endpoint do I use — pooled or direct?+
Both, for different jobs. Neon runs PgBouncer in transaction mode (pool_mode=transaction), so connections return to the pool after each transaction completes — which is exactly what a serverless request handler wants and exactly what a migration does not. Neon recommends a direct (unpooled) connection for schema migrations, because migration tools may not support transaction pooling; for pg_dump and pg_restore, because they use SET statements; for logical replication, which needs a persistent connection; and for long-running analytics queries, to avoid contending for the pool. Point DATABASE_URL at the -pooler hostname for the app, and DIRECT_URL at the hostname without it for migrations.
Which Postgres features stop working on a pooled Neon connection?+
Neon documents four categories as unsupported with pooled connections: SET / RESET (session variables), LISTEN / NOTIFY, WITH HOLD CURSOR, and PREPARE / DEALLOCATE (SQL-level prepared statements). Protocol-level prepared statements are supported, which is the distinction that matters for ORMs — a client library that prepares statements over the wire protocol is fine, while application code issuing SQL-level PREPARE is not. If something worked on the direct endpoint and fails on the pooled one, check that list before assuming the pool is broken: transaction-mode pooling hands you a different backend connection per transaction, so anything that expects session state to persist between transactions is the thing that breaks.
How do I find a memory leak in a Next.js app?+
Vercel Function logs show memory per invocation. If the number climbs steadily instead of oscillating, the runtime is not being recycled between requests. Common causes: a module-level cache that never evicts, a large in-memory array that grows with each webhook, or a timer that never clears. Reproduce locally with clinic.js or node --inspect and take heap snapshots 30 seconds apart.
Does adding a rate limit fix load crashes?+
It masks them, sometimes usefully. A rate limit (upstash/ratelimit or Vercel's built-in) rejects excess traffic before it hits the broken path. Keeps the app alive for users who got through. Actual fix is still at the database or memory layer. Use rate limiting as a shield while fixing the root cause.
How much does a scale-to-10k engagement cost?+
Our Scale-to-10k playbook is $6,499 and covers connection pooling, queue offloading, rate limiting, autoscaling, and a load test proving 10,000 concurrent users. Includes full APM setup so the next bottleneck is visible before users see it. For a single bottleneck fix, the $499 performance pass or $299 Emergency Triage are lighter options.
Next step

Ship the fix. Keep the fix.

Emergency Triage restores service in 48 hours. Break the Fix Loop rebuilds CI so this error cannot ship again.

About the author

Hyder Shah leads Afterbuild Labs, shipping production rescues for apps built in Lovable, Bolt.new, Cursor, Replit, v0, and Base44. our rescue methodology.

Sources