Next.js Discord

Discord Forum

Sorry, Too many clients

Unanswered
Polish posted this in #help-forum
Open in Discord
PolishOP
Hi.

I made the leaderboard for my clients project. But I have an error on production.

Reloading this page on production is causing a lot connections to my postgres database (hosted on railway) and database is just not responding.

I want to fix this issue.

I have been trying to fix it with pgbounce and pooling but nothing seems to work. please help <3

import { api } from "~/trpc/server";
import LeaderboardClient from "./_components/client";

export default async function Leaderboard() {
  const { totalUserCount, totalUniqueViews } = await api.account.get();

  await api.account.get.prefetch();

  const users = await api.account.getPaginated({
    pageIndex: 0,
    pageSize: 10,
  });

  return (
    <LeaderboardClient
      totalUniqueViews={totalUniqueViews}
      totalUsers={totalUserCount}
      users={users.data}
      totalCount={users.totalCount}
    />
  );
}

25 Replies

@B33fb0n3 if you have a serverfull env, then you can leverage the pooling option like you mentioned (that does not work in a serverless env). Make also sure to close your connections after you used them. If that still does not solve your issue, you can add an "idle timeout" to your database connection. After a specific amount of time the connection will be closed automatically. Dont reuse the connection then
PolishOP
if you are free can you please check my set up?

this is my db/index.ts

import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { env } from "~/env";

import * as accounts from "./schema/accounts";
import * as analytics from "./schema/analytics";
import * as badges from "./schema/badges";
import * as customization from "./schema/customization";
import * as externalLinks from "./schema/external-links";
import * as media from "./schema/media";
import * as sessions from "./schema/sessions";
import * as socialLinks from "./schema/social-links";
import * as subscriptions from "./schema/subscriptions";
import * as users from "./schema/users";
import * as verificationTokens from "./schema/verification-tokens";

const schema = {
  ...accounts,
  ...analytics,
  ...customization,
  ...externalLinks,
  ...media,
  ...sessions,
  ...socialLinks,
  ...subscriptions,
  ...users,
  ...verificationTokens,
  ...badges,
};

const globalForDb = globalThis as unknown as {
  conn?: postgres.Sql;
};

const conn =
  globalForDb.conn ??
  postgres(env.DATABASE_URL, {
    max: 50,
    idle_timeout: 30,
    connection: {
      application_name: "Frag.bio",
    },
  });

if (env.NODE_ENV !== "production") globalForDb.conn ??= conn;

export const db = drizzle(conn, { schema });
I have db idle_timeout set up already
@B33fb0n3 Where do you host?
PolishOP
railway
as I did a research I found out railway don't have built in pgbouncer so I found the most popular template and connected it with my postgres db
@Polish railway
I did a bit of research about railway right now and it looks pretty serverfull. So it’s inside containers and these containers will also shutdown after an amount of time, right?
Do you know what a singleton is? Can you build one for you connection client? It can look like that:
let queryClient;
if (!queryClient) queryClient = postgres(getDbConnectionString());

export const db = drizzle(queryClient, { schema });
@Polish isn’t this a singleton?
it does not look like a singleton explicit to your one connection. Maybe I am wrong here 🤔
@Polish solved?
PolishOP
no
I added rate limit to it but
still dont think its a good practice
because if like 10 users view it the same time it will crash
@Polish because if like 10 users view it the same time it will crash
yea, that's not really a solution. When the connections will be closed after 30 seconds, you can also allow unlimited connections
PolishOP
how?
doesn’t postgres itself has limited?
@Polish how?
set it to -1
@Polish solved?
PolishOP
still no
@Polish still no
did you srt it to -1? Also: what else do you need to solve the issue?