prisma / prisma

Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB
https://www.prisma.io
Apache License 2.0
39.1k stars 1.53k forks source link

Docker Local: Prisma Postgresql Pool Startup Latency #15851

Open arkmech opened 1 year ago

arkmech commented 1 year ago

Bug description

I have a single instance of Prisma client stored in global variable.

There is significant latency on initial request to my API with Prisma when initializing the connection pool.

After I see this log: prisma:info Starting a postgresql pool with 5 connections., aka connection pool has finished initializing, then the requests are regular speed.

Docker for both API (Node.js Typescript Apollo Server Express Prisma) and Postgresql

I use Nodemon for development, so every change, rebuilds and restarts my API, thus Prisma needs to build a new connection pool again. Aka every change to code, the first request has significant latency.

How to reproduce

  1. Docker compose
    • Typescript Node.js Apollo Server Express Prisma API
    • Postgres 15-alpine
  2. prisma.ts with
    
    import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient({ log: [ { emit: 'event', level: 'query', }, { emit: 'stdout', level: 'error', }, { emit: 'stdout', level: 'info', }, { emit: 'stdout', level: 'warn', }, ], });

prisma.$on('query', (e) => { console.log('Query: ' + e.query); console.log('Params: ' + e.params); console.log('Duration: ' + e.duration + 'ms'); });

export default prisma;

`prisma` instance passed throughout `context` of `Apollo Server`

### Expected behavior

No latency when establishing Postgres connection pool with Prisma.

### Prisma information

Have about 400+ tables. Although most tables have no data.

### Environment & setup

- OS: macOs
- Database: PostgreSQL
- Node.js version: v16.15.0

### Prisma Version

4.4.0

janpio commented 1 year ago

Could it be that opening a connection to the database just takes this amount of time you are observing? If not, can you share a minimal reproduction of this please?

arkmech commented 1 year ago

Could it be that opening a connection to the database just takes this amount of time you are observing? If not, can you share a minimal reproduction of this please?

When I first started using prisma, virtually no latency. It seems that after my database tables increased to 350+ tables, I had latency. Prisma client generation takes quite some time as well. But not sure how this would effect initial connection pooling latency.

How do I share a minimal reproduction without sharing my database models

janpio commented 1 year ago

The easiest way would be to see if a manually created list of fake models (foo1, foo2) etc is enough to reproduce. Might need to add some fields and relations as well - but that already tells us something what triggers this.

Alternatively you can also share in private via schemas@prisma.io and/or invite me to a GitHub repo. Will only be shared internally.

janpio commented 1 year ago

Hey @arkmech, are you still around?

We recently improved the startup performance a lot (read more about it in our blog post: https://www.prisma.io/blog/prisma-and-serverless-73hbgKnZ6t). This also applies to non serverless environments, so I am pretty positive that by upgrading to the newest version and enabling the jsonProtocol preview feature you could seriously improve this situation and probably get rid of the problem overall.

arkmech commented 1 year ago

@janpio, hey yeah I haven't been working with Prisma lately, but when I get back to it I will check it out!

janpio commented 8 months ago

@arkmech Did you by chance get back to this along the way? We still have no idea what might be going on there, and as there have been no other similar reports it is very hard to figure out what might be going on.