Skip to content

Fix Prisma memory leak and add configurable connection pool #281

@tsurbs

Description

@tsurbs

Summary

This PR addresses memory issues in Railway by implementing the Prisma singleton pattern and adding configurable connection pool sizing.

Changes

  • Default connection pool size reduced from 10 to 3 connections
  • Added PRISMA_CONNECTION_LIMIT environment variable to override the default
  • Automatically appends connection_limit to DATABASE_URL if not already set
  • Implements Prisma singleton pattern using globalThis to prevent multiple instances

Why This Matters

In containerized environments like Railway:

  • Each PrismaClient instance opens its own connection pool (default: 10 connections)
  • Multiple instances can accumulate during container restarts or hot reloads
  • Each connection consumes memory for buffering
  • Smaller containers (512MB-1GB) can quickly run out of memory

Configuration

Set the PRISMA_CONNECTION_LIMIT environment variable in Railway to adjust:

PRISMA_CONNECTION_LIMIT=3  # for 512MB containers
PRISMA_CONNECTION_LIMIT=5  # for 1GB+ containers

Or set it directly in your DATABASE_URL:

postgresql://...?connection_limit=3

Testing

  • Code compiles successfully
  • Connection pool limit is correctly applied
  • Environment variable override works as expected

Fixes memory issues observed in Railway deployment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions