This example shows how to use a Prisma Client extension to automatically retry transactions that fail due to a write conflict / deadlock timeout. Failed transactions will be retried with exponential backoff and jitter to spread out the database load under heavy traffic contention.
NOTE: This extension overwrites the
$transaction
method on a Prisma Client instance, and relies on this method being defined on a generated Prisma Client. It may not currently have correct type definitions when packaged and distributed as a reusable extension.
This extension is provided as an example only. It is not intended to be used in production environments.
Please read the documentation on client
extensions for more information.
Clone this repository:
git clone git@github.com:sbking/prisma-client-extensions.git
Create a .env
file and install dependencies:
cd retry-transactions
cp .env.example .env
npm install
Run the following command to start a new Postgres database in a Docker container:
docker compose up -d
Run this command to apply migrations to the database:
npx prisma migrate deploy
Run the following command to add seed data to the database:
npx prisma db seed
To run the script.ts
file, run the following command:
npm run dev