We just shipped add-mcp: think npx skills but for MCPs. One command to install MCPs across all your editors and agents
/With ORMs/Prisma

Use Neon read replicas with Prisma

Learn how to scale Prisma applications with Neon read replicas

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.

In this guide, we'll show you how you can leverage Neon read replicas to efficiently scale Prisma applications using Prisma Client's read replica extension: @prisma/extension-read-replicas.

Prerequisites

Create a read replica

You can create read replicas for any branch in your Neon project.

note

The Free plan is limited to a maximum of 3 read replica computes per project.

You can add a read replica by following these steps:

  1. In the Neon Console, select Branches.

  2. Select the branch where your database resides.

  3. Click Add Read Replica.

  4. On the Add new compute dialog, select Read replica as the Compute type.

  5. 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.

    note

    The compute size configuration determines the processing power of your database. More memory means more processing power but also higher compute costs. For information about compute costs, see Billing metrics.

  6. When you finish making selections, click Create.

    Your read replica compute is provisioned and appears on the Computes tab of the Branches page.

Alternatively, you can create read replicas using the Neon API or Neon CLI.

API
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"
  }
}
' | jq

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.

  1. Click the Connect button on your Project Dashboard. On the Connect to your database modal, select the branch, the database, and the role you want to connect with.

  2. Under Compute, select a Replica compute.

  3. 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:

    postgresql://alex:AbC123dEf@ep-cool-darkness-123456.us-east-2.aws.neon.tech/dbname?sslmode=require&channel_binding=require

    If you expect a high number of connections, enable the Connection pooling toggle to add the -pooler flag to the connection string.

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

@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.

  1. Install the extension in your Prisma project:

    npm install @prisma/extension-read-replicas
  2. Extend your Prisma Client instance by importing the extension and creating separate adapters for your primary and replica connections:

    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],
      })
    );

    note

    In Prisma 7, the read replicas extension requires you to pass an array of PrismaClient instances configured with adapters, not connection URLs. Each replica needs its own adapter and client instance.

    note

    You can pass multiple replica clients if you want to use multiple read replicas. Neon supports adding multiple read replicas to a database branch. A replica is selected randomly for each read query.

    // Create adapters for multiple replicas
    const replicaAdapter1 = new PrismaNeon({
      connectionString: process.env.DATABASE_REPLICA_URL_1
    });
    const replicaAdapter2 = new PrismaNeon({
      connectionString: process.env.DATABASE_REPLICA_URL_2
    });
    
    // Create replica clients
    const replicaClient1 = new PrismaClient({ adapter: replicaAdapter1 });
    const replicaClient2 = new PrismaClient({ adapter: replicaAdapter2 });
    
    // Extend primary client with multiple replicas
    const prisma = new PrismaClient({ adapter: mainAdapter }).$extends(
      readReplicas({
        replicas: [replicaClient1, replicaClient2],
      })
    );

    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 $transaction queries are sent to the primary compute defined by DATABASE_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:

    const posts = await prisma.$primary().post.findMany()

    This Prisma Client query will be routed to your primary database.

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?

Join our Discord Server to ask questions or see what others are doing with Neon. For paid plan support options, see Support.

Last updated on

Was this page helpful?