Skip to main content
← Back to articles

Prisma Read/Write Replica Patterns

Routing analytics queries to replicas while keeping writes on the primary — without consistency surprises.

Nestlancer Editorial

Share

Read replicas absorb analytics and list traffic—but replication lag can surprise users who write then immediately read. Prisma dual-client setups need explicit routing rules, not hope.

Dual client setup

@Injectable()
export class PrismaService {
  readonly write = new PrismaClient({ datasources: { db: { url: primaryUrl } } });
  readonly read  = new PrismaClient({ datasources: { db: { url: replicaUrl } } });
}

Inject read for dashboards and exports; write for mutations and transactional flows.

Consistency rules

Query typeTarget
User profile after login updatePrimary
Monthly analytics aggregationReplica
Paginated blog list (public)Replica with 30s lag OK
Idempotency key lookup post-paymentPrimary

Document lag tolerance per endpoint in OpenAPI descriptions.

Avoiding connection storms

Size replica pools separately from primary. Burst read traffic should not starve write connections. Monitor pg_stat_replication lag and fail over reads to primary when lag exceeds SLO.

Testing replicas locally

Docker Compose with primary + replica teaches teams replication behavior before production. Seed integration tests against both clients.

Read/write separation is a performance win when consistency expectations are explicit—not assumed.

Comments

Loading comments…

Related posts