Next.js Discord

Discord Forum

Max Redis connections error

Unanswered
Backxtar posted this in #help-forum
Open in Discord
I my app runs in max connections etablished error. Dont know why.

import { Redis } from "ioredis";
import { createPool, Pool } from "generic-pool";

interface RedisProps {
  host: string;
  port: number;
  password: string;
}

const getRedisProdData = (): RedisProps => {
  if (
    process.env.REDIS_HOST &&
    process.env.REDIS_PORT &&
    process.env.REDIS_PASSWORD
  ) {
    return {
      host: process.env.REDIS_HOST,
      port: Number(process.env.REDIS_PORT),
      password: process.env.REDIS_PASSWORD,
    };
  }

  throw new Error("REDIS_PROD_ENV are not defined!");
};

const getRedisDevUrl = () => {
  if (process.env.REDIS_DEV_URL) {
     return process.env.REDIS_DEV_URL;
  }
  throw new Error("REDIS_DEV_URL is not defined!");
};

const factory = {
  create: async (): Promise<Redis> => {
    const client = new Redis(getRedisDevUrl());
    client.on("error", (err) => console.error("Redis Client Error", err));
    return client;
  },
  destroy: async (client: Redis): Promise<void> => {
    await client.quit();
  },
  validate: async (client: Redis): Promise<boolean> => {
    return client.status === "ready";
  },
};

const options = {
  max: 10, // Maximum number of clients in the pool
  min: 2, // Minimum number of clients in the pool
  idleTimeoutMillis: 30000, // Close idle clients after 30 seconds
  evictionRunIntervalMillis: 30000, // Run the eviction process every 30 seconds
};

const redisPool: Pool<Redis> = createPool(factory, options);
export default redisPool;

2 Replies

i use it like:

I use it like:

export async function getHero(id: string | number) {
  const client = await redisPool.acquire();

  try {
    const data = await client.get("heroes-data");
    if (!data) return undefined;

    const heroes = JSON.parse(data) as HeroProps[];
    if (!heroes) return undefined;

    const hero = heroes.find((hero: HeroProps) => hero.id == id);
    return hero;
  } catch (error) {
    console.error("getHero()", error);
  } finally {
    await redisPool.release(client);
  }
}
i get this error

Redis Client Error ReplyError: ERR max concurrent connections exceeded. Limit: 100. See https://upstash.com/docs/redis/troubleshooting/max_concurrent_connections for details