created: 2021-03-29T06:34:29.000Z


PrismaのドキュメントにはFaaSで使う場合のアドバイスがいろいろ書いてあった。 あるあるな困りごとについてよくまとまっているのでメモ。

たとえば 「FaaSのプロセスがいくつもコネクションを張る必要はなく、またそれをするとスパイク時にDBMS側のリソースが枯渇するのでやめましょうね」というようなことが色々と書いてあった。

Serverless (FaaS): It's recommended to set the connection limit to 1 if you're not using an external connection pooler because each incoming request starts a short-lived Node.js process. This can cause the database connection pool to be quickly exhausted from a short spike in user traffic.

Container reuse



It is not guaranteed that subsequent nearby invocations of a function will hit the same container. AWS can choose to create a new container at any time.

Potential Solution

Code should assume the container to be stateless and create a connection only if it does not exist. Prisma Client JS already implements that logic.

Zombie connections



The containers that are marked to be removed and are not being reused still keep a connection open and can stay in that state for some time (unknown and not documented from AWS), this can lead to sub-optimal utilization of the DB connections

Potential Solution

One potential solution is to use a lower idle connection timeout. Another solution can be to clean up the idle connections in a separate service


Database connection pool exhaustion



Concurrent requests create multiple new connections to the database thereby exhausting the database's connection limit.

Potential Solution

Add a connection pooler, e.g. PgBouncer or limiting the serverless function concurrency to a number lower than the database's connection limit.


GCP の Cloud Function で Prisma を動かすサンプルが公開されていた。

GCP allows deploying just the project and fetches the modules for the user. To generate the Prisma client, we use the npm postinstall hook. Using the gcp-build hook does not work, since Google regenerates node_modules after that hook is executed.

  "scripts": {
    "postinstall": "CI=1 && PRISMA_TELEMETRY_INFORMATION='e2e-tests platforms/gcp-functions postinstall' && yarn prisma generate"