Use Neon read replicas with Prisma
A Neon read replica is an independent read only compute that performs read operations on the same data as your primary read write compute, which means adding a read replica to a Neon project requires ...
A Neon read replica is an independent read-only compute that performs read operations on the same data as your primary read-write compute, which means adding a read replica to a Neon project requires no additional storage.
A key benefit of read replicas is that you can distribute read requests to one or more read replicas, enabling you to easily scale your applications and achieve higher throughput for both read-write and read-only workloads.
For more information about Neon's read replica feature, see Read replicas.
This guide shows how to use Neon read replicas to scale Prisma applications using Prisma Client's read replica extension: @prisma/extension-read-replicas.
Prerequisites
Section titled “Prerequisites”- An application that uses Prisma with a Neon database. If you haven't set up Prisma yet, see Connect from Prisma to Neon.
Create a read replica
Section titled “Create a read replica”You can create read replicas for any branch in your Neon project.
You can add a read replica by following these steps:
-
In the Neon Console, select your branch from the project/branch menu at the top of the sidebar.
-
Under Postgres database, select Computes.
-
Click Add Read Replica.
-
On the Add new compute dialog, select Read replica as the Compute type.
-
Specify the Compute size settings options. You can configure a Fixed Size compute with a specific amount of RAM (the default) or enable autoscaling by configuring a minimum and maximum compute size. You can also configure the Scale to zero setting, which controls whether your read replica compute is automatically suspended due to inactivity after 5 minutes.
-
When you finish making selections, click Create.
Your read replica compute is provisioned and appears on the Computes tab under Postgres database.
Alternatively, you can create read replicas using the Neon API or Neon CLI.
curl --request POST \
--url https://console.neon.tech/api/v2/projects/late-bar-27572981/endpoints \
--header 'Accept: application/json' \
--header "Authorization: Bearer $NEON_API_KEY" \
--header 'Content-Type: application/json' \
--data '
{
"endpoint": {
"type": "read_only",
"branch_id": "br-young-fire-15282225"
}
}
' | jqneon branches add-compute mybranch --type read_onlyRetrieve the connection string for your read replica
Section titled “Retrieve the connection string for your read replica”Connecting to a read replica is the same as connecting to any branch in a Neon project, except you connect via a read replica compute instead of your primary read-write compute. The following steps describe how to retrieve the connection string (the URL) for a read replica from the Neon Console.
-
Click the Connect button in the Console nav. On the Connect to your branch modal, select the branch, the database, and the role you want to connect with.
-
Under Compute, select a Replica compute.
-
Select the connection string and copy it. This is the information you need to connect to the read replica from your Prisma Client. The connection string appears similar to the following:
Bash postgresql://alex:AbC123dEf@ep-cool-darkness-123456.us-east-2.aws.neon.tech/dbname?sslmode=require&channel_binding=requireIf you expect a high number of connections, enable the Connection pooling toggle to add the
-poolerflag to the connection string.
Update your env file
Section titled “Update your env file”In your .env file, set a DATABASE_REPLICA_URL environment variable to the connection string of your read replica. Your .env file should look something like this, with your regular DATABASE_URL and the newly added DATABASE_REPLICA_URL.
DATABASE_URL="postgresql://alex:AbC123dEf@ep-cool-darkness-123456.us-east-2.aws.neon.tech/dbname?sslmode=require&channel_binding=require"
DATABASE_REPLICA_URL="postgresql://alex:AbC123dEf@ep-damp-cell-123456.us-east-2.aws.neon.tech/dbname?sslmode=require&channel_binding=require"Notice that the endpoint_id (ep-damp-cell-123456) for the read replica compute differs. The read replica is a different compute and therefore has a different endpoint_id.
Configure Prisma Client to use a read replica
Section titled “Configure Prisma Client to use a read replica”@prisma/extension-read-replicas adds support to Prisma Client for read replicas. The following steps show you how to install the extension and configure it to use a Neon read replica.
-
Install the extension in your Prisma project:
Bash npm install @prisma/extension-read-replicas -
Extend your Prisma Client instance by importing the extension and creating separate adapters for your primary and replica connections:
JavaScript import 'dotenv/config'; import { PrismaClient } from '@prisma/client'; import { PrismaNeon } from '@prisma/adapter-neon'; import { readReplicas } from '@prisma/extension-read-replicas'; // Create adapter for primary connection const mainAdapter = new PrismaNeon({ connectionString: process.env.DATABASE_URL }); // Create adapter for replica connection const replicaAdapter = new PrismaNeon({ connectionString: process.env.DATABASE_REPLICA_URL }); // Create replica client const replicaClient = new PrismaClient({ adapter: replicaAdapter }); // Create primary client and extend with read replicas const prisma = new PrismaClient({ adapter: mainAdapter }).$extends( readReplicas({ replicas: [replicaClient], }) );When your application runs, read operations are sent to the read replica. If you specify multiple read replicas, a read replica is selected randomly.
All write and
$transactionqueries are sent to the primary compute defined byDATABASE_URL, which is your read/write compute.If you want to read from the primary compute and bypass read replicas, you can use the
$primary()method in your extended Prisma Client instance:Bash const posts = await prisma.$primary().post.findMany()This Prisma Client query will be routed to your primary database.
Examples
Section titled “Examples”This example demonstrates how to use the @prisma/extension-read-replicas extension in Prisma Client. It uses a simple TypeScript script to read and write data in a Postgres database.
Need help?
Section titled “Need help?”Join our Discord Server to ask questions or see what others are doing with Neon. For paid plan support options, see Support.