PrismaP
Prisma2y ago
16 replies
Tauhoo

Connections are over connection limit

I deployed my NextJS + Prisma on a dedicated server then I noticed that the connections on my Postgres database is higher than the connection limit.
I set my connection limit to 30 (?connection_limit=30) but the connections go to around 70 - 80. You can see it from the graph. I already made sure if the data from the graph is correct by using this query.
SELECT
  client_addr,
  COUNT(*) AS connection_count
FROM pg_stat_activity
GROUP BY client_addr
ORDER BY connection_count DESC;

And, here is how I initiate the Prisma client.
import { PrismaClient } from "@prisma/client";
import { updatePrismaMetrics } from "./monitor/database";

const prismaClientSingleton = () => {
  const prisma = new PrismaClient();

  setInterval(async () => {
    updatePrismaMetrics(prisma);
  }, 10000);
  return prisma;
};

declare const globalThis: {
  prismaGlobal: ReturnType<typeof prismaClientSingleton>;
} & typeof global;

export const db = globalThis.prismaGlobal ?? prismaClientSingleton();

globalThis.prismaGlobal = db;

Another weird thing is that I extracted metric from the Prisma to Statsd but The data doesn't match with data from Postgres database. The Prisma idle connection is less than data from Postgres. You can see from the graph.


So, all I want to know is
1. How can connections go over limit?
2. Why are Prisma metric and Postgres data different?
Screenshot_2567-08-05_at_16.30.56.png
Screenshot_2567-08-05_at_16.31.04.png
Solution
In the end, it's not problem with Pisma. It's problem with our PM2 config. We set instances to "max" which is too much. I reduced the instances number and it works now. Thank you for support. @Nurul (Prisma)
Was this page helpful?