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

FaaSでPrisma(DBMS)を使う際のドキュメント

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

FaaSが呼ばれる時に、毎度コンテナが立ち上がるか再利用されるかを制御することができないという話。

Description

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

FaaSの処理が終了しても、コンテナ内でプロセスとコネクションが残ってしまってDBMSのリソースを消耗させてしまうという話。

Description

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

リクエストがスパイクして、FaaSのプロセスが多数同時に立ち上がるとDBMS側のリソースが枯渇するという話。

Description

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"
  },
ザ・ロングラン 人生を走り出す日まで (Kindle Single)
[ad] ザ・ロングラン 人生を走り出す日まで (Kindle Single)
ミシュカ シュバリー, 村山 美雪 (Kindle版)